Thread: If_Else Problem

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    3

    Cool If_Else Problem

    /
    I'm a newbie and a senor citizen and to keep my mind alert I have decided to learn C++. However the following may be very simple for most of you, at least I thought it should be simple.

    The following piece of code will not compile. Any hints would be greatly appreciated.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int A, B;
    	
        A = 6;
        B = 7;
    
        If( A == B )
        {
                 cout << "A is equal to B";
    	
         else
                 cout << "A is NOT equal to B";
         }	
         return 0;
    }
    Last edited by SeekerOfWisdom; 01-03-2006 at 11:04 PM.

  2. #2
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    You need cin.get(); above return 0; to keep the console from closing once it has displayed what it has to display. Otherwise you could run in from the command prompt.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int A, B;
    	
        A = 6;
        B = 7;
    
        if( A == B )    // lower case 'if'
        {
                 cout << "A is equal to B";
        }
         else
        {
                 cout << "A is NOT equal to B";
        }	
         return 0;
    }
    I've amended your spelling of 'if'. C++ keywords are case-sensitive. I've aso added a couple of braces for clarity.
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    /
    Thank you. I suspected it would be something simple. But I should have known better than to make the "I" in "if" upper case.

    SeekerOfWisdom.
    \

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I've aso added a couple of braces for clarity.
    Those braces are needed not just for clarity, but to get the program to compile. One statement follows the if, or a block of code inside braces. If you have an else, that should follow the statement or block of code, so the closing brace for the if block must come before the else. Similarly, the opening and closing braces for the else block should come after the else.

    Don't forget to post exact compiler errors you get when something isn't compiling.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM