After a review of on-line resources and a review of the tutorial material, I don't entirely understand what getchar () is meant to do. I am only a few sections into the tutorial, so I'm not expecting to really... The way I'm using it is to hold my output screen, as mentioned by the tutorial. My XP ver 5.1 SP3 OS is shutting down output screens after programs execute faster than I can review. So as instructed by the tutorial, I am forced to use this command.
That said, I copied the Cprogramming tutorial (Else IF Section) found here:
If Statements in C - Cprogramming.com
In my version of the code, I had to insert more getchar () commands to hold the window:
Code:
#include <stdio.h>
int main()
/* Code to work out how the if statement functions*/
{
int age;
printf( "Please enter your age: " );
scanf( "%d", &age );
if (age < 100){
printf("You are pretty young! \n");
getchar();
}
else if (age==100) {
printf("You are pretty old! \n" );
getchar();
}
else
printf( "You are really old! \n");
getchar();
getchar();
return 0;
}
If I remove the getchar() at the if, else if, else statements, or the final position... or any combination thereof, the compiled code will not hold the window open. I'm not sure why I need to write this seemingly redundant code to hold the window. Seems like I should be able to hold the window with each conditional statement and no final getchar... Any help would be appreciated.