Whats wrong with my while loop?

Pages: 12
Oct 24, 2012 at 8:55am
I am doing a simple menu and also checking for errors.

Meaning my menu has only 4 options. If user enters 5, or 6, or A b C d. It will return "An invalid option choosen, please enter again.

Whenever I enter a correct number, it just goes into an infinite loop showing, "You have choosen 1". How do I resolve this? And when I input an invalid option, it will request me to input the correct option again but how do I re-direct it to the correct case rather than it showing to exit Quincy 2005..

This is my set of codes
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
class Test
{
	public:
		void mainMenu();
		int menuChoice;
};

void Test::mainMenu()
{
	cout<<"Welcome!"<<endl<<endl;
	cout<<"1)	test1"<<endl;
	cout<<"2)	test2"<<endl;
	cout<<"3)	test3"<<endl;
	cout<<"4)	test4"<<endl<<endl;
}

int main()
{
	int menuChoice;
	
	Test test;
	test.mainMenu();
	cout<<"Please enter your choice : "<<endl;
	cin>>menuChoice;
	
	while (menuChoice == 1 || menuChoice == 2 || menuChoice == 3 || menuChoice == 4)
	{
		switch(menuChoice)
		{
			case 1:
			{
				cout<<"You choosen 1"<<endl;
			
			}
			case 2:
			{
				cout<<"You choosen 2"<<endl;
				
			}
			
			case 3:
			{
				cout<<"You choosen 3"<<endl;
				
			}
			
			case 4:
			{
				cout<<"You choosen 4"<<endl;
				
			}
		}
	}
	
	cout<<"Invalid option choosen, please enter your choice again : "<<endl;
	cin>>menuChoice;
	return 0;
}
Oct 24, 2012 at 9:08am
use break; statement in each cases
Oct 24, 2012 at 9:25am
Still getting the same infinite loop when I did that.
Oct 24, 2012 at 9:31am
I added break; at the end of my switch. The loop ends but as for the checking of my options. When I choose 1, it will show
"you have choose 1"
"you have choosen 2"
"you have choosen 3"
... and also
"Invalid option choosen..."

How do I resolve this for error checking?

When I enter 1, I want to only show You have choosen 1.
And if I enter a wrong option like, 5 or A B C. I want it to show Invalid option choosem please enter your choice again..

How do I re-direct it to choose the main menu again and then go towards the while loop again?
Oct 24, 2012 at 9:36am
declare a bool variable in main and set it to false
bool found=false;

Change your while condition to
while((menuChoice == 1 || menuChoice == 2 || menuChoice == 3 || menuChoice == 4) && found==false)

then each case include
found=true; before break;

if you want to use the invalid option to be shown then rewrite your while condition to while (found==false)
and include default statement in the switch case to print error message like this
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
while (!found==true)
	{
		cout<<"Please enter your choice : "<<endl;
		cin>>menuChoice;
		switch(menuChoice)
		{
			case 1:
			{
				cout<<"You choosen 1"<<endl;
				found=true;
				break;
			
			}
			case 2:
			{
				cout<<"You choosen 2"<<endl;
				found=true;
				break;
			}
			
			case 3:
			{
				cout<<"You choosen 3"<<endl;
				found=true;
				break;
			}
			
			case 4:
			{
				cout<<"You choosen 4"<<endl;
				found=true;
				break;
			}
			default:
				cout<<"Invalid option choosen"<<endl;
		}
	}
Last edited on Oct 24, 2012 at 9:41am
Oct 24, 2012 at 9:43am
Use a default case in the switch, then you won't need the while loop at all. You will need a while in conjunction with the bool variable mentioned by vichu8888, to ask for valid input again.
Oct 24, 2012 at 9:49am
Thanks for your help! I successfully stopped the looping but another problem is found.

If i input an invalid choice (i.e 5/a/b/c) It goes straight to invalid choice, enter again, and when I enter a correct option, it goes straight to "Hit enter to exit quincy".
Any way to solve this?

And also when I input choice 1, it is correct, it shows "You have choosen 1" But the message, "Invalid choice comes up too".

Do i have to put an else statement?

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
class Test
{
	public:
		void mainMenu();
		int menuChoice;
};

void Test::mainMenu()
{
	cout<<"Welcome!"<<endl<<endl;
	cout<<"1)	test1"<<endl;
	cout<<"2)	test2"<<endl;
	cout<<"3)	test3"<<endl;
	cout<<"4)	test4"<<endl<<endl;
}

