Hello,
As will become quickly & painfully evident, I am new to "C" programming and trying to teach myself.
I am trying to use scanf function to get numeric input from the keyboard.
My code looks like this
(NOTE: Please forgive syntax errors as I am not copying and pasting working code, but retyping)
function = 0;
while (function < 1 || function > 3) {
printf ("Please enter a Function code. 1 = W 2 = W/V 3 = V\n");
scanf ("%i", &function);
if (function < 1 || function > 3)
printf ("BAD FUNCTION CODE, Please reenter");
else
printf ("\n");
}
This code does what I want it to do as long as a number is entered. However, if an alpha is entered it gets stuck in an endless loop, querrying for a new function code but never stopping for user input. Apparently, once the alpha is entered it is stuck in the keyboard buffer and keeps getting read over and over.
I am using an a old version of Borland C++ (V4.52) that I found lying around work and running under Windows 98.
Is there some way to avoid this while still using scanf? Should I just chunk scanf and use getchar and then convert the ASCII to an integer?
Finally, I would like to learn how program "right" so, if while you are looking at the problem if you have any comments on things that I am doing one way that would be better done another (can't be "wrong", since the code runs ..... right?) I would appreciate it.
Thanks,
JS