Need help with a simple code

Im doing an assignment that requires me to code a vending machine. Im currently stuck in a part where on the second time it loops, it doesnt proceed down to case 1 when input is 1, it just loops forever. Im a beginner, any help will be appreciated.

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
  
int main() {
	//Set counters & declare
	int coffee =10, milo = 10, tea =10,option;
	double totalN,n,returnChange;
	
	//Start loop of questions
	while(option!=4){
	cout<<"Welcome to Wendy's Machine\nPress 1 for Coffee, 2 For Tea, 3 For Milo and 4 to exit\n\nCoffee=RM1.20, Tea=RM1.00, Milo=RM1.50\n"
	"Please note this machine accepts only RM1, RM0.50, RM0.20 and RM0.10\n";
	cin>>option; 
	
	switch(option){
		case 1: if(coffee<1){
			cout<<"Sorry we are out of coffee\n";}	
			else{
			while(totalN<1.20){
			cout<<"Please insert cash\n";
			cin>>n;
			if(n==1 || n==0.5||n==0.2||n==0.1){
			totalN = totalN + n;
				if(totalN>1.20){
				returnChange = totalN - 1.20;
				cout<<"Thank you here is your change "<<returnChange<<endl;
				coffee--;}
				else{
				cout<<"Thank you\n";
				coffee--;}
			}
			else{
			cout<<"Invalid input\n"; 
			}
		
		}
			}
			
	}	
	
 			
}
return 0;
} 
while(totalN<1.20){

>>

1
2
totalN = 0;
while(totalN<1.20){


Wow, it took about ten mins from me to find = ="
morning dude.

while (option != 4){

at this point, 'option' is not initialised, it could be any value, so you need initialise it when you declare it on line 4. (you are initialising the other variables on this line, which is good).

Also, what compiler are you using? Your issue was picked up by my compiler when i tried to build (which is also a good thing):
Error	1	error C4700: uninitialized local variable 'option' used
Error	2	error C4700: uninitialized local variable 'totalN' used



You are also stuck in the wrong loop due to this line:
while (totalN<1.20){

if i put in 1RM, it thanks me (even though this isn't enough?). and then loops again as 1 is less that 1.2.
Last edited on
Ohmygod, it was that simple. Hahaha, okay i get it now. I didnt realize that option and totalN have to be declared 0 too. Im using DevC++
it worked with you guys' help. Thanks =D
Topic archived. No new replies allowed.