Thread: Newbie Help

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    Newbie Help

    Please bear with me, for I just started learning C today.

    I'm to the second lesson for C coding, I understand what's mostly going on, but this is bugging me. Are the {} brackets supposed to be at a certain point when you're converting over to an "else" statement?
    Code:
    #include <stdio.h>
    
    int main()
    {
        int age;
        
        printf( "Please enter your age" );
        scanf( "%d", &age );
        if ( age < 100 ) {
        printf ( "Your are pretty young!\n" )
        }      /*I'm getting an error on this spot when I try to compile.*/
        else if ( age == 100 ) {
           printf( "You are old\n" ?;
        }
        else {
          printf( "You are really old\n" );
        }
          return 0;
    }

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Code:
    #include <stdio.h>
    
    int main()
    {
        int age;
        
        printf( "Please enter your age" );
        scanf( "%d", &age );
    
        if ( age < 100 )
        {
            printf ( "You are pretty young!\n");
        }  
        else if ( age == 100 )
        {
            printf( "You are old\n");
        }
        else
        {
            printf( "You are really old\n" );
        }
    
        return 0;
    }
    Errors corrected above in blue, I have also made the indenting more consistent.

    A { and } are needed whenever you have more than one statement after an if or else, otherwise only the first statement will be associated with it. It is a good habit to always use braces in an if, even if there's only one statement, so that if you decide to add another, you won't make the mistake of forgetting the { and }.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    2
    Okay, thanks alot. I don't know why I put a question mark there.

    Darn those semi-colons.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM