【发布时间】:2018-09-10 12:32:20
【问题描述】:
首先我想说我是 C++ 新手。我一直在学习网站并尝试了几个小时在我的代码周围改组并尝试新事物以试图解决这个问题。
当我在修改变量的函数中引用变量时,它返回正确的值。一旦离开该函数,即使我已将变量传递给下一个函数,值也会被重置。我什至到处添加 couts 来显示值以帮助我调试,但没有产生任何结果。有人可以指出我正确的方向吗?我将在下面发布我的代码。谢谢你们的帮助,伙计们。
#include <iostream>
//void Loop(int Total, int Spend);
//int NewTotal(int Total, int Spend);
//void Spent(int Total, int Spend);
void UserInput(int Total, int Spend);
// Loops back to UserInput() for next entry input
void Loop(int Total, int Spend)
{
UserInput(Total, Spend);
}
int NewTotal(int Total, int Spend)
{
std::cout << "Output of Total is: " << Total << std::endl;
std::cout << "Output of Spend is: " << Spend << std::endl;
return Total + Spend;
}
void Expense()
{
std::cout << "Please enter a description of your expense!" << std::endl;
char ExpenseDesc;
std::cin >> ExpenseDesc;
std::cout << "You described your expense as: " << std::endl;
std::cout << ExpenseDesc << std::endl;
}
void Spent(int Total, int Spend)
{
std::cout << "Please enter the amount you spent!" << std::endl;
std::cin >> Spend;
NewTotal(Total, Spend);
}
void UserInput(int Total, int Spend)
{
Expense();
Spent(Total, Spend);
std::cout << "Result of Total and Spend (NewTotal) is: " << Total + Spend << std::endl;
std::cout << "Record saved!" << std::endl;
std::cout << "So far, you have spent " << NewTotal(Total, Spend) << "!" << std::endl; //int Total & int Spend not retaining value when NewTotal(Total, Spend) gets called again to return value
std::cout << "Ready for next entry!" << std::endl;
Loop(Total, Spend);
}
int main()
{
int Total;
int Spend;
Spend = 0;
Total = 0;
UserInput(Total, Spend);
return 0;
}
本质上,这是一个非常基本的提示,要求您提供交易描述(它只接受一个字符,我需要修复它)和交易金额。完成该条目后,您可以再创建一个,并且程序应该将旧总计添加到新总计以得出到目前为止的总支出,然后重复提示。
【问题讨论】:
-
高中时,我认识一个不想上数学课或从书本上学习数学的孩子。相反,他在街上学习数学。然后他一次不小心被零除,脑袋炸了。悲惨。这里有一些不错的 C++ 书籍:stackoverflow.com/questions/388242/…
-
请不要试图与其他语言相提并论。 C++ 不是 Java,如果您尝试根据 Java 经验学习 C++,您将失败。请参阅 Eljay 链接以获取不错的 C++ 书籍列表。
-
感谢您的参考! Java 是我在高中时上的一门编程课。我会试着用一些新的眼光来看待这个。另外,对不起,我输入了错误的标签。我不知道为什么我用 Java 而不是 C++....