Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Define an integer variable n in C language, and enter the value with %d, how to report an error when inputting letters?
Define an integer variable n in C language, and enter the value with %d, how to report an error when inputting letters?
#include < stdio.h>

int main()

{

int n;

while(scanf("%d", & n) < 1) {// scanf accepts several parameters, here <; A few

printf("input error. try again ... ");

while(getchar() ! = '\n');

}

printf("%d", n);

}

For example,

scanf ("%d% d",&; a, & b) < 2

scanf returns the number of variables successfully entered, and returns EOF if none of them are successful.