Vending Machine

Hello,

I have to simulate a vending machine. I am showing an error here in user verification input. I want them to only enter 1-4. Also the quantity of beverages does not update. I have to enter it twice so it decreases to 19 and so forth

Thank you in advance.

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
 #include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//Function Prototypes
//int validateUserInput(int);
int displayDrinksAndGetUserChoice(const string[],
	const double[],
	const int[],
	int);
void updateBeverageQty(int, int[]);

int main()
{
	const int MAX_NUM_DRINKS = 4;
	string beverage[MAX_NUM_DRINKS] = {
		"Coca-Cola",
		"Sprite",
		"Gatorade",
		"Spring-Water"
	};
	double price[] = {
		0.75, // Coca-Cola
		0.65, // Sprite
		0.95, // Gatorade
		1.25 }; // Spring-Water
	int qtyOnHand[] = {
		20,  // Coca-Cola 
		20,	 // Sprite	
		20,  // Gatorade
		20 }; // Spring-Water
	int selectedDrink;
	char userAnswer;

	do
	{
		//selectedDrink = validateUserInput(userChoice);
		selectedDrink = displayDrinksAndGetUserChoice(beverage, price, qtyOnHand, MAX_NUM_DRINKS);

		switch (selectedDrink)
		{
		case 1:
			cout << "You selected: " << beverage[selectedDrink - 1] << endl;
			updateBeverageQty(selectedDrink - 1, qtyOnHand);
			break;
		case 2:
			cout << "You selected: " << beverage[selectedDrink - 1] << endl;
			updateBeverageQty(selectedDrink - 1, qtyOnHand);
			break;

		case 3:
			cout << "You selected: " << beverage[selectedDrink - 1] << endl;
			updateBeverageQty(selectedDrink - 1, qtyOnHand);
			break;

		case 4:
			cout << "You selected: " << beverage[selectedDrink - 1] << endl;
			updateBeverageQty(selectedDrink - 1, qtyOnHand);
			break;
		default:
			cout << "Please enter 1,2,3 or 4 only!" << endl;
		}
		cout << "Do you want to go again? (Y/n):";
		cin >> userAnswer;

	} 
	while ((userAnswer == 'Y') || (userAnswer == 'y'));
	
		system("PAUSE");
	return 0;
}

//Function to get user choice 
int displayDrinksAndGetUserChoice(const string drink[],
	const double price[],
	const int qty[],
	int arrSize)
{
	int userChoice=0;
	cout << "Welcome to COP 1334 Vending Machine" << endl;
	cout << "Please choose from one of the following refreshing options\n" << endl;
	cout << left;

	// HEADER
	cout << setw(23) << std::left << "-__DRINK NAME_________-"
		 << setw(8) << std::left << "-___PRICE"
		 << setw(12) << std::left << "-QUANTITY-"
		 << endl;

	for (int i = 0; i < arrSize; i++)
	{
		cout << setw(1) << std::left << i + 1
			<< setw(2) << std::left << ". "
			<< setw(23) << std::left << drink[i]
			<< setw(2) << std::left << "$"
			<< setw(8) << std::left << price[i]
			<< setw(12) << std::left << qty[i]
			<< endl;
	}
	cout << "\nYour choice: ";
	cin >> userChoice;
	{
		while ((!(cin >> userChoice) || ((userChoice < 5) && (userChoice < 1))))
			cin.clear();
			cout << "Wrong input. Please enter values between 1 and 4 only:" << endl;
			char ch;
			while (cin.get(ch) && ch != '\n');
		}
	
			return userChoice;
	

}
	


//Function to update beverage quantity
void updateBeverageQty(int index, int beverageQty[])
{
	beverageQty[index]--;
}
Last edited on
Hello Anahuac18,

Welcome to the forum.

When I tested your program I found the problem in the displayDrinksAndGetUserChoice" function. Line 104 gets user input and again in line 106 you get user input. You only need to do this once. I believe that in the while condition you wanted to check that status of "cin" not get input.

The while loop works much better like this:
1
2
3
4
5
6
7
8
9
while (!cin || (userChoice < 1 || userChoice > 4))  // <--- Changed
{
	cout << "Wrong input. Please enter values between 1 and 4 only:" << endl;
	cin.clear();
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. Clears input buffer.

	std::cout << "\nYour choice: ";
	std::cin >> userChoice;
}

The "!cin" will catch anything that is not a number and the other part will keep the numbers between 0 and 5.

The only other thing I can think of is in line 66 use either upper case or lower case letters, but not both. I would also consider something like this:
1
2
3
4
5
	cout << "Do you want to go again? (Y/N):";
	cin >> userAnswer;
	userAnswer = std::toupper(userAnswer);  // <--- Requires header file <cctype>.

} while (userAnswer == 'Y');


Hope that helps,

Andy
For entering 1-4, here's a way to simplify that while loop (only changed last part of function). Not sure which issue you have with the quantities.

Cross-platform notes:
- remove the #include "stdafx.h"
- prefer cin.get() or equivalent instead of system("pause")
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
//Function to get user choice 
int displayDrinksAndGetUserChoice(const string drink[],
	const double price[],
	const int qty[],
	int arrSize)
{
	int userChoice=0;
	cout << "Welcome to COP 1334 Vending Machine" << endl;
	cout << "Please choose from one of the following refreshing options\n" << endl;
	cout << left;

	// HEADER
	cout << setw(23) << std::left << "-__DRINK NAME_________-"
		 << setw(8) << std::left << "-___PRICE"
		 << setw(12) << std::left << "-QUANTITY-"
		 << endl;

	for (int i = 0; i < arrSize; i++)
	{
		cout << setw(1) << std::left << i + 1
			<< setw(2) << std::left << ". "
			<< setw(23) << std::left << drink[i]
			<< setw(2) << std::left << "$"
			<< setw(8) << std::left << price[i]
			<< setw(12) << std::left << qty[i]
			<< endl;
	}
	
	while (true)
	{
	    cout << "\nYour choice: ";
	    cin >> userChoice;
	    if (userChoice<1 || userChoice>4)
	    {
	        cout << "Wrong input. Please enter values between 1 and 4 only:" << endl;
	        continue;
	    }
	    return userChoice;
	}
}
Thank you both so much. =)
Handy Andy,

That did it. Thank you so much!
icy1,

Thank you I tried your input and it works too. I am working on Visual studios so I need the #include "stdafx.h"
Thank you soooo much
Topic archived. No new replies allowed.