Switch Case w/Loop problem..please help

I'm fairly new to programming, here's a simple program I wrote and maybe eventually build an actual game from..maybe...with a lot of work..

anyway the issue is when the INPUT becomes a char type, I experience a runtime error where the "do" code infinitely loops when i need it to loop once and wait for the correct input...

#include <iostream>
using namespace std;

void single() {
cout<<"Playing Single Player Mode\n";
cout<<"\n";}

void multi() {
cout<<"Playing Multi Player Mode\n";
cout<<"\n";}

void options() {
cout<<"Options\n";
cout<<"\n";}

void exit() {
cout<<"Exiting Game\n";
cout<<"\n";}

int main()
{

int input;
char input1;

do {
cout<<"************ Welcome to my Test Program ************\n";

cout<<"1. Single Player\n";
cout<<"2. Multi Player\n";
cout<<"3. Options\n";
cout<<"4. Exit\n";
cout<<"Selection: ";
cin>>input;
cin.ignore();
if(!(input==1||input==2||input==3||input==4)) {
cout<<"Invalid input, please try again\n";
cout<<"Selection: \n";
cout<<"\n";}
switch(input) {
case 1:
single();
break;
case 2:
multi();
break;
case 3:
options();
break;
case 4:
exit();
break;
}
}
while(!(input==1||input==2||input==3||input==4));


return 0;
}
Last edited on
closed account (10oTURfi)
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
#include <iostream>
using namespace std;

void single() {
cout<<"Playing Single Player Mode\n";
cout<<"\n";}

void multi() {
cout<<"Playing Multi Player Mode\n";
cout<<"\n";}

void options() {
cout<<"Options\n";
cout<<"\n";}

void exit() {
cout<<"Exiting Game\n";
cout<<"\n";}

int main()
{

char input; //Changed your int input to char input
//char input1; useless

do {
cout<<"************ Welcome to my Test Program ************\n";

cout<<"1. Single Player\n";
cout<<"2. Multi Player\n";
cout<<"3. Options\n";
cout<<"4. Exit\n";
cout<<"Selection: ";
cin>>input;
cin.ignore();
if(!(input=='1'||input=='2'||input=='3'||input=='4')) {
cout<<"Invalid input, please try again\n";
cout<<"Selection: \n";
cout<<"\n";}
switch(input) {
case '1':
single();
break;
case '2':
multi();
break;
case '3':
options();
break;
case '4':
exit();
break;
}
}
while(!(input=='1'||input=='2'||input=='3'||input=='4'));


return 0;
}


If you want input to be char, you must use single quotes (look above)


And please use [code][/code]
Last edited on
hey thanks for the reply..i ran the code you posted but same issue when i input a letter
Topic archived. No new replies allowed.