Chocolate Vending Machine

My group's chocolate vending machine code is clean, but has a problem.

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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>

using namespace std;

int main()
{
	// Variables from function "chocoSelect".
	int cChoice;
	double tPrice;
	int qAMB, qSD, qCS, qCMB;

	// Variables from function "insertCoin".
	double currentTotal;

	chocoSelect();

	insertCoin(tPrice);

	dispenser(qAMB, qSD, qCS, qCMB, currentTotal, cChoice);

	system ("pause");
	return 0;
}

double chocoSelect()
{
	system ("cls");

	int cChoice; // Your chocolate selection.
	int qAMB, qSD, qCS, qCMB; // Quantity of All Milk Bar, Sugar Drops, Caramel Surprise, and Chunky Milk Bar respectively.
	double cPrice; // Current price for all chocolates.
	double tPrice = 0.00; // Total price for all chocolates.

	cout << "**************************************" <<endl;
	cout << "            CHOCOLATE MENU            " <<endl;
	cout << "**************************************" <<endl;
	cout << "ITEM \t\t\t Price/each" <<endl;
	cout << "1. All Milk Bar \t $0.25" <<endl;
	cout << "2. Sugar Drops \t\t $0.35" <<endl;
	cout << "3. Caramel Surprise \t $0.50" <<endl;
	cout << "4. Chunky Milk Bar \t $0.75" <<endl<<endl;

	do
	{
	return0: 
	cout << "Make a selection: ";
	cin >> cChoice;

	switch (cChoice)
	{
	case 1: // Choosing "All Milk Bar"
		return1:
		cout << "How many All Milk Bar(s) would you like? (max 5): ";
		cin >> qAMB;

		if (qAMB > 5)
		{
			MoreThan5(); // Calling a function of having a customer entering more than 5.
			goto return1;
		}
		else if (qAMB < 1)
		{
			LessThan1(); // Calling a function of having a customer entering less than 1.
			goto return1;
		}
		else
		{
			cPrice = 0.25 * qAMB;
			tPrice = tPrice + cPrice;
		}

		break;

	case 2: // Choosing "Sugar Drops"
		return2:
		cout << "How many Sugar Drops would you like? (max 5): ";
		cin >> qSD;

		if (qSD > 5)
		{
			MoreThan5(); // Calling a function of having a customer entering more than 5.
			goto return2;
		}
		else if (qSD < 1)
		{
			LessThan1(); // Calling a function of having a customer entering less than 1.
			goto return2;
		}
		else
		{
			cPrice = 0.35 * qSD;
			tPrice = tPrice + cPrice;
		}
		
		break;

	case 3: // Choosing "Caramel Surprise"
		return3:
		cout << "How many Caramel Surprise(s) would you like? (max 5): ";
		cin >> qCS;

		if (qCS > 5)
		{
			MoreThan5(); // Calling a function of having a customer entering more than 5.
			goto return3;
		}
		else if (qCS < 1)
		{
			LessThan1(); // Calling a function of having a customer entering less than 1.
			goto return3;
		}
		else
		{
			cPrice = 0.50 * qCS;
			tPrice = tPrice + cPrice;
		}
		
		break;

	case 4: // Choosing "Chunky Milk Bar"
	return4:
		cout << "How many Chunky Milk Bar(s) would you like? (max 5): ";
		cin >> qCMB;

		if (qCMB > 5)
		{
			MoreThan5(); // Calling a function of having a customer entering more than 5.
			goto return4;
		}
		else if (qCMB < 1)
		{
			LessThan1(); // Calling a function of having a customer entering less than 1.
			goto return4;
		}
		else
		{
			cPrice = 0.75 * qCMB;
			tPrice = tPrice + cPrice;
		}
		
		break;

	default: // If a customer enters other than 1 to 4 will get an error!
		cout << "INVALID SELECTION! Please wait 10 seconds." <<endl;
		system ("pause");
		goto return0;
		break;
	}

	} while (cChoice < 1 && cChoice > 4);

	return tPrice;
	return qAMB, qSD, qCS, qCMB;
	return cChoice;
}

void MoreThan5() // Function for showing a customer entering more than 5.
{
	cout << "You've entered more than 5. Please enter 1 to 5." <<endl<<endl;
}

void LessThan1() // Function for showing a customer entering less than 1.
{
	cout << "You've entered less than 1. Please enter 1 to 5." <<endl<<endl;
}

double insertCoin(double tPrice)
{
	system ("cls");

	double purchase = 0.00; // Purchase price
	double currentTotal = 0.00; // Current total inserted
	double change; // Change given
	int choice; // Choose a coin

	purchase = chocoSelect();

	cout << "\nINSERT COINS" <<endl;
	cout << "1. 5 cents" <<endl;
	cout << "2. 10 cents" <<endl;
	cout << "3. 20 cents" <<endl;
	cout << "4. 50 cents" <<endl;
	cout << "5. 100 cents" <<endl<<endl;

	do
	{
	cout << "Choose a coin: ";
	cin >> choice;

		switch (choice)
		{
			case 1:
			currentTotal = currentTotal + 0.05;
			cout << "Current Total	: $" << currentTotal <<endl<<endl;
			break;

			case 2:
			currentTotal = currentTotal + 0.10;
			cout << "Current Total	: $" << currentTotal <<endl<<endl;
			break;

			case 3:
			currentTotal = currentTotal + 0.20;
			cout << "Current Total	: $" << currentTotal <<endl<<endl;
			break;

			case 4:
			currentTotal = currentTotal + 0.50;
			cout << "Current Total	: $" << currentTotal <<endl<<endl;
			break;

			case 5:
			currentTotal = currentTotal + 1;
			cout << "Current Total	: $" << currentTotal <<endl<<endl;
			break;

			default:
			cout << "Invalid choice!" <<endl<<endl;
		}

	} while (currentTotal < purchase);

	return currentTotal;
}

double dispenser(int qAMB, int qSD, int qCS, int qCMB, double currentTotal, int cChoice) // Function for dispensing chocolates from a vending machine
{
	int chocoChoice; // Chocolate selected from the function "chocoSelect".
	int rAMB, rSD, rCS, rCMB; // Remaining number of All Milk Bar(s), Sugar Drops, Caramel Surprise(s), and Chunky Milk Bar(s) dispensed respectively.
	double rTotal; // Remaining total after each item is dispensed.

	rAMB = chocoSelect();
	rSD = chocoSelect();
	rCS = chocoSelect();
	rCMB = chocoSelect();
	rTotal = chocoSelect();
	chocoChoice = chocoSelect();

	system ("cls");

	if (chocoChoice == 1)
	{
		cout << "Your " << rAMB << " All Milk Bar(s) is/are dispensed one at a time." <<endl;

		do
		{
			cout << "All Milk Bar number: " << rAMB <<endl;
			rAMB = rAMB - 1;
			rTotal = rTotal - 0.25;
			cout << "Remaining money: $" << rTotal <<endl;
		} while (rAMB > 0);
	}

	else if (chocoChoice == 2)
	{
		cout << "Your " << rSD << " Sugar Drops is/are dispensed one at a time." <<endl;

		do
		{
			cout << "Sugar Drops number: " << rAMB <<endl;
			rSD = rSD - 1;
			rTotal = rTotal - 0.35;
			cout << "Remaining money: $" << rTotal <<endl;
		} while (rSD > 0);
	}

	else if (chocoChoice == 3)
	{
		cout << "Your " << rCS << " Caramel Surprise(s) is/are dispensed one at a time." <<endl;

		do
		{
			cout << "Caramel Surprise number: " << rAMB <<endl;
			rCS = rCS - 1;
			rTotal = rTotal - 0.50;
			cout << "Remaining money: $" << rTotal <<endl;
		} while (rCS > 0);
	}

	else
	{
		cout << "Your " << rCMB << " Chunky Milk Bar(s) is/are dispensed one at a time." <<endl;

		do
		{
			cout << "Chunky Milk Bar number: " << rCMB <<endl;
			rCMB = rCMB - 1;
			rTotal = rTotal - 0.75;
			cout << "Remaining money: $" << rTotal <<endl;
		} while (rCMB > 0);
	}

	cout << "Thank you for using our machine." <<endl;
	cout << "You have $" << rTotal << "change to colect." <<endl;

	return 1;
}


The functions doesn't seem to link in proper.

Also, for the "chocoSelect" function, if a user enters a selection number other than 1 to 5, the machine will stall and wait for 10 seconds to try again. How?
The functions doesn't seem to link in proper.

What does that mean? If you have link errors, then TELL US WHAT THEY ARE. How can we help you if you don't tell us properly what the problem is?
Last edited on
You haven't included the header that declares system(), and you haven't declared a whole lot of functions that you're trying to use.

In C++, you have to declare a function before you use it. You used all these functions without declaring them:

double chocoSelect();
void MoreThan5();
void LessThan1();
double insertCoin(double tPrice);
double dispenser(int qAMB, int qSD, int qCS, int qCMB, double currentTotal, int cChoice);

int system(char *S);
tipaye, I've added the declared functions on top of int main(), but i got errors stating that uninitialized local variables are used. (see error code C4700 - visual studio)
All these goto statements in the code are horrible. I strongly suggest redoing this code when you get it working, without any goto statements in. You don't use anything from <cmath>, so no need to include that.

You should only be getting a warning for using uninitialized variables, not an error?! Sometimes you want to use them!
How to get a program to wait for 10 seconds if a user inputs a number that is outside the range?
Why are you passing all those parameters into your dispenser function? It doesn't use a single one of them!
@SpottyBlue, please show the error messages you're getting, and the new code (fragment) with the declarations you added.
@Mats, I've re-done the code and it's working properly. But the problem is only the first part is running twice before the second part. Here it is:

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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

void MoreThan5() // Function for showing a customer entering more than 5.
{
	cout << "You've entered more than 5. Please enter 1 to 5." <<endl<<endl;
}

void LessThan1() // Function for showing a customer entering less than 1.
{
	cout << "You've entered less than 1. Please enter 1 to 5." <<endl<<endl;
}

int ChocoSelect() // Function for selecting a chocolate.
{
	int cChoice; // Your chocolate selection.
	int qAMB, qSD, qCS, qCMB; // Quantity of All Milk Bar, Sugar Drops, Caramel Surprise, and Chunky Milk Bar respectively.	
	double tPrice; // Total price for all chocolates.

	system("cls");

	cout << "**************************************" <<endl;
	cout << "            CHOCOLATE MENU            " <<endl;
	cout << "**************************************" <<endl;
	cout << "ITEM \t\t\t Price/each" <<endl;
	cout << "1. All Milk Bar \t $0.25" <<endl;
	cout << "2. Sugar Drops \t\t $0.35" <<endl;
	cout << "3. Caramel Surprise \t $0.50" <<endl;
	cout << "4. Chunky Milk Bar \t $0.75" <<endl<<endl;

	do
	{ 
	cout << "Make a selection: ";
	cin >> cChoice;

	system ("cls");

	switch (cChoice)
	{
	case 1: // Choosing "All Milk Bar"
		do
		{
			cout << "How many All Milk Bar(s) would you like? (max 5): ";
			cin >> qAMB;

			if (qAMB > 5)
			{
				MoreThan5(); // Calling a function of having a customer entering more than 5.
			}
			else if (qAMB < 1)
			{
				LessThan1(); // Calling a function of having a customer entering less than 1.
			}
			else
			{
				tPrice = 0.25 * qAMB;
			}
		} while (qAMB < 1 && qAMB > 5);
		break;

	case 2: // Choosing "Sugar Drops"
		do
		{
			cout << "How many Sugar Drops would you like? (max 5): ";
			cin >> qSD;

			if (qSD > 5)
			{
				MoreThan5(); // Calling a function of having a customer entering more than 5.
			}
			else if (qSD < 1)
			{
				LessThan1(); // Calling a function of having a customer entering less than 1.
			}
			else
			{
				tPrice = 0.35 * qSD;
			}
		} while (qSD < 1 && qSD > 5);
		break;

	case 3: // Choosing "Caramel Surprise"
		do
		{
			cout << "How many Caramel Surprise(s) would you like? (max 5): ";
			cin >> qCS;

			if (qCS > 5)
			{
				MoreThan5(); // Calling a function of having a customer entering more than 5.
			}
			else if (qCS < 1)
			{
				LessThan1(); // Calling a function of having a customer entering less than 1.
			}
			else
			{
				tPrice = 0.50 * qCS;
			}
		} while (qCS < 1 && qCS > 5);
		break;

	case 4: // Choosing "Chunky Milk Bar"
		do
		{
			cout << "How many Chunky Milk Bar(s) would you like? (max 5): ";
			cin >> qCMB;

			if (qCMB > 5)
			{
				MoreThan5(); // Calling a function of having a customer entering more than 5.
			}
			else if (qCMB < 1)
			{
				LessThan1(); // Calling a function of having a customer entering less than 1.
			}
			else
			{
				tPrice = 0.75 * qCMB;
			}
		} while (qCMB < 1 && qCMB > 5);
		break;

	default: // If a customer enters other than 1 to 4 will get an error!
		cout << "INVALID SELECTION! Please wait 10 seconds." <<endl;
		system ("pause");
		break;
	}

	} while (cChoice < 1 && cChoice > 4);

	cout << "\nTotal price is: $" << tPrice <<endl;
	system("pause");

	return qAMB, qSD, qCS, qSD, cChoice;
	return tPrice;
}

