Problem with an if-else program... ?

This is a discussion on Problem with an if-else program... ? within the C Programming forums, part of the General Programming Boards category; Here's the code I used: Code: #include<stdio.h> int main() { int age; printf( "Enter your age:\n" ); scanf( "%d", &age ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    16

    Smile Problem with an if-else program... ?

    Here's the code I used:

    Code:
    #include<stdio.h>
    
    int main()
    {
    int age;
    printf( "Enter your age:\n" );
    scanf( "%d", &age );
    if (age < 35 )
    {
    printf( "You are pretty young!\n" );
      }
    else if (age == 35)
    {
    printf( "You are old.\n" );
    }
    else
    {
    printf( "You are really old!\n" );
    }
    getchar();
    return 0;
    }
    So if I type 30 and hit return, I should instantly see "You are pretty young!" right?
    But that's not happening. If I exit to the code page and press Control + F9 once again, I can see "You are pretty young!" and "Enter your age:" in a new line. What's wrong?
    I'm a beginner... started only today... so please be nice and explain in simple words! Thanks!

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Try adding a second getchar() after the first; that should keep the console window open so you can see the result (the first getchar will be used when you hit return at the scanf).

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    16

    Smile

    Thank you, mike_g! It works!

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    You need to work on that indentation!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    16
    Oops!! Sorry, I will!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 10:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 01:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM

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