do while loop won't work.

I am taking a shot at the exercise in this book that I've been reading through. The instructions read "1. Rewrite the Menu Chooser program from this chapter using an enumeration to represent difficulty levels. The variable choice will still be of type int."

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
int choice;
do{
    enum difficulty {EASY,MEDIUM,HARD};

    cout << "Difficulty levels:\n\n" << endl;
	cout << "1 - Easy" << endl;
	cout << "2 - Medium" << endl;
	cout << "3 - Hard" << endl;
	
	cin >> choice;
	if(choice == 1)
	{
		cout << "You chose \"Easy\" " << endl; 
	}
	else if(choice == 2)
	{
		cout << "You chose \"Medium\" " << endl;
	}
	else if(choice == 3)
	{
		cout << "You chose \"Hard\" " << endl;
	}
	else
	{
		cout << "Nope!" << endl;
	}
}while(choice !=1 || choice !=2 || choice !=3);


Looking back now I realize that the original menu chooser program used a switch and not do while + if else statements. I suppose a switch would make more sense for the current usage but I can't understand why the do while loop will not exit upon entering a number that is not 1,2 or 3. I get the feeling that the answer is probably obvious but I can't grasp it for some reason.
You used an or statement testing all the possibilities of choice.

Your logic statement reads, continue if choice is not 1, or if choice is not 2 or if choice is not 3.

If you enter the number 1, the choice !=1 will evaluate to false, then choice !=2 is executed and it results to true, thus engaging the loop.

Ah yes, using & instead of or seems to have mostly fixed it. however if I type something that is not an integer then it turns into what I think is referred to as an infinite loop(it just keeps repeating endlessly). Any idea why it does that? I understand that "choice" is an int and therefore putting a non-int into it is kinda a nono but is there any explanation for why it loops infinitely after doing so?

Also can anyone tell me if it looks like I did the exercise right or more specifically if I am using enum in the correct manner?
cin is expecting an integer, inserting a character causes the cin to fail and choice remains unitialized.

http://www.cplusplus.com/forum/beginner/75776/
I followed the link you posted and came up with this as a fix:

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
int choice;
do{
    enum difficulty {EASY,MEDIUM,HARD};

    cout << "Difficulty levels:\n\n" << endl;
	cout << "1 - Easy" << endl;
	cout << "2 - Medium" << endl;
	cout << "3 - Hard" << endl;
	
	cin >> choice;
	if(!(cin>>choice))
	{
		cin.clear();
	    cin.get();
		continue;
	}
	else if(choice == 1)
	{
		cout << "You chose \"Easy\" " << endl; 
	}
	else if(choice == 2)
	{
		cout << "You chose \"Medium\" " << endl;
	}
	else if(choice == 3)
	{
		cout << "You chose \"Hard\" " << endl;
	}

	else
	{
		cout << "Nope!" << endl;
	}
}while(choice !=1 && choice !=2 && choice !=3);


But then I end up with the issue that it seems I get 2 inputs instead of just one(as it should be). If I enter a letter into the first input then it reruns the loop asking for a choice again(no problem there) but if I enter number it gives me a second input at which point it will act as it should but this second input is not needed or wanted(otherwise all is good).

I figured out the 2 input thing. I didn't realize that "if(!(cin>>choice))" causes it to call for an input. I had thought that it would just work as a modifier for the cin>>choice; that I already had there.

Still would like to know if I am using enum correctly(or rather if I'm even using it at all or if it is just sitting there like an unused variable).
Last edited on
Topic archived. No new replies allowed.