一旦我编译它就可以正常输入,但是,当我从我的输入中得到结果时,我碰巧注意到在实际预算什一税中,它没有给我输入的数字。
这是我到目前为止尝试过的:
#include <iostream>
#include <iomanip>
using namespace std;
float getIncome(float income);
float getBudgetLiving(float budgetLiving);
float getActualLiving(float actualLiving);
void display(float income, float tithing, float other, float budgetLiving, float actualLiving, float tax);
float getActualOther(float other);
float getActualTithing(float tithing);
float getActualTax(float tax);
float getIncome(float income) {
cout << "\tYour monthly income: ";
cin >> income;
return income;
}
float getBudgetLiving(float budgetLiving) {
cout << "\tYour Budgeted Living expenses: ";
cin >> budgetLiving;
return budgetLiving;
}
float getActualLiving(float actualLiving) {
cout << "\tYour actual Living expenses: ";
cin >> actualLiving;
return actualLiving;
}
我觉得我已经纠正了我所有的错误,但我觉得我的虚空才是问题所在。
void display(float income, float tithing, float other, float budgetLiving, float actualLiving, float tax) {
cout << "\nThe following is a report on your monthly expenses\n";
cout << "\tItem Budget Actual \n" <<
"\t================ =============== =============== \n";
cout << "\tIncome $" << setw(11) << income << " $" <<
setw(11) << income << endl;
cout << "\tTaxes $" << setw(11) << 0.00 << " $" <<
setw(11) << tax << endl;
cout << "\tTithing $" << setw(11) << 0.00 << " $" <<
setw(11) << tithing << endl; //There is an error that I dont undestand what I am doing
cout << "\tLiving $" << setw(11) << budgetLiving << " $" <<
setw(11) << actualLiving << endl;
cout << "\tOther $" << setw(11) << 0.00 << " $" <<
setw(11) << other << endl;
cout << "\t================ =============== =============== \n";
cout << "\tDifference $" << setw(11) << 0.00 << " $" <<
setw(11) << tax << endl;
}
float getActualOther(float other) {
cout << "\tYour actual other expenses: ";
cin >> other;
return other;
}
float getActualTithing(float tithing) {
cout << "\tYour actual tithe offering: ";
cin >> tithing;
return tithing;
}
float getActualTax(float tax) {
cout << "\tYour actual taxes withheld: ";
cin >> tax;
return tax;
}
float computeTax(float income) {
return 0;
}
float computeTithing(float income) {
return 0;
}
int main() {
cout << "This program keeps track of your monthly budget\n" <<
"please enter the following:\n";
float income = getIncome(income);
float budgetLiving = getBudgetLiving(budgetLiving);
float actualLiving = getActualLiving(actualLiving);
float tax = getActualTax(tax);
float tithing;
getActualTithing(tithing);
float other = getActualOther(other);
display(income, tithing, other, budgetLiving, actualLiving, tax);
return 0;
}
上面的代码在我认为有问题的地方有注释行。 谢谢!
最佳答案
看来您是编程新手,还不了解函数,如何传递参数以及如何从函数返回值。
这个bug很容易被发现。它当然与“无效”或其他东西无关。让我们从这里开始吧。
float tithing;
getActualTithing(tithing);
您定义了变量“什一税”,但没有对其进行初始化。您没有给它一个初始值(您必须始终初始化所有变量)。所以变量的值是“某物”。然后,与您的其他功能相比,您没有为“什一奉献”赋值。
您只需调用函数“getActualTithing(tithing);”但是您没有将结果值分配给十分之一变量。就像您在
中所做的那样float actualLiving = getActualLiving(actualLiving);
但这不是主要问题。如前所述,您不了解函数、参数和返回值。您迫切需要阅读相关内容。
你所有的函数都遵循相同的模式,就像在
float getActualTax(float tax) {
cout << "\tYour actual taxes withheld: ";
cin >> tax;
return tax;
}
出于某种我不知道的原因,您正在将变量“tax”按值传递给您的函数。这没有意义也没有效果。您正在传递值。因此,您将制作一个拷贝,然后覆盖该值。没有效果也没有意义。
请阅读:
- 按值传递
- 通过引用传递
- 通过指针传递
阅读有关函数和返回值的信息。
然后重新访问您的代码。
关于c++ - 如何解决什一奉献的未知数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57533471/