double InsertCoin(double totalPrice)
{
	double currentTotal = 0; // Current total inserted
	int choice; // Choose a coin

	system("cls");

	cout << "\nINSERT COINS" <<endl;
	cout << "1. 5 cents" <<endl;
	cout << "2. 10 cents" <<endl;
	cout << "3. 20 cents" <<endl;
	cout << "4. 50 cents" <<endl;
	cout << "5. 100 cents" <<endl<<endl;

	do
	{
	cout << "Choose a coin: ";
	cin >> choice;

		switch (choice)
		{
			case 1:
			currentTotal = currentTotal + 0.05;
			cout << "Current Total	: $" << currentTotal <<endl<<endl;
			break;

			case 2:
			currentTotal = currentTotal + 0.10;
			cout << "Current Total	: $" << currentTotal <<endl<<endl;
			break;

			case 3:
			currentTotal = currentTotal + 0.20;
			cout << "Current Total	: $" << currentTotal <<endl<<endl;
			break;

			case 4:
			currentTotal = currentTotal + 0.50;
			cout << "Current Total	: $" << currentTotal <<endl<<endl;
			break;

			case 5:
			currentTotal = currentTotal + 1;
			cout << "Current Total	: $" << currentTotal <<endl<<endl;
			break;

			default:
			cout << "Invalid choice!" <<endl<<endl;
		}

	} while (currentTotal < totalPrice);

	return currentTotal;
}

double dispenser(int AMB, int SD, int CS, int CMB, int chocoChoice, double totalPrice, double cTotal)
{
	double change; // Change given

	change = cTotal - totalPrice;

	system ("cls");

	if (chocoChoice == 1)
	{
		cout << "Your 1. All Milk Bar(s) is/are dispensed one at a time." <<endl;
		cout << "All Milk Bar number: " << AMB <<endl;
		cout << "Remaining money: $" << change <<endl;
	}

	else if (chocoChoice == 2)
	{
		cout << "Your 2. Sugar Drops is/are dispensed one at a time." <<endl;
		cout << "Sugar Drops number: " << SD <<endl;
		cout << "Remaining money: $" << change <<endl;
	}

	else if (chocoChoice == 3)
	{
		cout << "Your 3. Caramel Surprise(s) is/are dispensed one at a time." <<endl;
		cout << "Caramel Surprise number: " << CS <<endl;
		cout << "Remaining money: $" << change <<endl;
	}

	else
	{
		cout << "Your 4. Chunky Milk Bar(s) is/are dispensed one at a time." <<endl;
		cout << "Chunky Milk Bar number: " << CMB <<endl;
		cout << "Remaining money: $" << change <<endl;
	}

	system("pause");
	system("cls");

	cout << "Thank you for using our machine." <<endl;
	cout << "You have $" << change << " change to colect." <<endl;

	return 1;
}


int main()
{
	int AMB = 0, SD = 0, CS = 0, CMB = 0;
	int chocoChoice = 0;
	double totalPrice = 0.00;
	double cTotal = 0.00;

	cout << "Welcome to the Chocolate Vending Machine." <<endl;
	system("pause");

	AMB, SD, CS, CMB, chocoChoice = ChocoSelect();
	totalPrice = ChocoSelect();
	cTotal = InsertCoin(totalPrice);
	dispenser(AMB, SD, CS, CMB, chocoChoice, totalPrice, cTotal);

	system("pause");
	return 0;
}
I see you still left in <cmath> for some reason. You also are not using anything from <iomanip> and <stdio>. It's senseless to bloat the program with these things when you're not using them!

I would also suggest replacing using namespace std; with:
1
2
3
using std::cout; 
using std::endl; 
using std::cin; 


system("dosomething"); calls should also be avoided. For instance, if I threw your code as is in my compiler, it wouldn't know what these calls were (and would fail).

As for you major problem, I think you want:
1
2
3
4
5
while (cChoice < 1 || cChoice > 4)

//In place of

while (cChoice < 1 && cChoice > 4)


You may only return one value from a function.

When you do return x, y, z;, the compiler evaluates and discards x and y, then evaluates and returns z. It is equivalent to:
1
2
3
    x ;
    y ;
    return z ;



By the same token, when the compiler encounters x, y, z = func(), it evaluates x and y and discards them, then sets z to the (single) value returned from func(). It is equivalent to:
1
2
3
    x ;
    y ;
    z = func();


See:
http://www.cplusplus.com/doc/tutorial/functions2/
http://stackoverflow.com/questions/54142/how-does-the-comma-operator-work

Last edited on
Why is your dispenser() function set to return a double, only to return 1?
Also, why are you calling it in main(), only to have its returned result discarded?
Topic archived. No new replies allowed.