Thread: need "if" help

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    need "if" help

    I am trying to make a program that asks for a number, and if you give it the right number, it will give you a message telling you that it is either correct or incorrect.
    Here's the code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a;
        cout<<"Enter a number: ";
        cin>>a;
        cin.ignore();
        if(a==15){
            cout<<"Correct!\n";
        if(a!=15){
            cout<<"Incorrect!\n";
    
        cin.get();
    
        }
    
    }
    Can anyone tell me if I did something wrong or if I missed anything? I am new to cpp, so I'm kind of clueless. Thanks.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Can anyone tell me if I did something wrong or if I missed anything?
    What does your compiler have to say on the topic?

    These kinds of simple mistakes are easily caught by them.

    Also, inferring from your code, CONGRATULATIONS ....for using a _good_ compiler, not a prehistoric one !!

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Where are your closing braces "}" for your if statements?

    You could also use an if/else instead of the two separate if statements.

    Code:
    if(a==15){
            cout<<"Correct!\n";
    }
    else {    
            cout<<"Incorrect!\n";
    }
    Jim

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    jimblumberg;1048692]Where are your closing braces "}" for your if statements?

    You could also use an if/else instead of the two separate if statements.

    hey thanks, it worked!

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a;
        cout<<"Enter a number: ";
        cin>>a;
        cin.ignore();
        if(a==15){
            cout<<"Correct!\n";
        if(a!=15){
            cout<<"Incorrect!\n";
    
        cin.get();
    
        }
    
    }
    i believe your problem is "if(a!=15)"

    should have 2 "==" to look for equality and not assigning.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by twistedforever
    i believe your problem is "if(a!=15)"

    should have 2 "==" to look for equality and not assigning.
    Except that != is not an assignment operator. You probably should read more of the replies before replying.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  2. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM