Following along a C tutorial, I entered this code. When I compile and run, it aks for my age, which I enter. But it doesn't display any of the 'if' or 'else' statements. So I hit enter and the window closes. Is there a trick or am I entering the code wrong?

#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;
}

Recommended Answers

All 5 Replies

The window will close when the program is done. It's possible that the program is running correctly and displaying what you want, but is simply closing faster than your eye can register. Check out this link on how to hold the execution window open using both C and C++. There are similar links at Daniweb too.
http://www.dreamincode.net/forums/showtopic30581.htm

add getch() just before the return statement so that you can see the output. Otherwise your program works just great.

If you are using a Turbo C++ editor, then Alt+F5 will also let you see the result apart from putting getch();

Getch is nothing but a function which makes the control of the program wait till an interrupt is received from the keyboard....

abhikkumarbasu, it would really help if you'd read the threads you are responding in. He very definitely said he's using DevC++.

And I've seen other posts that you've offered the exact same advice as the post immediately before you -- and hours earlier.

We appreciate you desire to help and fully welcome it. Just try to be a little more observant so you can help people with the best advice.

add getch()

AD?!?! Really? Don't you mean getchar() ? :icon_wink:

commented: good catch :) +24


AD?!?! Really? Don't you mean getchar() ? :icon_wink:

Yes :icon_redface:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.