Can't abort a process and return

This program is a soda machine simulation from my C++ book. There is only one part that I can't get to work. When the user decides that he/she doesn't want to make the purchase anymore, he/she enters "0" as the money deposited. This should then end and return to the "If you would like to make another purchase enter 1 to continue or 0 to stop."

I get one message that is correct: "Purchase canceled", but what is wrong is that it still runs: "*****Dispensing******" and "Here is your beverage! Enjoy!" AND it counts down the amount of sodas available in the machine and counts up the money in the machine. I don't want it to do that. It should stop and after "Purchase canceled" read: "Come back next time!" and NOT count the sale then go back to: "If you would like to make another purchase enter 1 to continue or 0 to stop."


Please help with my code. Thank you to ALL that read this. I appreciate your time! :)


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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <iostream> 
#include <string>
#include <iomanip>

using namespace std;

struct Drink
{
	Drink(string n, double cs, int cn)
	{
		name = n;
		cost = cs;
		count = cn;
	}
	Drink()
	{
		name = " ";
		cost = 0;
		count = 0;
		money = 0;
	}
		
	string name; 
	double cost; 
	double money;
	int count; 
		
	int displayChoices();
	double buyDrink(int c, Drink[]); 
	double inputMoney(double c); 
	int dailyReport(double m, int i); 
};

int Drink::displayChoices()
{
	int choice;
	cout << "\n" << setw(7) << "Item" << setw(16) << "Cost\n";
	cout << "1. Coca Cola      1.00\n";
	cout << "2. Root Beer      1.00\n";
	cout << "3. Orange Soda    1.00\n";
	cout << "4. Grape Soda     1.00\n";
	cout << "5. Bottled Water  1.50\n";
	cout << "\nEnter your beverage choice: ";
	cin >> choice;

	while (choice < 1 || choice > 5)
	{
		cout << "Please select items 1-5";
		cout << "\nRe-enter: ";
		cin >> choice;
	}
	return choice;
}

double Drink::buyDrink(int choice, Drink data[])
{
	string selection;
	switch (choice)
	{
	case 1:
		selection = "Coca Cola";
		if (data[0].count == 0)
		{ 
			cout << "\nSOLD OUT\n";
			return choice;
		}
		break;
	
	case 2:
		selection = "Root Beer";
		if (data[1].count == 0)
		{ 
			cout << "\nSOLD OUT\n";
			return choice;
		}
		break;
	
	case 3:
		selection = "Orange Soda";
		if (data[2].count == 0)
		{ 
			cout << "\nSOLD OUT\n";
			return choice;
		}
		break;
	
	case 4:
		selection = "Grape Soda";
		if (data[3].count == 0)
		{ 
			cout << "\nSOLD OUT\n";
			return choice;
		}
		break;
	
	case 5:
		selection = "Bottled Water";
		if (data[4].count == 0)
		{ 
			cout << "\nSOLD OUT\n";
			return choice;
		}
		break;
	}
	cout << "You selected " << selection << ".\n";

double change; 
	if (choice > 0 && choice < 5)
	{	
		cost = 1.00;
		change = inputMoney(cost);
		return change;
	}
	if (choice == 5)
	{	
		cost = 1.50;
		change = inputMoney(cost);
		return change;
	}
}

int Drink::dailyReport(double money, int count)
{
	if (money > 0)
	{
		cout << "*****Dispensing*****\n";
		cout << "Here is your beverage! Enjoy!\n";
		count--;
		return count--;
	}
	else
	{
		cout << "Come back next time!\n";
		return count;
	}
	
}

double Drink::inputMoney(double cost)
{	double money = 0;
	if (cost == 1.00)
	{	
		cout << "Please enter $" << fixed << setprecision(2) << cost << "\n";
		cin >> money;
			while (money < cost && money !=0)
			{ 
				cout << "Please enter the correct change.\n";
				cin >> money;
			}
		if (money > cost && money !=0)
		{	
			money = money - cost;
			cout << "Your change is $" << money << "\n";
		}
		else if (money == 0 && money < cost)
		{
			money = 0;
			cout << "Purchase canceled.\n";	
		}
		return cost;
	}

	else if (cost == 1.50)
	{	
		cout << "Please enter $" << fixed << setprecision(2) << cost << "\n";
		cin >> money;
			while (money < cost && money !=0)
		{ 
			cout << "Please enter the correct change.\n";
			cin >> money;
		}
		if (money > cost && money !=0)
		{	
			money = money - cost;
			cout << "Your change is $" << money << "\n";
		}
		else if (money == 0 && money < cost)
		{
			money = 0;
			cout << "Purchase canceled.\n";	
		}
		return cost;
	}
}





int main()
{
	Drink vending;
	int choice;
	int	continuing;
	double cost;
	double depositcola = 0;
	double depositroot = 0; 
	double depositorange = 0; 
	double depositgrape = 0;
	double depositwater = 0;
	const int DRINKS = 5;

	Drink data[DRINKS] = {Drink("Coca Cola", 1.00, 20), 
						  Drink("Root Beer", 1.00, 20),
						  Drink("Orange Soda", 1.00, 20),
						  Drink("Grape Soda", 1.00, 20),
						  Drink("Bottled Water", 1.50, 20)};
	cout << "Welcome to the Drink Machine!\n";
	cout << "Press 1 to enter, 0 to stop.";
	cin >> continuing;

	while (continuing != 0)
	{
		choice = vending.displayChoices();
		cost = vending.buyDrink(choice, data);

		switch (choice)
		{
		case 1:
			if (data[0].count >= 1)
				{data[0].count = vending.dailyReport(data[0].cost, data[0].count);
				depositcola+=1.0;
				cout <<"\n";
				cout << data[0].name << " Status:" <<endl;
				cout << "$" << depositcola << " deposited, ";
				cout << data[0].count <<" remain for purchase." << endl;}
			break;

		case 2:
			if (data[1].count > 0)
				{data[1].count = vending.dailyReport(data[1].cost, data[1].count);
			    depositroot+=1.0;
				cout <<"\n";
				cout << data[1].name << " Status:" <<endl;
				cout << "$" << depositroot << " deposited, ";
				cout << data[1].count <<" remain for purchase." << endl;}
			break;

		case 3:
			if (data[2].count > 0)
				data[2].count = vending.dailyReport(data[2].cost, data[2].count);
				depositorange+=1.0;
				cout <<"\n";
				cout << data[2].name << " Status:" <<endl;
				cout << "$" << depositorange << " deposited, ";
				cout << data[2].count <<" remain for purchase." << endl;
			break;

		case 4:
			if (data[3].count > 0)
				data[3].count = vending.dailyReport(data[3].cost, data[3].count);
				depositgrape+=1.0;
				cout <<"\n";
				cout << data[3].name << " Status:" <<endl;
				cout << "$" << depositgrape << " deposited, ";
				cout << data[3].count <<" remain for purchase." << endl;
			break;

		case 5:
			if (data[4].count > 0)
				data[4].count = vending.dailyReport(data[4].cost, data[4].count);
				depositwater+=1.5;	
				cout <<"\n";
				cout << data[4].name << " Status:" <<endl;
				cout << "$" << depositwater << " deposited, ";
				cout << data[4].count <<" remain for purchase." << endl;
			break;
		}
	cout << "\nIf you would like to make another purchase enter 1 to continue or 0 to stop.\n";
	cin >> continuing;
	}
	return 0;
}
Last edited on
id recommend using code tags and indentations, people will consider spending their time to help you
line 54 delete this ]
line 145 should be while (money < cost )
line 167 should be while (money < cost )
Last edited on
in lines 221 , 231 , 241 , 251 , 261 you are passing data [n].cost to "DailyReport" function , data [n].cost is something fixed (and always larger than 0) you should pass the input the customer enters to that function . Looks like you just forgot .

I suggest you return "money" from your "InputMoney" function and then use it in your main witch case as the first argument for DailyReport .
Chriscpp,
I tried your idea. That just gives me the output "Please enter the correct change."

Thank you very much for your time. :) :) :) :)
Ardeshir81,
I tried something like that, but it wasn't working. I get so stuck at passing back and forth.

If you have time to show me exactly what you mean, I would appreciate it.

Thank you very much for your time. :) :) :) :)
In line 155 after the Purchase is Canceled I returned cost as "-1" ;
So that means the customer aborted the purchase .
And in lines 222 232 242 252 262 (old 221 ... 261 , because I added one line above) I changed data [n].cost to cost , I mean the return statement of BuyDrink .
I came up with this :
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct Drink
{
	Drink(string n, double cs, int cn)
	{
		name = n;
		cost = cs;
		count = cn;
	}
	Drink()
	{
		name = " ";
		cost = 0;
		count = 0;
		money = 0;
	}

	string name;
	double cost;
	double money;
	int count;

	int displayChoices();
	double buyDrink(int c, Drink[]);
	double inputMoney(double c);
	int dailyReport(double m, int i);
};

int Drink::displayChoices()
{
	int choice;
	cout << "\n" << setw(7) << "Item" << setw(16) << "Cost\n";
	cout << "1. Coca Cola      1.00\n";
	cout << "2. Root Beer      1.00\n";
	cout << "3. Orange Soda    1.00\n";
	cout << "4. Grape Soda     1.00\n";
	cout << "5. Bottled Water  1.50\n";
	cout << "\nEnter your beverage choice: ";
	cin >> choice;

	while (choice < 1 || choice > 5)
	{
		cout << "Please select items 1-5";
		cout << "\nRe-enter: ";
		cin >> choice;
	}
	return choice;
}

double Drink::buyDrink(int choice, Drink data[])
{
	string selection;
	switch (choice)
	{
	case 1:
		selection = "Coca Cola";
		if (data[0].count == 0)
		{
			cout << "\nSOLD OUT\n";
			return choice;
		}
		break;

	case 2:
		selection = "Root Beer";
		if (data[1].count == 0)
		{
			cout << "\nSOLD OUT\n";
			return choice;
		}
		break;

	case 3:
		selection = "Orange Soda";
		if (data[2].count == 0)
		{
			cout << "\nSOLD OUT\n";
			return choice;
		}
		break;

	case 4:
		selection = "Grape Soda";
		if (data[3].count == 0)
		{
			cout << "\nSOLD OUT\n";
			return choice;
		}
		break;

	case 5:
		selection = "Bottled Water";
		if (data[4].count == 0)
		{
			cout << "\nSOLD OUT\n";
			return choice;
		}
		break;
	}
	cout << "You selected " << selection << ".\n";

double change;
	if (choice > 0 && choice < 5)
	{
		cost = 1.00;
		change = inputMoney(cost);
		return change;
	}
	if (choice == 5)
	{
		cost = 1.50;
		change = inputMoney(cost);
		return change;
	}
}

int Drink::dailyReport(double money, int count)
{
	if (money > 0)
	{
		cout << "*****Dispensing*****\n";
		cout << "Here is your beverage! Enjoy!\n";
		count--;
		return count--;
	}
	else
	{
		cout << "Come back next time!\n";
		return count;
	}

}

double Drink::inputMoney(double cost)
{	double money = 0;
	if (cost == 1.00)
	{
		cout << "Please enter $" << fixed << setprecision(2) << cost << "\n";
		cin >> money;
			while (money < cost && money !=0)
			{
				cout << "Please enter the correct change.\n";
				cin >> money;
			}
		if (money > cost && money !=0)
		{
			money = money - cost;
			cout << "Your change is $" << money << "\n";
		}
		else if (money == 0 && money < cost)
		{
			money = 0;
			cout << "Purchase canceled.\n";
			return -1 ;
		}
		return cost;
	}

	else if (cost == 1.50)
	{
		cout << "Please enter $" << fixed << setprecision(2) << cost << "\n";
		cin >> money;
			while (money < cost && money !=0)
		{
			cout << "Please enter the correct change.\n";
			cin >> money;
		}
		if (money > cost && money !=0)
		{
			money = money - cost;
			cout << "Your change is $" << money << "\n";
		}
		else if (money == 0 && money < cost)
		{
			money = 0;
			cout << "Purchase canceled.\n";
		}
		return cost;
	}
}





int main()
{
	Drink vending;
	int choice;
	int	continuing;
	double cost;
	double depositcola = 0;
	double depositroot = 0;
	double depositorange = 0;
	double depositgrape = 0;
	double depositwater = 0;
	const int DRINKS = 5;

	Drink data[DRINKS] = {Drink("Coca Cola", 1.00, 20),
						  Drink("Root Beer", 1.00, 20),
						  Drink("Orange Soda", 1.00, 20),
						  Drink("Grape Soda", 1.00, 20),
						  Drink("Bottled Water", 1.50, 20)};
	cout << "Welcome to the Drink Machine!\n";
	cout << "Press 1 to enter, 0 to stop.";
	cin >> continuing;

	while (continuing != 0)
	{
		choice = vending.displayChoices();
		cost = vending.buyDrink(choice, data);

		switch (choice)
		{
		case 1:
			if (data[0].count >= 1)
				{data[0].count = vending.dailyReport(cost, data[0].count);
				depositcola+=1.0;
				cout <<"\n";
				cout << data[0].name << " Status:" <<endl;
				cout << "$" << depositcola << " deposited, ";
				cout << data[0].count <<" remain for purchase." << endl;}
			break;

		case 2:
			if (data[1].count > 0)
				{data[1].count = vending.dailyReport(cost, data[1].count);
			    depositroot+=1.0;
				cout <<"\n";
				cout << data[1].name << " Status:" <<endl;
				cout << "$" << depositroot << " deposited, ";
				cout << data[1].count <<" remain for purchase." << endl;}
			break;

		case 3:
			if (data[2].count > 0)
				data[2].count = vending.dailyReport(cost, data[2].count);
				depositorange+=1.0;
				cout <<"\n";
				cout << data[2].name << " Status:" <<endl;
				cout << "$" << depositorange << " deposited, ";
				cout << data[2].count <<" remain for purchase." << endl;
			break;

		case 4:
			if (data[3].count > 0)
				data[3].count = vending.dailyReport(cost, data[3].count);
				depositgrape+=1.0;
				cout <<"\n";
				cout << data[3].name << " Status:" <<endl;
				cout << "$" << depositgrape << " deposited, ";
				cout << data[3].count <<" remain for purchase." << endl;
			break;

		case 5:
			if (data[4].count > 0)
				data[4].count = vending.dailyReport(cost, data[4].count);
				depositwater+=1.5;
				cout <<"\n";
				cout << data[4].name << " Status:" <<endl;
				cout << "$" << depositwater << " deposited, ";
				cout << data[4].count <<" remain for purchase." << endl;
			break;
		}
	cout << "\nIf you would like to make another purchase enter 1 to continue or 0 to stop.\n";
	cin >> continuing;
	}
	return 0;
}


If my code broke any other part of your program just try to fix it :)
+do comment your code .
If you dont look at your code for one/two week (s) and return to it you have a hard time understanding what you did .
It is better for you and for other people .
Thanks , just comment it .
I'm still hashing this out. Thank you SO MUCH!!
Topic archived. No new replies allowed.