Problem with OR operator


Hi

I am having trouble getting this or operator to work.
From what I can tell the line that uses the input asks:
"If the userkey is equal to 88 or is equal to 77 then print "Login" else
print "Invalid".

I have better results when I use letters, though for the life of me I cannot work out why this isn't working.

It is the Or operator and I feel I may be confused as to what it is actually doing.

Any help appreciated (I am using Code-Blocks).

Dan

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

using namespace std;

int main()
{

    int userkey = 0;

    cout << "Please enter the pass key: " << "\n";
    cin >> userkey;

    if (userkey == 88 || 77) // error here!
     {
     cout <<  "Login!" << endl;
    }
    else
    {
     cout << "Invalid!" << endl;

    }

    return 0;
}
if (userkey == 88 || userkey == 77)
Last edited on
the logical OR operator works like a function internally. It's syntactic sugar.
take this step-by-step.

if(userkey == 88 || 77)

relation operator == returns a boolean if( [true/false] || 77 )
the logical operator || checks to see if either or both of the operands are a nonzero value and returns true if so.

Even if the precedence for || was higher, you'd be looking at 88||77, which would be true no matter what.

Thanks Bassam300! and thanks Nexius! for clearing that up for me.

I see now that the || operator must have a "true/false" on both sides.
My attempt was giving only true/true.

Dan
Topic archived. No new replies allowed.