Switch Structures

Out of curiosity, suppose the user entered the wrong input under switch structure. If user entered wrong, how do I make him ask again?

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

char ch1, ch2;
	int number;
	cout<<"Type in first two letters: ";
	cin>>ch1>>ch2;

	switch(ch1)
	{
	case 'A':
	case 'a':
		cout<<"Incorrect input";
		break;
	case 'm':
	case 'M':
		cout<< "Math";
		number=math;
		break;
	case 's':
	case 'S':
		cout<< "science";
		number=science;
		break;
	
	}
Use a do-while loop that contains the test conditions you want to meet.

1
2
3
4
5
do 
{
    cout<<"Type in first two letters: ";
    cin>>ch1>>ch2;
}while(test condition);


But you may want to get someone more experienced to help as I am still learning myself.
also, instead of writing both the capital and lowercase version of the case, you can do switch (toupper(ch1)) it will recognize both instead of having to type it twice.

also remember to #include<cctype> to be able to use toupper!

as for your question, you can use an if else statement but I am not too sur
Topic archived. No new replies allowed.