Thread: Boolean prob

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    6

    Boolean prob

    Hi, I'm quite new to cpp programming, and I made this small script using Dev-C++.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    int x;
        
        cout<<"Enter 1 or 0: "; 
        cin>>x;
        cin.ignore();
        if ( x = ( 1 || 0 ) ) {
             cout<<"Hello World";
             } 
        else if ( x = ( 1 && 0 ) ) {
             cout<<"World Hello";
             }
        cin.get();
    }
    Problem here is, if I enter 0, it should be printing World Hello instead of Hello World, which doesn't happens. Ofcourse I could easily solve this by adding a NOT operator, but I want to know about the AND and OR operators in specific. Could someone help me out?

  2. #2
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37
    You're first if statement is checking to see if the value of x is EITHER a 1 or 0. If it is EITHER of those values, then it prints Hello World.

    In a literal sense, your second if statement is saying if the value of x is BOTH 1 and 0, then it prints World Hello.

    You would want something like this:
    Code:
    if(x = 1)
    	 cout << "Hello World";
    else
    	 cout << "World Hello";

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    Quote Originally Posted by xmltorrent
    You're first if statement is checking to see if the value of x is EITHER a 1 or 0. If it is EITHER of those values, then it prints Hello World.

    In a literal sense, your second if statement is saying if the value of x is BOTH 1 and 0, then it prints World Hello.

    You would want something like this:
    Code:
    if(x = 1)
    	 cout << "Hello World";
    else
    	 cout << "World Hello";
    Aha! Ofcourse I understand what was in you code, but I was just playing around with the booleans. Thank you.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You're first if statement is checking to see if the value of x is EITHER a 1 or 0. If it is EITHER of those values, then it prints Hello World.

    In a literal sense, your second if statement is saying if the value of x is BOTH 1 and 0, then it prints World Hello.
    Not exactly.

    Code:
    if ( x = ( 1 || 0 ) )
    1 || 0 always evaluates to a true, which you then assign to x, automatically making it execute.

    Code:
    else if ( x = ( 1 && 0 ) )
    1 && 0 evaluates to a false, which you then again assign to x, and making it not execute.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    couple of problems:

    >> if ( x = ( 1 || 0 ) )

    first off, your assigning a value to x, use == for comparison. the above expression would then evaluate to:

    if(x == 1)

    since (0 || 1) == 1

    so the correct expression would be:

    if (x == 1 || x == 0)
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    That solved the problem, assigned them to x, instead of checking them, stupid me. Tnx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  2. Deal or No Deal listbox prob
    By kryptkat in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2009, 06:53 PM
  3. Casting boolean as string
    By doofusboy in forum C Programming
    Replies: 11
    Last Post: 11-10-2005, 12:24 PM
  4. Working with boolean...
    By CompiledMonkey in forum C Programming
    Replies: 4
    Last Post: 11-03-2003, 10:39 AM
  5. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM