Loop ending when expected to continue

Oof, sorry about posting twice in a row... Another problem, likely very simple.

Skip to lines 64-84.

I'm trying to make it so that the user cannot proceed to purchase drinks if they have a balance of less than $2. Line 67 is what allows them to proceed. Line 76 is what gives them an error, and the loop SHOULD continue until their balance is equal to, or over $2.

Now, when I run the code, no matter what the balance is, the code will move on to the next loop if the user typed N/n. I even added quit = false, but the loop still ends.

I believe my issue is with how I have formatted inside the parenthesis
else if (choice == 'n' || choice == 'N' && money < 2)
I have never added multiple variables this way, am I going about it wrong?

Is if the order in which I have the if statements? Should the <$2 balance code be put before the end loop (balance >= $2) code?

Thanks again


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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
 #include <iostream>
using namespace std;



int main()
{
	bool quit = false;
	bool quit2 = false;
	char 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;
	money = 0;

	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' && money >= 2) {
			quit = true;
			cout << "You've inserted a total of: $" << money << endl;
			cout << "Please make a selection: " << endl;

		} 
		else if (choice == 'y' || choice == 'Y') {

		} 
		else if (choice == 'n' || choice == 'N' && money < 2) {
			cout << "You don't have sufficient funds to make a purchase." << endl;
			quit = false;
		} 
		else {
			cout << "Please enter only (Y/N): " << endl;
			cout << "Add more coins/bills? (Y/N)" << endl;
			cin >> choice;

		}
	}

		
	while (quit2 == false) {

		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;

		if (drink == 'A' || drink == 'a') {
			money = (money - 1.5);

		}
		else if (drink == 'B' || drink == 'b') {
			money = (money - 2);

		}
		else if (drink == 'C' || drink == 'c') {
			money = (money - 2);

		}
		else if (drink == 'G' || drink == 'g') {
			money = (money - 2.25);

		}

		cout << "Coins/Bills: " << money << endl;
		cout << "Add more drinks? (Y/N)" << endl;
		cin >> choice;

		if (choice == 'n' || choice == 'N') {
			quit2 = true;
			cout << "Take your change of $" << money << " and enjoy your drink(s)!" << endl;

		}
		else if (choice == 'y' || choice == 'Y') {

		}
		else {
			cout << "Please enter only (Y/N): " << endl;
			cout << "Add more drinks? (Y/N)" << endl;
			cin >> choice;

		}
	}

}
You problem is the operator precedence. See:

http://www.cplusplus.com/doc/tutorial/operators/

&& is higher, so:

if (choice == 'n' || choice == 'N' && money >= 2)
is equivalent to
if (choice == 'n' || (choice == 'N' && money >= 2))


I guess you want this:

if ((choice == 'n' || (choice == 'N') && money >= 2)

Note the round brackets.
Thanks, I didn't realize there was a preference vs executing it in order on the line. That helps a lot!
Topic archived. No new replies allowed.