我对 C++ 很陌生,我试图弄清楚如何从包含多个变量的函数中获取特定变量。
所以在这里我有一个函数调用userInput()
,提示用户输入 3 个值。然后,该函数稍后会被多次调用,以便为每个特定函数提供用户输入的值。问题是,我尝试这样做的方式会导致整个userInput()
函数重复,这导致输出多次询问值(见下文)。
我知道我的代码可以减少很多,但是嘿,我在这里学习。因此,非常感谢任何关于任何部分的建议。代码审查帖子在这里,供参考。
代码
#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.输入每月总销售额:20000 2.输入县税率:0.02 3.输入每月总销售额:20000 4.输入国家税率:0.04 5.输入每月总销售额:20000 6.输入县税率:0.02 7.输入每月总销售额:20000 8.输入国家税率:0.04 [...ETC...] 17.县税金额为400 18.国税金额为800 19.总销量1200