0

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.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
knorberg
  • 462
  • 5
  • 19
  • 2
    In high school, I knew this kid who did not want to go to math class or learn math from a book. Instead, he learned math on the street. Then he once accidentally divided by zero, and his head exploded. Tragic. Here are some good C++ books: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Eljay Sep 10 '18 at 12:41
  • Please, don't try to draw parallels with other languages. C++ is not Java, and if you try to learn C++ basing on Java experiences, you'll fail. See Eljay link for a list of good C++ books to start with. – Yksisarvinen Sep 10 '18 at 14:17
  • Thanks for the references! Java was the one programming class I took in high school. I'll try to look at this with some fresh eyes. Also, sorry, I typed the wrong tag in. I'm not sure why I put Java in instead of C++.... – knorberg Sep 10 '18 at 21:47

2 Answers2

4

You need to either pass your variables by reference or return them from your functions. As it stands right now, you are creating copies of each variable that are local to each function, modifying the copies, and then discarding them at the end of scope.

Returning values:

std::pair<int, int> Spent(int Total, int Spend) {
    ...
    return std::make_pair(Total, Spend);
}

// Getting values out
std::pair<int, int> result = Spent(Total, Spend);
int newTotal = result.first;
int newSpend = result.second;
// or
int newTotal, newSpend;
std::tie(newTotal, newSpend) = Spent(Total, Spend);
// or (C++17)
auto [newTotal, newSpend] = Spent(Total, Spend);

Reference parameters:

void Spent(int& Total, int& Spend) {
    // Modifications to Total and Spend in this function will apply to the originals, not copies
    ...
}
0x5453
  • 12,753
  • 1
  • 32
  • 61
0

Another option is to pass pointers:

void f(int* Total, int* Spent)
{
    *Total = ...;
    *Spent = ...;
}

Or use std::tuple:

std::tuple<int, int> f(int Total, int Spent)
{
    ...
    return std::tuple<int, int>(Total, Spent);
}
Avert
  • 435
  • 3
  • 17