MENU DRIVEN LOOP

Ok guys, this program runs and compiles. As long as you don't try to go backwards on the menu. I have been searching for 2 hours trying to figure out how to fix it.
Post the menu
get user input
do what 1 is supposed to do
repost menu and ask for input again
do what it says.... so on and so forth.
My problem is that when it asks for menu, it doesnt go back to start of program to validate from the start, it picks up right there and only goes down the code.
Please help...
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
// vending.cpp
// Name: Jeff Kirn
// Date: 9/11/14
// This program will act as a vending machine

#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
struct Candy
{
	
	double price;
	string name = "";
	int count;
};




void menu();
	
int main()
{
	double totalSales = 0;
	if (totalSales >0)
	{
		cout<<"So far your cost of candy is $"<<totalSales<<endl;
	}
	cout<<"Please press \n1:Snickers\t$1.00\n2:M&Ms\t\t$1.00\n3:Cheetos\t$1.50\n4:Exit"<<"\n"<<endl;
	Candy vend[3];
	int input = 0;
	double total = 0;
	double quant = 0;
	double dollars = 0;
	double tranPrice = 0;
	double sPrice = 1.00,
			mPrice = 1.00,
			cPrice = 1.50;
	double bills =0;
	double change = 0;
//	double totalSales = 0;
	
	vend[0].price = 1.00;
	vend[0].name = "Snickers";
	vend[0].count = 5;
	
//	cout<<vend[0].name<<endl;
//	cout<<vend[0].price<<"\t"<<endl;
//	cout<<vend[0].count<<"\t"<<endl;
	vend[1].name = "M&Ms";
	vend[1].price= 1.00;
	vend[1].count = 5;
	
	vend[2].name = "Cheetos";
	vend[2].price = 1.50;
	vend[2].count = 5;
	
	cin>>input;
//	menu(input);

	
	if (input == 1)
	{
		cout<<"You have chosen "<<vend[input-1].name<<endl;
		if( vend[input-1].count <= 0)
		{
			cout<<"Sold out"<<endl;
		}
		if( vend[input-1].count>=1)
		{
		
		cout<<"There are "<<vend[input-1].count<<" "<<vend[input-1].name<<" in the machine"<<endl;
		cout<<"How many would you like to buy? : ";
		cin>>quant;
		tranPrice = quant * sPrice;
	//	tranprice =  quant *vend[intput-1].price;
		cout<<"The price of "<<quant<<" "<< vend[input-1].name<<" is $"<<tranPrice<<endl;
		cout<<"How much money will you be putting in? : ";
		cin>>bills;
		change = bills - tranPrice;
		if (change<0)
		{
			cout<<"Not enough money"<<endl;
			cout<<"The price of "<<quant<<" "<< vend[input-1].name<<" is $"<<tranPrice<<endl;
			cout<<"How much money will you be putting in? : ";
			cin>>bills;
					{
			cout<<"Here is your snack"<<endl;
			totalSales = totalSales + tranPrice;
						vend[input-1].count -=  quant;
			cout<<vend[input-1].count<<endl;
		}
		}
		if(change>=0)
		{
			cout<<"Here is your snack"<<endl;
			totalSales = totalSales + tranPrice;
			vend[input-1].count -=  quant;
			cout<<vend[input-1].count;
		}
		}
		menu();
		//	cout<<"Please press \n1:Snickers\t$1.00\n2:M&Ms\t\t$1.00\n3:Cheetos\t$1.50\n4:Exit"<<"\n"<<endl;
			cin>>input;
		//	menu(input);
		//main();
	}
	
		if (input == 2)
	{
		cout<<"You have chosen "<<vend[input-1].name<<endl;
		if( vend[input-1].count <= 0)
		{
			cout<<"Sold out"<<endl;
		}
		if( vend[input-1].count>=1)
		{
		
		cout<<"There are "<<vend[input-1].count<<" "<<vend[input-1].name<<" in the machine"<<endl;
		cout<<"How many would you like to buy? : ";
		cin>>quant;
		tranPrice = quant * sPrice;
	//	tranprice =  quant *vend[intput-1].price;
		cout<<"The price of "<<quant<<" "<< vend[input-1].name<<" is $"<<tranPrice<<endl;
		cout<<"How much money will you be putting in? : ";
		cin>>bills;
		change = bills - tranPrice;
		if (change<0)
		{
			cout<<"Not enough money"<<endl;
			cout<<"The price of "<<quant<<" "<< vend[input-1].name<<" is $"<<tranPrice<<endl;
			cout<<"How much money will you be putting in? : ";
			cin>>bills;
					{
			cout<<"Here is your snack"<<endl;
			totalSales = totalSales + tranPrice;
						vend[input-1].count -=  quant;
			cout<<vend[input-1].count<<endl;
		}
		}
		if(change>=0)
		{
			cout<<"Here is your snack"<<endl;
			totalSales = totalSales + tranPrice;
			vend[input-1].count -=  quant;
			cout<<vend[input-1].count;
		}
		}
		menu();
		cin>>input;
		//	cout<<"Please press \n1:Snickers\t$1.00\n2:M&Ms\t\t$1.00\n3:Cheetos\t$1.50\n4:Exit"<<"\n"<<endl;
		//	cin>>input;
		//main();
	}
	
		if (input == 3)
	{
		cout<<"You have chosen "<<vend[input-1].name<<endl;
		if( vend[input-1].count <= 0)
		{
			cout<<"Sold out"<<endl;
		}
		if( vend[input-1].count>=1)
		{
		
		cout<<"There are "<<vend[input-1].count<<" "<<vend[input-1].name<<" in the machine"<<endl;
		cout<<"How many would you like to buy? : ";
		cin>>quant;
		tranPrice = quant * cPrice;
	//	tranprice =  quant *vend[intput-1].price;
		cout<<"The price of "<<quant<<" "<< vend[input-1].name<<" is $"<<tranPrice<<endl;
		cout<<"How much money will you be putting in? : ";
		cin>>bills;
		change = bills - tranPrice;
		if (change<0)
		{
			cout<<"Not enough money"<<endl;
			cout<<"The price of "<<quant<<" "<< vend[input-1].name<<" is $"<<tranPrice<<endl;
			cout<<"How much money will you be putting in? : ";
			cin>>bills;
					{
			cout<<"Here is your snack"<<endl;
			totalSales = totalSales + tranPrice;
			vend[input-1].count -=  quant;
			cout<<vend[input-1].count<<endl;
		}
		}
		if(change>=0)
		{
			cout<<"Here is your snack"<<endl;
			totalSales = totalSales + tranPrice;
					
			vend[input-1].count -=  quant;
			cout<<vend[input-1].count<<endl;
		}
		}
				menu();
		cin>>input;
		//	cout<<"Please press \n1:Snickers\t$1.00\n2:M&Ms\t\t$1.00\n3:Cheetos\t$1.50\n4:Exit"<<"\n"<<endl;
		//	cin>>input;
		//main();
	}
	if (input == 4)
	{
		cout<<"The machine has "<<vend[0].count<<" "<< vend[0].name<<" "<<vend[1].count<<" "<< vend[1].name<<" "<<vend[2].count<<" "<< vend[2].name<<" left inside"<<endl;
		cout<<"You spent $"<<totalSales;
	}
	

	return 0;
}
void menu()
{
		int input = 0;
		cout<<"Please press \n1:Snickers\t$1.00\n2:M&Ms\t\t$1.00\n3:Cheetos\t$1.50\n4:Exit"<<"\n"<<endl;
	//	cin>>input;
	//	return input;
}
I don't mean to be cryptic here, but this is exactly the sort of situation where a do-while loop would be useful. It would allow you to jump all the way back up to the top of your menu code without needing to resort to recursion (I see those commented-out main() calls).
http://www.cplusplus.com/doc/tutorial/control/#dowhile

You may need to rearrange some parts of your code near the top of main to properly use it.

-Albatross
Sounds good, I will give it a try, THANK YOU!!
Topic archived. No new replies allowed.