broken while loop

I get a number from the user 1,2, or 3. I have a while loop checking to make sure the input was a 1,2, or 3. The input is then put in a switch statement. The problem is 1 and 2 work, but for some reason 3 doesn't, even though all three numbers codes are pretty much the same. After I put 3 in it acts like it's in a infinite loop, but after using break points it showed it wasn't. It gets out of the while loop and reads the cout after the while loop, but doesn't do it. I'm not sure whats missing.

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
  void Colors::gameLevel(){
   int level;
   
   cout << "What level difficulty do you what to play?\n" << "Easy(1), Normal(2), Hard(3): ";
   cin >> level;
   while(level != 1 && level != 2 && level != 3){
      cout << "That is not one of the difficulty levels.\n" << "Easy(1), Normal(2), Hard(3): ";
      cin >> level;
   }
   cout << "out of loop\n";
   switch(level){
      case 1:
         cout << "easy";
         kSize = 4;
         break;
      case 2:
         cout << "normal";
         kSize = 6;
         break;
      case 3:
         cout << "hard";
         kSize = 8;
         break;
   }
}
Last edited on
What exactly do you want this program to do? What do you mean it reads the cout after the while loop but doesn't do it? May I see the question for this code?
The while loop would be beter emplemented with less-than/greater-than operators:

while((level < 1) && (level > 3)){/* Your code*/}

Also, add a default to your switch statement:

1
2
3
4
5
6
7
8
9
switch(level)
{
    default:
    {
        cout<< "There ain't room enough for more than "<< max_level<< "level"<< ((max_level > 1) ? "s " : " ")<< 
                    "in these parts..."<< '\n';
    }
    break;
}
Last edited on
theres a cout on line 10 after the while loop. When I run the program and step through it with break points it does get out of the loop and reads the cout << "out of loop/n"; but does output it to the screen and continues on. This only happens when 3 is entered. I'm trying to figure out why…

The input must be exactly 1,2 or 3 so no less-than/greater-than operators
oh and what the program is suppose to do is set kSize to 4, 6, or 8
ah I'm sorry guys the problem is someplace else in my code. I just took out the while loop and ran it with no luck. :(
toast9: BECAUSE YOU DIDN'T USE PARENTHESIS!

while(level != 1 && level != 2 && level !=3) can be interprested differently by the compiler:

while(1 && level) -> false if level != true (which is implicitly converted to 1)

etc...
what? O_o
does what i have mean while level doesn't equal 1 and while level doesn't equal 2 and while level doesn't equal 3 keep looping?
well looks like the problem is using the number 8. I changed it to 6 and it works. I wonder why?
@IWishIKnew: stop yelling crap, check out operator precedence.

@toast9: my mind reading cat is quite old. Post a minimal snip that does reproduce your problem.
Also http://blog.codinghorror.com/rubber-duck-problem-solving/
@ne555:

I know, but it's still good to use parenthesis. It emphasizes what order that operations should be executed in. It also eliminates the possibility of a mistake.
Then nicely say so: ``I recommend you to use parenthesis to improve readability''.
you worded as if the condition was ill-defined.
Topic archived. No new replies allowed.