Hello,
I am a mid-thirties networking professional (Cisco, Juniper et al.) and I have recently caught the bug to learn how to program above and beyond some very noddy bash scripts I've created in the past. Therefore please excuse my post if it's something simple, but I'm trying to undertand what I'm seeing.
I'm working through the CProgramming.com C Tutorial "Intro to C" section. In my attempt to use the concepts from the tutorial beyond the examples provided, I am trying to understand why I need two instances of the getchar() function within my exitprogram() function. Without two instances, the exitprogram() function returns back to main() and the user doesn't see the result from the inputfunction() calculation.
If I comment out the inputfunction() call within alive() then I need to press enter twice before the program exits.
I hope my explanation is clear and I have provided the program code below to refer to. Apologies for the insane level of commenting, however I figure if I get into a good practice now, whilst I may rationalise what I comment a little as I get more experience, from what I've read it's a good habit to get into.
Thanks.
Code:
/*
This program was written to show a basic set of C commands and syntax.
*/
/* Let's begin by bringing in the standard libraries we'll be using*/
#include <stdio.h>
#include <stdlib.h>
int alive(); /* Declare our function alive() */
int exitprogram(); /* Declare our function exitprogram() */
int inputfunction(); /* Declare our function inputfunction() */
int main() /* Function main() */
{ /* This is the start of our main() function */
alive(); /* Let's call our function alive() */
exitprogram(); /* Let's call our function exitprogram() */
return 0; /* Let's exit cleanly */
} /* This is the start of our main() function */
int alive() /* Function alive() */
{ /* This is the start of our alive() function*/
system("clear"); /* Clear the screen using a system command "clear" */
printf( "I am alive! Beware.\n\n\n" ); /* This is our main print statement */
inputfunction(); /* Let's call our function inputfunction() */
return 0; /* Let's exit cleanly */
} /* This is the end of our alive() function */
int exitprogram() /* Function exitprogram() */
{ /* This is the start of our exitprogram() function */
printf( "Press enter to exit.\n" ); /* Let's ask the user to do something */
getchar(); /* We'll wait for their input */
getchar(); /* We'll wait for their input */
system("clear"); /* Clear the screen using a system command "clear" */
printf( "Program exited successfully!\n\n\n" ); /* Give our user a nice friendly message */
return 0; /* Let's exit cleanly */
} /* This is the end of our exitprogram() function */
int inputfunction() /* Function inputfunction() */
{ /* This is the start of our inputfunction() function */
int variable_a, variable_b, variable_x; /* Declare our variables variable_a, variable_b and variable_x */
variable_a = 0; /* Let's zero our variable variable_a */
variable_b = 0; /* Let's zero our variable variable_b */
variable_x = 0; /* Let's zero our variable variable_x */
printf( "Please enter your first number: " ); /* Ask the user to enter a number */
scanf( "%d", &variable_a ); /* Store the user input into variable variable_a */
printf( "Please enter your second number: " ); /* Ask the user to enter a number */
scanf( "%d", &variable_b ); /* Store the user input into variable variable_b */
variable_x = variable_a + variable_b; /* Our variable variable_x the sum of variables variable_a and variable_b */
printf( "The sum of your numbers is %d!\n\n\n", variable_x ); /* Print our variable variable_x */
return 0; /* Let's exit cleanly */
} /* This is the end of our inputfunction() function */