Looping

I want to loop this switch case when the user entered an invalid input, what should I do?

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


using namespace std;
int main()


{
	char choice;
	
	cout<<"Periodic Table of Elements: A Simple C++ Game"<<endl;
	cout<<"Choose the difficulty of the game:Please enter the corresponding letter of your choice:"<<endl<<'\n'<<"A. Beginner"<<endl<<"B. Intermmediate"<<endl<<"C. Expert"<<endl;
	cout<<"Your choice:"; cin>>choice;
	
	

	switch (choice)
	{
		case 'A':
			cout<<"You chose the Beginner level"<<endl;
			break;
		case 'B':
			cout<<"You chose the Intermmediate level"<<endl;
			break;	
		case 'C':
			cout<<"You chose the Expert level"<<endl;
			break;
		default:
			cout<<"Invalid input"<<endl;			
	}
	
return 0;
	
	
}
	
}
If you want to let the user know, you must use IF-block.
Ex:
1
2
3
4
5
if (choice == A || choice == B || choice == C){}
else {
cout<<"incorrect entry. Please try again.";
return(0);
}

But that will end the program I dont know if it will work for you...
Last edited on
Hello Fortem,

You could use:

1
2
3
4
5
6
do
{
    switch(choice)
    {
    }
} while (choice != 'A' && choice != 'B' && choice != 'C');


Hope that helps,

Andy
Topic archived. No new replies allowed.