I'm trying to create a C program to allow the user to input an integer number greater than 0, but if any other type is entered make the user try again until they enter an integer.
int minBet;
printf("Enter integer"\n);
while(scanf("%d",&minBet)!=1)
{
printf("Must be an integer value greater than 0\n");
scanf(" %d",&minBet);
}
When a non integer value is entered the printf statements loops infinitely. How can I solve this?
can only use stdio.h and NO global varables
If the input character doesn’t match the coversion, it’s left in the input stream to foul up the next read - you’ll have to get rid of it using getchar
or fgetc
.
while ( scanf( “%d”, &minBet ) < 1 )
{
getchar();
printf( “You entered a non-numeric value, try again: );
}