Thread: questions about if, or, not, and etc

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    questions about if, or, not, and etc

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	int ggg;
    	cout <<"enter a number\n";
    	cin>>ggg;
    	cin.ignore();
    	if (ggg == 776 && 777){
    		cout <<"you entered 776 or 777";
    	}
    	else {
    		cout <<"bah\n";
    	}
    	cin.get();
    }
    I think you get the idea of what im TRYING to do.
    now, taking this idea can someone show me how to use it correctly? also with or/not
    thanks! im a little confused while reading the tutorial

  2. #2
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    Well I assume you want to check if they enter 776 or 777, yes? Well you need to use the || (logical OR operator).

    EDIT: Also, think about how you've wrote your if statement. This is what it will do as you've wrote it:

    It will check if ggg equals 776, if this is true it will turn "ggg == 776" into 1. Now any other value other than 0 is true, so 777 turns into 1. So the if statement is:

    Code:
    if (1 && 1)
    {
        //1 && 1 = true so this will execute
    }
    Last edited by gin; 07-06-2008 at 03:27 PM.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    if ( ggg == 776 || ggg == 777)
    {
        cout <<"you entered 776 or 777";
    }
    "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

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    awesome thanks for the help

  5. #5
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    You should give this a read:
    http://www.cprogramming.com/tutorial/lesson2.html

    To better your understanding of conditional statements and conditional expressions. Once you understand the last part of that tutorial it gets really easy.

Popular pages Recent additions subscribe to a feed