Thread: Beginner Question about OR operator

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Beginner Question about OR operator

    Im having trouble getting this to work. Im not sure if Im using the OR operator right... since it will only output the "Wrong!" no matter what.

    Just assume the rest of the code is there.

    Code:
            cout << endl << "Now, what number am I thinking of from 1 to 10?" << endl;
            cin >> answer;
            if (answer == (6 || 9)) {
                cout << "Right!" << endl;
            }
            else if (answer == (5 || 7 || 8 || 10)) {
                cout << "Close! But you just missed it!" << endl;
            }
            else {
                cout << "Wrong" << endl;
            }
    What am I doing wrong?

    Im a beginner just playing around with C++

    Thank you for any help.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    23
    yes you are using it wrong, 6 || 9 equals true. If you are comparing answer to either 6 or 9 you need to do each comparison seperatly
    Code:
    ( answer == 6 ) || ( answer == 9 )
    look up operator precedence and operator associativity ( http://en.wikipedia.org/wiki/Operator_associativity, http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx )
    Last edited by pYro; 04-29-2011 at 08:54 PM.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Thank you very much Pyro!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Question
    By TKnight in forum C Programming
    Replies: 3
    Last Post: 11-09-2008, 07:16 AM
  2. Beginner Question
    By pobri19 in forum C Programming
    Replies: 5
    Last Post: 05-03-2008, 04:04 AM
  3. Beginner Question
    By LKH in forum C Programming
    Replies: 15
    Last Post: 04-28-2004, 09:45 PM
  4. a beginner question
    By xpflyer2002 in forum C Programming
    Replies: 6
    Last Post: 03-07-2004, 05:24 PM
  5. increment operator Question from a beginner
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:23 AM