vending machine - selection issue

Good Evening,
Everything seems to work fine with my program, unless a selection is made other than 1 through 6. I have tried using nested loops and a do/while loop, but then the program does not go through the entire process. Any advice/guidance would be most appreciated, thanking you in advance.

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
struct Drink
{
string drinkName;
double drinkCost;
int numOfDrinks;
};
// Function prototype(s)
int getChoice(Drink D[]); // Get D = drink of choice
void transaction(Drink D[], int choice, double& price);
// Start of program
int main()
{
Drink D[5] ={{"Cola ",.75,1},{"Root Beer",.75,20},{"Lemon-Lime",.75,20},{"Grape Soda",.80,20},{"Cream Soda",.80,20}};
double moneyMachineEarned = 0.0;
int choice;
while(true)
{
choice = getChoice(D);
if(choice == 6) break;
transaction(D, choice-1, moneyMachineEarned);
}
cout << endl << "The total amount of money the machined earned is " << moneyMachineEarned << endl << endl;
system("pause");
return 0;
}
int getChoice(Drink D[]) // Get D = drink of choice
{
int choice;
cout << endl << endl;
cout << "************** List of Drink Choices ***********************" << endl;
cout << "Drink Name Cost Number of Drinks in machine " << endl;
cout << "************************************************************" << endl;
for(int index = 0; index < 5; index++)
// Display drink choices, cost per drink, and number of drinks available
cout << (index + 1) << ". "
<< D[index].drinkName << "\t\t"
<< D[index].drinkCost << "\t" << setprecision(2) << fixed
<< D[index].numOfDrinks << endl;
cout << (6) << ". " << "Quit" << endl << endl;
cout <<"Please enter the number for your drink of choice, or enter (6) to quit: ";
cin >> choice;
return choice;
}
void transaction(Drink D[], int choice, double& price)
{
if(D[choice].numOfDrinks == 0)
{
cout << endl;
cout <<"That drink is sold-out! Please select again, or enter (6) to quit: ";
return;
}
double amount;
cout << endl << "Please enter the amount of money deposited for your drink selection: $ ";
cin >> amount;
cout << endl;
while(amount < 0 || amount > 1)
{
cout << "Amount entered was invalid. Please enter an amount between $0.00 and $1.00: $ ";
cin >> amount;
cout << endl;
}
while(amount < D[choice].drinkCost)
{
cout << "Amount entered was insufficient. Please enter an amount to purchase drink: $ ";
cin >> amount;
cout << endl;
}
if(amount > D[choice].drinkCost)
cout << "Thank you, your change is: $ " << (amount - D[choice].drinkCost) << endl;
D[choice].numOfDrinks--;
price = price + D[choice].drinkCost;
}
The simplest change would be this:

if(choice >= 6) break; // Note: >=
The classic idiom for this is a do/while loop.
1
2
3
4
5
    do
    {   cout <<"Please enter the number for your drink of choice, or enter (6) to quit: ";
        cin >> choice;
    } while (choice < 1 || choice > 6);        
    return choice;  // choice can only be a valid value 


Line 24: One comment on calling transaction(). You already know the specific drink chosen.
Why not just pass the drink entry from the array rather than the entire array and the choice? This would require passing the entry by reference, if you've learned that.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Thank you very much for your replies, coder777 and AbstractionAnon.

Also, I will use the <> formatting button in the future.

Thank you again.
This is my final code, all tests passed, I appreciate you, your time and your guidance.

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

using namespace std;

struct Drink
{
	string drinkName;
	double drinkCost;
	int numOfDrinks;
};

int getChoice(Drink D[]);  // Get D = drink of choice
void transaction(Drink D[], int choice, double& price);

int main ()
{
	Drink D[5] ={{"Cola     ",.75,1},{"Root Beer",.75,20},{"Lemon-Lime",.75,20},{"Grape Soda",.80,20},{"Cream Soda",.80,20}};
	
	double moneyMachineEarned = 0.0;
	int choice;
	
	while (true)
	{
		choice = getChoice (D);
		if (choice == 6) break;
			if (choice > 6 or choice < 1)
			{
			    cout << endl;
			   	cout << "Invalid selection!  Please try again." << endl << endl;
			}
		else 
		{
		transaction (D, choice-1, moneyMachineEarned);
		}
	}
	
	cout << endl;
	cout << "The total amount of money the machined earned is " << moneyMachineEarned << endl << endl;
	
	system ("pause");
	
	return 0;
}

int getChoice (Drink D[])  // Get D = drink of choice
{
	int choice;
	cout << endl << endl;
	cout << "************** List of Drink Choices ***********************" << endl;
	cout << "Drink Name              Cost    Number of Drinks in machine " << endl;
	cout << "************************************************************" << endl;
	
	for (int index = 0; index < 5; index++)
	
	// Display drink choices, cost per drink, and number of drinks available
	cout << (index + 1) << ". "
	<< D [index].drinkName << "\t\t"
	<< D [index].drinkCost << "\t" << setprecision(2) << fixed
	<< D [index].numOfDrinks << endl;
	
	cout << (6) << ". " << "Quit" << endl << endl;
	cout << "Please enter the number for your drink of choice, or enter (6) to quit: ";
	cin >> choice;
	return choice;
}

void transaction (Drink D[], int choice, double& price)
{
	if (D[choice].numOfDrinks == 0)
	{
		cout << endl;
		cout << "That drink is sold-out!  Please select again, or enter (6) to quit: ";
		return;
	}
	
	double amount;
	
	cout << endl;
	cout << "Please enter the amount of money deposited for your drink selection: $ ";
	cin >> amount;
	cout << endl;
	
	while (amount < 0 || amount > 1)
	{
		cout << "Amount entered was invalid.  Please enter an amount between $0.00 and $1.00:  $ ";
		cin >> amount;
		cout << endl;
	}
	
	while (amount < D[choice].drinkCost)
	{
		cout << "Amount entered was insufficient.  Please enter an amount to purchase drink:  $ ";
		cin >> amount;
		cout << endl;
	}
	
	if (amount >= D[choice].drinkCost)
	cout << "Thank you, your change is: $ " << (amount - D [choice].drinkCost) << endl;
	D [choice].numOfDrinks--;
	price = price + D [choice].drinkCost;
}
Topic archived. No new replies allowed.