Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - In C language, users need to enter an integer. How to judge that the program does not run when the user enters an integer, and remind the user to re-enter the integer?
In C language, users need to enter an integer. How to judge that the program does not run when the user enters an integer, and remind the user to re-enter the integer?
Just add the following code to your program.

int flag = 0; //Mark with a sign

Floating I; //float is used here because the user can enter a decimal.

Do {

scanf("%f ",I);

If (me! =(int)i) {// Perform forced type conversion on i to determine whether the number converted into an integer is equal to the value of i..

Printf ("The number you entered is not an integer! Please re-enter! "

flag = 0; //If the input is not an integer, set the flag to 0.

}while(! Flag); //judges! Whether the flag is true or not, if so, continue to execute the loop.