Trouble with my 'while' something is 'not true' do something code. please help

Hi all. 1 week into c++ learning loops. I’m trying to put something into a loop that says - whilst the answer is not 1 or 2, enter humber again. This is what I’ve done. It’s not working. keeps repeating loop even if conditions are met. What have I missed? Cheers!!
std::cout << “select 1 or 2\n\n”;
std::cin >> answer;

while (answer !=1 || answer !=2) {
std::cout <<
" you must enter 1 or 2\n\n"; answer =0; /// keeps getting stuck here even when I’ve entered 1 or 2???
std::cin >> answer;}

1 Like

Logical or || is true if even one of the operands is true.

Your loop condition is:

while (answer != 1 || answer != 2) {

Suppose you enter 1, then answer != 1 is false, but answer != 2 is true. Therefore, (false || true) is true.
Suppose you enter 2, then answer != 1 is true and answer != 2 is false. Therefore, (true || false) is true.
If you enter a number other than 1 or 2, then you will have (true || true) which is true.
There is no number which will make both operands of your loop condition false i.e. no value of answer will lead to (false || false).
So, you will end up in an infinite loop in each scenario.


Logical and && is true only if both operands are true.

If you write your condition as:

while (answer != 1 && answer != 2) {

If you enter 1, then answer != 1 is false and answer != 2 is true. Therefore, (false && true) is false and you exit the while loop.
If you enter 2, then answer != 1 is true and answer != 2 is false. Therefore, (true && false) is false and you exit the while loop.
If you enter a number other than 1 or 2, then you will have (true && true) which is true and the loop will not be exited.
&& gives you the desired behavior i.e. loop will terminate if answer is 1 or 2. If answer is some number other than 1 or 2, then the loop will not terminate.

1 Like

Thanks! It’s a so a (false) or a (true) would be a true. (pours a drink…)

So what I want to articulate - for some context. Multiple choice- I need user to input 1 or 2. Any other numbers or digits or what ever are not right. So If answer == 1 or 2 the program can move on. Is your solution above able to do that?
Gareth

@mtrtmk just tried your suggestion and it worked . Still not 100% on the logic so going to draw flow chart tomorrow to help visualise it.

Thanks again.
Gareth

1 Like

Another way to think about the condition is in terms of negation. You want the user to enter 1 or 2. If a number other than 1 or 2 is entered, then you want to keep prompting until the correct choice is made. In other words: if it is NOT the case that answer is 1 or 2, then keep looping and prompting.
The condition can be written as:

while (!(answer == 1 || answer == 2)) {

The above condition is equivalent to:

while (answer != 1 && answer != 2)) {

The temptation may be to distribute the negation like so:

while (!(answer == 1 || answer == 2)) {

while (answer != 1 || answer != 2) { // Not equivalent to above

while (answer != 1 && answer != 2)) { // Correct

Basically, just negating the operands doesn’t necessarily negate the original expression.

Instead, De Morgan’s Laws are used. You may want to search for De Morgan’s Laws and their use in the context of programming (different languages will have different syntax but the underlying boolean logic is not language-specific). e.g. the following articles are about JavaScript and Ruby, but the underlying logic is applicable to C++ as well: