Do while won't stop doing while

Why isn't my while ending the loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string outerLoop()
{
   string currentSize;

   do
   {
      cout << "\nWhat size pizza would you like?\n\n";
      cout << " Press '0' for small\n Press '1' for medium\n";
      cout << " Press '2' for large\n Press 'Q' to quit\n\n";
      getline(cin, currentSize);
      
      if (currentSize == "0" || currentSize == "1" || currentSize == "2")
         return currentSize;
      else
         cout << "\n\nThat's not a valid selection! Please try again.\n";
   }
   while (currentSize != "q" || currentSize != "Q");

   return 0;
}
Last edited on
My suggestion would be to return a strinng variable like temp and make sure you check for that variable i main if you need to. it keeps on loop because it needs to gewt the values you entered in this case 0, 1,2. It will keep going until you type those values. if you want it to stop return a string variable after the cout << "\n\nThat's not a valid selection! Please try again.\n";, and it should work.
Your loop will keep loopin as long as the following is TRUE
(currentSize != "q" || currentSize != "Q");

This is an OR statement, so if either side is TRUE, then the whole thing comes out as TRUE. So for the loop to stop, we need both sides to come out as FALSE.

Let's take a look at the left side.
currentSize != "q" This will be FALSE only in one case, right? When currentSize is "q".

Let's take a look at the right side.
currentSize != "Q" This will be FALSE only in one case, right? When currentSize is "Q".

So for both sides to be FALSE, you need currentSize to be q, and also Q, at the same time. Is that possible? No.

Basically, you've written a condition that is true forever.
Topic archived. No new replies allowed.