I'm new to c language and trying to write a fail-safe in my program in case of wrong input by the user.
This is the fail-safe part:
int h1, m1;
char junk; /* Storing a colon or a period when people type in the hour */
printf("Enter start hour: ");
while (scanf("%d %c %d", &h1, &junk, &m1) != 3)
{
printf("Invalid, try again\n");
}
In any case of wrong input (for example entering a char instead of an int) the loop just continues ad infinitum. Doesn't matter if I scan every variable individually, or assign them 0 at the end of the iteration.
I searched Stack for answers, but only found methods for strings (basically moving to 'fgets' or 'isdigit()') or simply for scanning a single integer.
Is there a way to fail-safe the scan both for int and char data types?