Runtime issue when trying to print a sum

So I have spent the past 2 hours trying to figure this out. Can't wrap my head around it.

First I had "money + 1;" or "money + 5;" etc. depending on the selection the user inputs. After they type N/n I want it to print the total money they have inserted to the machine.

I had an error, so I changed it to "money = money + 5;" etc. I even tried adding parenthesis "money = (money + 5);" and tried it as an int instead of float.

However, this time it builds but I get a runtime error.
"Run-Time Check Failure #3 - The variable 'money' is being used without being initialized." (Where I am trying to print the sum)
Line 63: cout << "You've inserted: $" << money << endl;

What on earth am I doing wrong? When I run the code, it skips all the loops and goes straight to that line (63). Why?? Is there another, easier way to add the total bills the user inserted into the machine, and print them after they input n/N?

Here's a visual of what I am trying to achieve: https://i.ibb.co/6w0Vg1P/P-2-Vending.png

I would consider myself an amateur, so apologies if I don't immediately understand your suggestions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  #include <iostream>
using namespace std;



int main()
{
	bool quit = false;
	string drink;
	float total = 0.0f;
	float price;
	float totalsum;
	char choice;
	float money;

	int selection;

	
	float change;



	cout << "======================================" << endl;
	cout << "Welcome to the GATORS vending machine!  " << endl;
	cout << "======================================" << endl;

	while (quit == false) {

		cout << "Please insert your coins/bills:" << endl;
		cout << "(1) - $1  " << endl;
		cout << "(2) - $5  " << endl;
		cout << "(3) - $10  " << endl;
		cout << "(4) - $20  " << endl;
		cin >> selection;

		if (selection == 1) {
			cout << "You've inserted: $1" << endl;
			money = (money + 1);
			

		} else if (selection == 2) {
			cout << "You've inserted: $5" << endl;
			money = (money + 5);

        } else if (selection == 3) {
			cout << "You've inserted: $10" << endl;
			money = (money + 10);

		} else if (selection == 4) {
			cout << "You've inserted: $20" << endl;
			money = (money + 20);

		} else {
			cout << "Please enter a valid number!" << endl;
			
		}

		cout << "Add more coins/bills? (Y/N)" << endl;
		cin >> choice;

		if (choice == 'n' || choice == 'N') {
			quit = true;
			cout << "You've inserted: $" << money << endl;
			cout << "Please make a selection: " << endl;

		} else if (choice == 'y' || choice == 'Y') {
			
		} else {
			cout << "Please enter only (Y/N): " << endl;
			cout << "Add more coins/bills? (Y/N)" << endl;
			cin >> choice;

		}

		/*cout << "(A)quaveena                 $1.50" << endl;
		cout << "(B)epsi                     $2.00" << endl;
		cout << "(C)ool Cola                 $2.00" << endl;
		cout << "(G)atorade                  $2.25" << endl;
		cin >> drink;*/

	}
	

}
Last edited on
Imagine I asked you to tell me "MY age + 5". What would you reply to me?

Now I ask you "money + 5". What would you reply?

Oh shoot, so money = 0;?
Okay wow, well that solved my problem!

But, is there a correct way to add like that? Should it be:

money + [value];
money = money + [value];
money = (money + [value];

Which one is best practice? Are all OK?
Last edited on
money + [value];
This does nothing - it's just a number. If you wrote just:

3;

as a line of code, it wouldn't do anything. That's exactly equivalent to the line you suggested.

money = money + [value];

is fine. More succinctly, it's idiomatic in C++ to use:

money += [value];
Last edited on
I appreciate the help guys. I can't believe I made such a simple error.
Topic archived. No new replies allowed.