Switch Structure Error-do while

Problem: In my switch structure inside the following function, the switch structure works perfectly when and IF I enter in the correct cases (a,b,c) for the first time. Although, on the other hand, if I enter in default value for the first attempt, suppose "e",so on the console it says
Incorrect!
Enter: 


So, I'm going to type in a valid input this time, suppose a.
enter: a


After typing "a", It's not processing any furthermore. It keeps saying

Incorrect!
Enter:
repeatedly and I'm even entering in the valid choices for those one. So can someone tell me what I hav epotentially done wrong in my following code? Thank you for taking your time to read this! Looking forward for guidance!
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
  void class_type(char &plane_class)
{
	//Decalrea a value of bolean to test the validtiy
	bool flag=false;
	//Ask the user what type of ticket they want
	cout<<"\n\n\tAmerican Airlines: What class of seat do you want: ";
	cout<<"\n\n\tA.FirstClass B.BusinessClass C. EconomyClass";
	
	cout<<"\n\n\tSelection: ";

	//Make a switch structure to get the correct answer
	do
	{
	cin.get(plane_class);
	cin.ignore(1000,'\n');

	//make place_class into lowerletter
	(char)tolower(plane_class);


	switch(plane_class)
		{
			case 'a':
				break;
			case 'b':
				break;
			case 'c':
				break;
			default:
				{
				cout<<"\n\t\tIncorrect";
				cout<<"\n\t\tEnter: ";
				flag=true;
				}
	
	
		}
	}
	while (flag);

}
Last edited on
well for one if a wrong input is entered the flag is set to true, but if the right input is entered there isn't a way to set it back to false to break out of the while loop.
Last edited on
I don't think the behaviour you've observed is as described.

Once the flag is set to true with the first answer, you're stuck in that do-while for ever, because you never unset flag.

Each time you give a "correct" answer after this, it processes it as normal, but you're still in the do-while, and you don't give any output in the correct cases. So you will keep seeing a blank screen, waiting for input and then looping back to wait for more input until you enter a "wrong" answer, when it will do some output, and then loop back to wait for more input.

Test this by putting some output in the "correct" cases, including a way out of your infinite loop.
I got it solved guys! forgot to set my flag=false inside my do while loop. Thanks for the guidance :)
Topic archived. No new replies allowed.