Hi folks,
So In my studying of C I am starting to come across more programs with functions in them. I am more comfortable with them than I used to be, but I have a couple of questions about this one function in a particular program from a book I'm reading, if you wouldn't mind taking a look. This particular program asks for two numbers, and calculates the sum of all the squares of the two numbers and the numbers in between them. I am confused about the get_int() function.
Code:
/* checking.c -- validating input */
#include <stdio.h>
#include <stdbool.h>
int get_int(void); // validate that input is an integer
bool bad_limits(int begin, int end, int low, int high); // validate that range limits are valid
double sum_squares(int a, int b); // calculate the sum of the squares of the integers a through b
int main(void)
{
const int MIN = -1000; // lower limit to range
const int MAX = +1000; // upper limit to range
int start; // start of range
int stop; // end of range
double answer;
printf("\nThis program computes the sum of the squares of "
"integers in a range.\n\nThe lower bound should not "
"be less than -1000 \nand the upper bound should not "
"be more than +1000.\n\nEnter the limits (enter 0 for "
"both limits to quit):\n\n\tlower limit: ");
start = get_int();
printf("\n\tupper limit: ");
stop = get_int();
while(start != 0 || stop != 0)
{
if(bad_limits(start, stop, MIN, MAX))
printf("\nPlease try again\n");
else
{
answer = sum_squares(start, stop);
printf("\nThe sum of the squares of the integers ");
printf("from %d to %d is %g\n", start, stop, answer);
}
printf("\nEnter the limits (enter 0 for both "
"limits to quit):\n");
printf("\n\tlower limit: ");
start = get_int();
printf("\n\tupper limit: ");
stop = get_int();
}
printf("\nDone.\n");
return 0;
}
int get_int(void)
{
int input;
char ch;
while(scanf("%d", &input) != 1)
{
while((ch = getchar()) != '\n'){
printf("\n");
putchar(ch); // dispose of bad input
}
printf(" is not an integer.\n\nPlease enter an ");
printf("integer value, such as 25, -178, 3, etc...: ");
}
return input;
}
double sum_squares(int a, int b)
{
double total = 0;
int i;
for(i = a; i <= b; i++)
total += i * i;
return total;
}
bool bad_limits(int begin, int end, int low, int high)
{
bool not_good = false;
if(begin > end)
{
printf("\n%d isn't smaller than %d.\n", begin, end);
not_good = true;
}
if(begin < low || end < low)
{
printf("\nValues must be %d or greater.\n", low);
not_good = true;
}
return not_good;
}
So in that get_int() function, it's pretty simple if scanf returns 1, otherwise... another while loop call the getchar() function, which is assigned to the variable ch. So say I enter 'a' for the lower limit, this is what happens:
Code:
Enter the limits (enter 0 for both limits to quit):
lower limit: a
a is not an integer.
It seems as if getchar() somehow "got the character" from scanf(). There's no way that getchar() could have been simultaneously reading my input, because that function is not even called until scanf has already not returned 1. So I'm confused how that happened. I was under the impression that getchar() got input from the keyboard, which would lead me to think that I would be prompted to input something again when getchar() is called, before putchar() prints the scanf input. But this is obviously not the case.
Also, I'm curious about that comment beside putchar(ch) that says //dispose of bad input. (The author of the book I'm reading put that there, not me). I recall a long time ago on these forums, someone told me something about how once scanf has read input, that input is like... stuck to scanf (I'm butchering this phrasing I'm sure). So does putchar() not only print the input from scanf/getchar, but also unload it from scanf, so to speak?
Forgive me if I'm explaining this stuff poorly, I probably am. As always, I really appreciate you guys & your help