I was wanting to create a loop for data entry verification so that it loops only when a user enters a wrong number. In my code below, you can see that the while loop will only break with the numbers 1, 5, and 10. The code works correctly if I limit my condition to one number but will not work with more than one. Can anyone tell me why this is happening? I have not been able to find anything in the book I have been reading (An Intro to C++ by Diane Zak 6th ed.) or anywhere else on the web.
#include <iostream>
using namespace std;
int main ()
{
int number = 0;
cout << "Choose one of the following numbers: 1, 5, or 10: ";
cin >> number;
while (number != 1 || number != 5 || number != 10)
{
cout << "\nError: Choose ONLY one of the following numbers:1, 5, or 10: ";
cin >> number;
}
It looks like I needed to input && operators instead of || operators. The logic for the while loop as seen above creates an infinite loop. The while loop will always evaluate to true even though one of the conditions will evaluate to false. This is fixed by using && operators so that if one condition evaluates to false, the while loop evaluates to false and discontinues the loop.
while (number != 1 && number != 5 && number != 10)