logical error entering while

jumperkid400 (23)
I am attempting to enter a while loop but for some reason my logic is off. It enters the loop exactly when i dont want it to. i want the range to be either 1,2 or 3.

1
2
3
4
5
6
7
8
9
  
cout << "What would you like the difficulty to be? " ;
cin >> difficulty;
while ((difficulty > 0 ))
{
      cout << "That is not a valid difficulty. Please enter a number 1-3.\n";
      cin >> difficulty;
}
James2250 (324)
If you wanted the number to be 1,2 or 3 I would expect something like this instead.

1
2
3
4
5
while ((difficulty <=0 || difficulty >= 4))
{
      cout << "That is not a valid difficulty. Please enter a number 1-3.\n";
      cin >> difficulty;
}

jumperkid400 (23)
see that confuses the crap out of me only because it seems backwards. I read that like this tell me if im wrong. difficulty is less than or equal to 0 OR difficulty is greater than or equal to 4... so the teh true value would be passed if difficulty was higher than 4 or less than 0
James2250 (324)
That while loop will run while difficulty is less than or equal to 0 and greater or equal to 4 as you said. The point of that is to keep the user entering in numbers inside the loop until it's between 0 and 4.

Once it's between 1-3 it will skip that loop and continue on to whatever is next.
jumperkid400 (23)
ahhh seee im dumb im so used to wanting to enter the loop with the values im useing i didnt think to do it the way i should have. thank you
Topic archived. No new replies allowed.