I am trying to write a program that takes a series of numbers input by a user and performs some calculations on it. While I have most of the code put together and working perfectly fine, I haven't been able to write code that will refuse the users input if it is a number followed by letters (ie., "12a" or "12f345"). For some reason, this causes the program to close. Below is the snippet of code isolated to test with.
How can I get the code to display a message that the user input was not a number, to ask for a valid number, and to not close the program?
Thank you, All...
#include <stdio.h>
int main(void)
{
float num1;
printf("Please enter a number:");
while(! scanf("%f", &num1))
{
printf("\n\tInvalid entry, please try again: ");
fflush(stdin);
}
printf("The number you entered was %.2f\n", num1);
getChar();
}