/* Demonstration of input with checking */ #include main () { int num_input, i, j, k; printf("Enter three values: "); num_input = scanf("%d %d %d", &i, &j, &k); while (num_input < 3) { /* An error has occurred. */ /* First throw away the rest of the input line; we use the simple function, getchar, for this. getchar() reads one character from the input and returns it as its result,. */ while (getchar() != '\n' /* i.e. newline */ ) { /* keep looping until getchar gets the newline. */ } /* Now print the message */ printf("You only typed %d values correctly.\n", num_input); printf("Try again: "); /* Re-read */ num_input = scanf("%d %d %d", &i, &j, &k); } /* Here the values must be correct. */ printf("Your correct input was %d, %d, and %d\n", i, j, k); }