/** note, I have included a main at the bottom of this code for testing purposes. Only put the prototype at the top of your main and put the function after main. **/ /********************************************************************* * This function is designed to input an integer value from the user * * and guarantee that it is greater than or equal to some low value. * * It also will check to make sure that a character has not been * * entered. If it has, the subprogram will clean out the rest of the* * input stream. Try it and see how it works. * * It also checks for 327a or 23.398 and says they are errors also. * *********************************************************************/ /** the scanf() returns and integer value indicating the number of correct conversions it was able to make. I use the return value to see if the value was converted to integer correctly. the while((iochar = getchar()... loop is complete in the one statement. It removes all left over stuff in the input stream the isspace() function is in ctype.h so include this library **/ void get_int(int *value, int low) { int iochar, error_flag; do { error_flag = scanf(" %d", value); if (error_flag != 1) { printf("\nEnter an integer value please "); *value = low - 1; while((iochar = getchar()) != '\n'); } else { iochar = getchar(); if (!isspace(iochar)) { printf("\nInvalid integer input, try again \n"); while((iochar = getchar()) != '\n'); *value = low - 1; } else if (*value < low) printf("\nEnter an integer >= to %d ", low); } } while ((error_flag != 1) || (*value < low)); } void main(void) { int x; get_int(&x, 0); printf("\ndone x = %d\n", x); }