Another issue is that you're reading with scanf("%f", &input);
only. If the user types something that can't be interpreted as a C floating-point number, like "pi", the scanf()
call will not assign anything to input
, and won't progress from there. This means it would attempt to keep reading "pi", and failing.
Given the change to while(!feof(stdin))
which other posters are correctly recommending, if you typed "pi" in there would be an endless loop of printing out the former value of input
and printing the prompt, but the program would never process any new input.
scanf()
returns the number of assignments to input variables it made. If it made no assignment, that means it didn't find a floating-point number, and you should read through more input with something like char string[100];scanf("%99s", string);
. This will remove the next string from the input stream (up to 99 characters, anyway - the extra char
is for the null terminator on the string).
You know, this is reminding me of all the reasons I hate scanf()
, and why I use fgets()
instead and then maybe parse it using sscanf()
.