Beginner help with boolean operators.

This is a discussion on Beginner help with boolean operators. within the C++ Programming forums, part of the General Programming Boards category; Hello, I just started learning C++ following the online tutorials on this site. It's all gone very smooth until I ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Beginner help with boolean operators.

    Hello, I just started learning C++ following the online tutorials on this site. It's all gone very smooth until I tried using boolean operators. Following the tutorial I'm now at Cprogramming.com Tutorial: If Statements and I've written the following code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
        int a;
    
        cout << "Write either 1, 2, or 3: ";
        cin >> a;
    
        if ( a == (2 || 3) )
    
        {
            cout << "You entered 2 or 3.";
        }
    
        else
    
        {
            cout << "You entered 1.";
        }
    
        cin.get();
        cin.ignore();
    
        return 0;
    
    }
    The way I understood it I should be able to use "if ( a == ( 2 || 3 ) ) " to make it write out the if command if "a" gets the value 2 or 3, but as of now, if I enter 1 it writes the if command. If i enter any other value it writes the else command.
    Edit: Didn't quite preview enough.
    Last edited by Doughan; 11-18-2010 at 01:03 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    (2||3) evaluates to 1...

    a == 1 will be true only when you've entered 1 for the value of 'a'.

    You want to use:
    Code:
    if ( a == 2 || a == 3 )
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    Oh I see, quite obvious now that you pointed it out, just didn't know how to write it properly.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help on Boolean Operators?
    By yosimba2000 in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2009, 08:58 PM
  2. Boolean Operators...
    By Nean in forum C++ Programming
    Replies: 8
    Last Post: 10-06-2009, 02:31 AM
  3. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  4. C++ and Boolean Operators
    By Rockskin in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2006, 02:45 PM
  5. Boolean Operators?
    By civilwarsearch in forum C++ Programming
    Replies: 11
    Last Post: 12-15-2003, 08:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21