what is wrong with my code?

Hi. I am just starting to learn C++ just as a hobby. I am working on a very basic program right now. I am wondering if anyone would tell me what is wrong with my while conditions. From what I have read, entering in || should make the loop run while neither gullible is 5 nor iteration is 10, but what happens is that neither condition is recognized. The loop continues forever.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

int main()
{
   int gullible;
   int iteration = 0;

   while (gullible != 5 || iteration != 10)
    {
        cout << endl << "Enter any number other than 5.\n";
        cin >> gullible;
        iteration++;
        cout << iteration << endl;

    }

    if (gullible = 5)
    {
        cout << endl << "You were not supposed to enter the number 5!\n";
    }

    else
    {
        cout << endl << "You are too patient.  You win.\n";
    }


    return 0;
}
Last edited on
|| is not the "nor" operator, it is the "or" operator. So you will continuing looping so long as either gullible is not 5 OR iteration is not 10. In other words, you will only leave the loop when both gullible is 5 and iteration is 10.

Also, on line 19 I think you meant to compare gullible to 5, not assign it. Use == instead.
Thanks for that. :)

For some reason my brain momentarily cannot wrap around the idea that it makes sense to use && there instead of ||, but I am sure I'll get used to it.
"Neither X nor Y" can be written with the || operator if you like:
!(X || Y)
This is equivalent to writing (as you suggested)
!X & !Y
It may help to look at the code from another angle:
This code uses an infinite loop (one which never terminates, the only way out is via the break; statement).
1
2
3
4
5
6
7
8
9
10
11
12
13
    while (true) // repeat forever
    {
        if (gullible == 5)
            break;
            
        if (iteration == 10)
            break;
            
        cout << endl << "Enter any number other than 5.\n";
        cin >> gullible;
        iteration++;
        cout << iteration << endl;
    }
There are two obstacles to overcome before reaching the main body of the code.
If either the test at line 3 OR the test at line 6 is true, then the loop will terminate.
And looking at it the converse way, both line 3 AND line 6 must be false, in order to execute the body of the code.


Or to express the same code yet another way, this time using the not-equal condition,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    while (true) // repeat forever
    {
        if (gullible != 5) 
        {
            if (iteration != 10) 
            {
                cout << endl << "Enter any number other than 5.\n";
                cin >> gullible;
                iteration++;
                cout << iteration << endl;
            }
            else
            {
                break;
            }
        }
        else
        {
            break;
        }
    } 
This time, in order to reach line 7 both line 3 AND line 5 must be true. If either condition is false, the corresponding else statement is executed and the loop terminates.
Topic archived. No new replies allowed.