Like Tree1Likes
  • 1 Post By laserlight

need "if" help

This is a discussion on need "if" help within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a program that asks for a number, and if you give it the right number, ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    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 !!
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    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
    20,975
    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.
    AndrewHunter likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07: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, 06:59 AM

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