int GetNum() {
char buffer[3];
size_t size_read;
int i = 0;
printf("Enter a value (number) for the node between 1-99:\n");
_cgets_s(buffer, _countof(buffer), &size_read);
i = atoi(buffer);
if ( i == 0 || (i != 0 && (i < 1 || i > 99 ) ) ) {
printf("Bad input, using 0.\n");
return 0;
}
elsereturn i;
}
Often, there seems to be an unexpected character (or characters) hanging out in stdin, such that, when the function is called to get a number, the program does not wait for the user to enter a number -- the program finds its input waiting for it.
I searched on the web for a solution to this and found this suggestion:
while ((c = getchar()) != '\n' && c != EOF);
But that seems to put the program in an infinite loop.
So, looking for any suggestions on how to clear the input stream so that there are no lingering chars.
OK, none of your responses enabled me to get _cgets_s to work. Thanks for nothing (juuuust kidding). But the reference to scanf made me think...Hmm, so, I replaced _cgets_s with scanf.
And viola.
Problem gone.
So, from this I leaned a very, very important lesson: _cgets_s bad, scanf good.
Life is so simple.
Thanks.