-3

This code should accept three inputs represent the money: "cent", "paper" and "coins", then calculate the total amount of the money:

int coins = 0;
int paper = 0;
int cents = 0;
int transaction = 0;
int total_money = coins + paper + cents;

cout << "Do you want to fill your piggy bank?";
cin >> transaction;

while (transaction != 2)
{
    cout << "How many paper money? :";
    cin >> paper;
    cout << "How many coins ? :";
    cin >> coins;
    cout << "How many cents? :";
    cin >> cents;
    cout << "Do you want another transaction?";
    cin >> transaction;
    if (transaction == 2)
    {
        cout << "total in piggy bank" << total_money << endl;
    }
    else
    {
        cout << "End of the program";
    }

    return 0;
}

What seems to be the problem here?

thedemons
  • 1,139
  • 2
  • 9
  • 25
  • [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Jesper Juhl Oct 23 '22 at 08:07
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Oct 23 '22 at 08:08
  • Try not to use `using namespace std;`. It will savbe you some time now, but it is not that good in big projects (something about nameclashes). So just learn yourself to type `std::` where needed. – Pepijn Kramer Oct 23 '22 at 08:10
  • Another tip : transaction models something else then just a number. It is something unique, so you could make an enum class for that. e.g. `enum class transaction_choice { withdraw, deposit };` and then you can use `if (transaction == transaction_choice::deposit)` which makes your code read more like a story (which good code should do). – Pepijn Kramer Oct 23 '22 at 08:13
  • @PepijnKramer unfortunately, every single programming tutorial/course teaches you to use `using namespace std;`. It makes a bad practice for every new programmer. I couldn't count how many times I've encountered that line of code, one good side of it is that it indicates to the reader that "This code was written by an inexperienced programmer" – thedemons Oct 23 '22 at 08:16
  • 2
    In fact most C++ (training) material is seriously outdated. That is because C++ is evolving but training material is not :(. Also a lot of C++ code there tries to teach datastructures using C++, which is way different then teaching C++ on its own. All i all it is kind of like teaching about steam engines while we are already using electric motors. Worst of all are the "competitive coding sites" which will promise instant gratification but teach the most horrible C++ I've ever seen. – Pepijn Kramer Oct 23 '22 at 08:34
  • I mean you *could* make code like this work, but this gets much more complex than needed and probably exceeds your current knowlege of C++: https://godbolt.org/z/3chvc1c8f – fabian Oct 23 '22 at 09:03

1 Answers1

3

You need to do the addition after you get the values from the user, not before.

Like this (for example)

if (transaction == 2)
{
    int total_money = coins + paper + cents;
    cout<<"total in piggy bank" <<total_money<< endl;
}

A program is a set of statements that are executed in a particular order. Variables in that program are updated at certain points in the execution of the program. If you get your code in the wrong order then the variables are not going to have the values you expect them to.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thank you so much sir, i am a newbie in programming, thank you for your help – Dexter Hecali Oct 23 '22 at 08:10
  • @DexterHecali Welcome to StackOverflow, if this solved your problem, consider marking it as accepted by click on the checkmark on the left of the question. – thedemons Oct 23 '22 at 08:33