Probably a simple answer to this, but I am relatively new to C. I have a loop that validates that a user has entered an integer value within certain parameters. It works fine, except when a user inputs a character. I thought my code checked for this already, but I was wrong.
My code at the moment:
while(1) {
printf("Enter Recommended Shot Doses (1-255): ");
if(scanf("%d", &shotDoses) == 1 &&
shotDoses >=1 && shotDoses <=255) break;
printf("\033[1;31mPlease Enter a Valid Number.\033[0m\n");
}
I get an infinite loop if you accidentally input a character instead of a number. Any help?
Thanks