int main()
{
	int menuChoice;
	
        bool found=false;
	Test test;
	test.mainMenu();
	cout<<"Please enter your choice : "<<endl;
	cin>>menuChoice;
	
	while (menuChoice == 1 || menuChoice == 2 || menuChoice == 3 || menuChoice == 4 && found==true)
	{
		switch(menuChoice)
		{
			case 1:
			{
				cout<<"You choosen 1"<<endl;
			        found=true;
			}break;
			case 2:
			{
				cout<<"You choosen 2"<<endl;
				found=true;
			}break;
			
			case 3:
			{
				cout<<"You choosen 3"<<endl;
				found=true;
			}break;
			
			case 4:
			{
				cout<<"You choosen 4"<<endl;
				found=true;
			}break;
		}
	}
	
	cout<<"Invalid option choosen, please enter your choice again : "<<endl;
	cin>>menuChoice;
	return 0;
}
Oct 24, 2012 at 9:54am
You haven't tried the default case in the switch.

The purpose of a default: clause in a switch is to catch all other values - that is the values that don't match any of the other cases. For this reason, it is often used to do error processing. So this is where you put the invalid option code.
Oct 24, 2012 at 9:54am
use proper brackets for while condition, include break inside each case, include default statement for switch and insert the error statements 56 and 57 in it.
Oct 24, 2012 at 9:55am
The while condition you have isn't scalable - what if you had 15 menu options? Are you going to have while condition with 15 OR conditions?
Oct 24, 2012 at 10:35am
I have done this is another way with the default: clause you guys mentioned.

Everything seems to be fine but I would wish that when I have choosen 1, the interface of test.mainMenu() doesn't show up again. I want it to stop at You have choosen 1 because I will be making it into a option and also letting people input datas into this option.

How do I make it so that the default menu will not appear 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
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
class Test
{
	public:
		void mainMenu();
		int menuChoice;
};

void Test::mainMenu()
{
	cout<<"Welcome to Test program!"<<endl<<endl;
	cout<<"1)	1"<<endl;
	cout<<"2)	2"<<endl;
	cout<<"3)	3"<<endl;
	cout<<"4)	4"<<endl<<endl;
}

int main()
{
	int menuChoice;
	
	bool repeat=false;
	Test test;

	
	do
	{
		test.mainMenu();
		cout<<"Please enter your choice : ";
		cin>>menuChoice;

		switch(menuChoice)
		{
			case 1:
				cout<<"You choosen 1"<<endl;
				repeat=false;
				break;
			case 2:
				cout<<"You choosen 2"<<endl;
				repeat=false;
				break;	   	   	 
			case 3:
				cout<<"You choosen 3"<<endl;
				repeat=false;
				break;	 	 	 	 	 
			case 4:
				cout<<"You choosen 4"<<endl;
				repeat=false;
				break;	  	   	   	   	   
			default:
				cout<<"Invalid entry!"<<endl;
				repeat=true;
				break;
		}
	}
	while (menuChoice != 1 || menuChoice != 2 || menuChoice != 3 || menuChoice != 4 && repeat == true);
}
Oct 24, 2012 at 10:37am
put test.mainMenu(); outside(above) the do..while loop
Oct 24, 2012 at 10:44am
Thank god for this forum and all your help. Especially vichu888!
Oct 24, 2012 at 10:59am
you are welcome. But there is a logical error in your problem. How you end your program?
Oct 24, 2012 at 11:21am
You still have your long condition in the while part of the do loop - it isn't necessary, that is the whole idea of the default clause in the switch.
Oct 24, 2012 at 11:34am
@TheIdeasMan . Does while condition make any sense to you?
I could understand his while condition part.
Suppose user enters 1, why it execute again. Because when input is 1 the repeat becomes false right? But the program works as expected.
How come this?
Can you explain whats going on in while condition?
Oct 24, 2012 at 11:37am
@b1b2b3b4
What does your program actually wants to do?
Does it want to loop again only if the user enters wrong input or
it should repeat as long as the user wish?
Oct 24, 2012 at 11:54am
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
char menuChoice; // char not int, so we can have a quit option

bool repeat=false;
bool Quit = false;

Test test;

while ( repeat == true || Quit == false) {
		test.mainMenu();

		cout<<"Please enter your choice : ";
		cin>>menuChoice;

		switch(menuChoice) {
			case '1':
				cout<<"You choosen 1"<<endl;
				break;
			case '2':
				cout<<"You chose 2"<<endl;
				break;	   	   	 
			case '3':
				cout<<"You chose 3"<<endl;
				break;	 	 	 	 	 
			case '4':
				cout<<"You chose 4"<<endl;
				break;
                        case 'Q':
                                 cout<<"You have chosen to Quit"<<endl;
                                 Quit = true; //program continues after the end of the while
                                                   //could use exit(0) function to quit altogether
                                 break;	  	   	   	   	   
			default:
				cout<<"Invalid entry!"<<endl;
				repeat=true;
				break;
		}
}



I haven't tested this, but it is what I was thinking of with the comments I made earlier.

Can you all see how this can be easily extended?
Oct 24, 2012 at 11:58am
now its like
easy peasy lemon squeezy :)
Oct 24, 2012 at 12:08pm
Yes, I think a major aim should be to create elegant solutions, where ever you can.
Pages: 12