我是 C++ 的新手,我正在尝试弄清楚如何从包含多个变量的函数中获取特定变量。
所以这里我有一个名为 userInput()
的函数,它提示用户输入 3 个值。然后,稍后会多次调用该函数,为每个特定函数提供用户输入的值。问题是,我尝试这样做的方式会导致整个 userInput()
函数重复,从而使输出多次询问值(见下文)。
我知道我的代码可以精简很多,但是嘿,我在这里学习。因此,非常感谢有关任何部分的任何建议。 Code Review post here , 供引用。
代码
#include <iostream>
using namespace std;
float countySalesTax(), stateSalesTax(), totalSales();
void userInput(), display();
int main()
{
countySalesTax(), stateSalesTax(), totalSales(), display();
return 0;
}
void userInput(float totalMonthlySales, float countyTaxRate, float stateTaxRate) // Not sure how to do multiple variables in a function
{
cout << "Enter the Total Monthly Sales:\t";
cin >> totalMonthlySales;
cout << "Enter the County Tax Rate:\t";
cin >> countyTaxRate;
cout << "Enter the State Tax Rate:\t";
cin >> stateTaxRate;
}
float countySalesTax()
{
float countyTax;
float totalMonthlySales, countyTaxRate, stateTaxRate;
userInput(totalMonthlySales, countyTaxRate, stateTaxRate); // Trying to call variables from userInput function?
countyTax = totalMonthlySales * countyTaxRate;
return countyTax;
}
float stateSalesTax()
{
float stateTax;
float totalMonthlySales, countyTaxRate, stateTaxRate;
userInput(totalMonthlySales, countyTaxRate, stateTaxRate); // same
stateTax = totalMonthlySales * stateTaxRate;
return stateTax;
}
float totalSales()
{
float totalSalesTax;
float countyTax = countySalesTax();
float stateTax = stateSalesTax();
totalSalesTax = countyTax + stateTax;
return totalSalesTax;
}
void display()
{
float countyTax = countySalesTax();
float stateTax = stateSalesTax();
float totalSalesTax = totalSales();
cout << "The amount of County Tax is\t" << countyTax << endl;
cout << "The amount of State Tax is\t" << stateTax << endl;
cout << "The amount of Total Sales Tax is\t" << totalSalesTax << endl;
}
输出
1. Enter the Total Monthly Sales: 20000 2. Enter the County Tax Rate: 0.02 3. Enter the Total Monthly Sales: 20000 4. Enter the State Tax Rate: 0.04 5. Enter the Total Monthly Sales: 20000 6. Enter the County Tax Rate: 0.02 7. Enter the Total Monthly Sales: 20000 8. Enter the State Tax Rate: 0.04 [...etc...] 17. The amount of County Tax is 400 18. The amount of State Tax is 800 19. The total sales is 1200
最佳答案
我认为您缺少的概念是您可以将值传递给函数。只需调用 userInput
一次,然后将输入的值传递给您的其他函数。像这样
float countySalesTax(float totalMonthlySales, float countyTaxRate);
int main()
{
float totalMonthlySales, countyTaxRate, stateTaxRate;
userInput(totalMonthlySales, countyTaxRate, stateTaxRate);
float countryTax = countySalesTax(totalMonthlySales, countyTaxRate);
float stateTax = stateSalesTax(totalMonthlySales, stateTaxRate);
...
return 0;
}
void userInput(float& totalMonthlySales, float& countyTaxRate, float& stateTaxRate)
{
cout << "Enter the Total Monthly Sales:\t";
cin >> totalMonthlySales;
cout << "Enter the County Tax Rate:\t";
cin >> countyTaxRate;
cout << "Enter the State Tax Rate:\t";
cin >> stateTaxRate;
}
float countySalesTax(float totalMonthlySales, float countyTaxRate)
{
float countyTax;
countyTax = totalMonthlySales * countyTaxRate;
return countyTax;
}
float stateSalesTax(float totalMonthlySales, float stateTaxRate)
{
float stateTax;
stateTax = totalMonthlySales * stateTaxRate;
return stateTax;
}
另一个错误是,如果你想从 userInput
返回多个值,你必须使用 references,即你必须使用 float&
而不是float
用于 userInput
的参数。
关于C++:从包含多个变量的函数中获取一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18790618/