Problems Starting C Programs -
i'm trying execute code below. however, in eclispe, program doesn't start until enter character , press enter. instance, if hit run, have enter number represent age before asks me enter age. wondering how fix this.
int main() { int age; /* need variable... */ printf( "please enter age" ); /* asks age */ scanf( "%d", &age ); /* input put in age */ if ( age < 100 ) { /* if age less 100 */ printf ("you pretty young!\n" ); /* show works... */ } else if ( age == 100 ) { /* use else show example */ printf( "you old\n" ); } else { printf( "you old\n" ); /* executed if no other statement */ } return 0; }
this pretty classic problem. output stream has not been flushed. happens after writing newline (which not doing). if don't want newline, issue hard flush on standard out:
printf( "please enter age" ); fflush( stdout ); scanf( "%d", &age );
while i'm here, can ask think commenting style? commenting every line of code excessive. can read code understands each line does, without needing comment. put comments on own line, , give overview of several lines of code. prefer see quick overview of i'm read know whether bother reading or not.
Comments
Post a Comment