I want to start off by saying I am brand new to C++. I have been learning off of websites and trying for hours shuffling around my code and trying new things in an attempt to solve this.
When I reference a variable while in the function where the variable is modified, it returns the correct value. Once that function is left, even though I've passed the variables on to the next function, the values get reset. I even went about adding couts here and there to display values to help me debug, but nothing was yielding any results. Can someone point me in the right direction please? I'll post my code below. Thanks for the help, guys.
#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;
}
Essentially, this is a very basic prompt that asks you for a description of a transaction (which only accepts one character, I need to fix that) and a transaction amount. Upon finishing that entry, you can make another one and the program is supposed to add the old total to the new total to arrive at a total spendings so far, and then repeat the prompt.