Home > Net >  while(decision1 != 1 || decision1 != 2) why does this keep repeating even though decision is already
while(decision1 != 1 || decision1 != 2) why does this keep repeating even though decision is already

Time:09-09

This is the code. Why does it keep repeating even though decision is already 1 or 2?

    std::cout << "How many toppings do you want? (1/2): ";
    std::cin >> decision1;
        
    while(decision1 != 1 || decision1 != 2)
    {
        std::cout << "Please enter either 1 or 2\n";
        std::cout << "How many toppings do you want? (1/2): ";
        std::cin >> decision1;
    }

CodePudding user response:

When decision1 == 1, the condition decision1 != 2 is true, and vice-versa.

Since decision1 cannot be both 1 and 2, decision1 != 1 || decision1 != 2 is always true.

CodePudding user response:

Change the while loop condition to decision1 != 1 && decision1 != 2. This means 'do it while decision1 is neither 1 or 2', and what you have now means 'do it while decision1 is different than 1 or different than 2', and that's why it's repeating: the value will always be either different than 1 or different than 2.

  •  Tags:  
  • c
  • Related