Vending Machine Money Validation

Hello,

I am working on a vending Machine simulator. I am missing the payment part.

What I need is (using bool) a function that will ensure the user can enter only Nickels$ 0.05Dimes$ 0.10Quarters$ 0.25Dollar Bills$ 1.0. If they are short the machine has to ask for more money or give change if they pay more.
My code won't run here as I am using MS Visual Studio.
If anyone can please guide me. I have tried several ways but nothing works. I can't paste the whole code either because it is too long. I hope the part I pasted is able to run.

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
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
  #include <iostream>
#include <string>
#include <iomanip>
#include <climits>
using namespace std;

//Function Prototypes
int displayDrinksAndGetUserChoice(const string[], const double[], const int[], int);
int displayOPerationsAndGetUserChoice();
void updateBeverageQty(int, int[]);
void updateSoldBeverageQty(int, int[]);
void ComputeTotalProfit(const double[], int[], double[], int);
void DisplayTotalProfit(const string[], const double[], const int[], double[], int);
int getBestPerformer(const double[], const int);
int getWorstPerformer(const double[], int);
void displayBestDrink(const string[], const double[], const int[], double[], int);
void displayWorstDrink(const string[], const double[], const int[], double[], int);

int main()
{
	const int MAX_NUM_DRINKS = 4;
	int operation;
	int index = 0;


	string beverage[MAX_NUM_DRINKS] = { "Coca-Cola", "Sprite" , "Gatorade" , "Spring-Water" };
	double price[MAX_NUM_DRINKS] = { /* Coca-Cola*/1.50, /*Sprite*/1.25, /*Gatorade*/2.25, /*Spring-Water*/ 1.85 };
	int qtyOnHand[MAX_NUM_DRINKS] = {/* Coca-Cola*/20, /*Sprite*/20, /*Gatorade*/20, /*Spring-Water*/20 };
	int qtySold[MAX_NUM_DRINKS] = { 0,0,0,0 };
	double totalProfit[MAX_NUM_DRINKS];

	int selectedDrink;
	char userAnswer;

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

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

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

		case 4:
			cout << "You selected: " << beverage[selectedDrink - 1] << endl;
			updateBeverageQty(selectedDrink - 1, qtyOnHand);
			updateSoldBeverageQty(selectedDrink - 1, qtySold);
			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'));

	do
	{

		operation = displayOPerationsAndGetUserChoice();
		switch (operation)
		{
		case 1:
			//calculate total profit
			ComputeTotalProfit(price, qtySold, totalProfit, MAX_NUM_DRINKS);
			DisplayTotalProfit(beverage, price, qtySold, totalProfit, MAX_NUM_DRINKS);
			break;
		case 2:
			//Best
			ComputeTotalProfit(price, qtySold, totalProfit, MAX_NUM_DRINKS);
			index = getBestPerformer(totalProfit, MAX_NUM_DRINKS);
			displayBestDrink(beverage, price, qtySold, totalProfit, index);
			break;
		case 3:
			//worst
			ComputeTotalProfit(price, qtySold, totalProfit, MAX_NUM_DRINKS);
			index = getWorstPerformer(totalProfit, MAX_NUM_DRINKS);
			displayWorstDrink(beverage, price, qtySold, totalProfit, index);
			break;
		case 4:
			//Totals
			ComputeTotalProfit(price, qtySold, totalProfit, MAX_NUM_DRINKS);
			DisplayTotalProfit(beverage, price, qtySold, totalProfit, MAX_NUM_DRINKS);
			break;
		case 5:
		default:
			cout << "For operations please enter 1, 2, 3, or 4 only, key in 5 to exit" << endl;
		}
	} while (operation != 5);


	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 < 1 || userChoice > 4))
	{
		cout << "Wrong input. Please enter values between 1 and 4 only:" << endl;
		cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		std::cout << "\nYour choice: ";
		std::cin >> userChoice;
	}

	return userChoice;


}


//Function to make sure the user enters only valid money
/*double isValidMoney()
//bool isValidMoney = true;
//double(isValidMoney == true);

{
	//double moneyAmount = 0;


	//if (isValidMoney == Nickle || isValidMoney == Dime || isValidMoney == Quarter || isValidMoney == dollar)
		//return true;
	
}*/



Last edited on
I added this

//Function Money handler
int moneyHandler(bool, price [], int coin)
{
int coin, total_coin, change;
bool good;

if(good)
{
total_coin+=coin;
cout<<"you have added"<< total_coin< "cents"<<endl;
cout<<" You still need to enter" << result=price[selectedDrink -1] - total_coin << "cents" <<endl;
return result;
}
else
{
cout<< "Not a valid amount, for it to be accepted, please enter only 0.05 or 0.10 or 0.25 or 1.00"<< endl;
} while (total_coin < price[selectedDrink -1]);
cout << "Enjoy you Drink"
<< "change" << change=total_coin - price[selectedDrink -1]
<< "Enjoy your cold Drink"<<endl;
if (change > 0)
{
cout<< "Your change is:"<< change <<endl;
return change;
}
}
Hello Anahuac18,

It has taken me awhile to comment out all the functions that are not available yet. And the " moneyHandler" function is not properly defined and needs some work. I do not believe it is needed yet, so we can put that off for later.

I did add choice "5. Exit" to the menu. Right now it just exits the program later you can redirect the program elsewhere.

In the second do/while loop you switch on "operation", but "operation" does not have a value before it is used because I had to comment the function call that is not there. That is when I really noticed that you did not initialize your variables when they were defined. You should ALWAYS initialize variables when they are defined. This gave me an error trying to use an uninitialized variable.

The first appears to be working correctly, but I do not have the other functions to test them.

Hope that helps,

Andy

thank you so much for your input. I will continue making those corrections.
Topic archived. No new replies allowed.