I am creating a program in C where i have to accept an integer to continue.I read some questions to use the following code but my program just goes into infinite loop soon after entering any character.Please help me
int tnum,scancheck;
printf("Enter the number of teams(Maximum 4)\n");
scancheck = scanf("%d",&tnum);
while(scancheck != 1)
{
printf("ERROR: Please enter numbers only\n\n");
printf("Enter the number of teams(Maximum 4)\n");
scancheck = scanf("%d",&tnum);
}
I used Kirilenko's method but my program goes into infinite loop on entering a special character such as l=
EDIT PROBLEM HAS BEEN SOLVED.Thanks guys
You have to clean
stdin
between your calls toscanf
. Otherwise the behaviour ofscanf
with an invalid input is to put back the character on the stream. Then, it go to an infinite loop where you read the same wrong character every iteration.