Need help with Beginner exercise(#2)

When i run program,and type in what beverage do i want,it just terminates program
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
#include<iostream>

using namespace std;

char ch;

int main(){
	cout<<"Hello!Which beverage do you want?\n";
	cout<<"1.cola 2.pepsi 3.coke 4.beverage 5.water\n";
	cin>>ch;
	
	
	ch=30;
	cout<<"\n";
	
	
	switch(ch){
		case '1':
			cout<<"Here is your cola!";
			break;
		case '2':
			cout<<"Here is your pepsi!";
			break;
		case '3':
			cout<<"Here is your coke!";
			break;
		case '4':
			cout<<"Here is your beverage!";
			break;
		case '5':
			cout<<"Here is your water!";
			break;
	}
}


i've tried removing "break"s but it doesn' work!
Need help!
lol dude.

1
2
3
4
5
6
cout<<"Hello!Which beverage do you want?\n";
	cout<<"1.cola 2.pepsi 3.coke 4.beverage 5.water\n";
	cin>>ch;
	
	
	ch=30;


Whats the point of asking the user, if you're just gonna change it to 30 for no reason? remove line 13 and it should be fine.

Edit: oh and, move char ch; to main.
Last edited on
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
#include<iostream>

using namespace std;

int main(){
	cout<<"Hello! Which beverage do you want?" << endl;
	cout<< "1.cola 2.pepsi 3.coke 4.beverage 5.water" << endl;

	int choice;

	cin >> choice;
	
	
	switch(choice){
		case 1:
			cout<<"Here is your cola!";
			break;
		case 2:
			cout<<"Here is your pepsi!";
			break;
		case 3:
			cout<<"Here is your coke!";
			break;
		case 4:
			cout<<"Here is your beverage!";
			break;
		case 5:
			cout<<"Here is your water!";
			break;
	}
}
Last edited on
Topic archived. No new replies allowed.