Possible Duplicate:
How to scanf only integer and repeat reading if the user enter non numeric characters?
Hi i’m new to c and i am writing a program that asks the user to enter an integer and if it does not match the criteria of being within a certain range, like lass than 20, but greater than 5. It asks the user to enter the value again.
However i wondered what would happen if i entered a character, and well it creates an infinite loop.
So i want to know is there a way or a specific function that will check the entered value to see if it is the correct data type. And i not repromts the user.
you can read with
scanf("%d")and if the user enter a string containing a non numeric character then you have to clean the stdin buffer before reading again withscanf("%d").when you clean the stdin, you will stop the infinite loop
Use the following function to clean your stdin
And The read of your integer from stdin could be done in this way: