> this part of code. Could you explain it ?
I did - with this comment.
// this throws away the garbage line of input
I suggest reading the manual page for getchar() to begin with.
> Now , if i enter "1a" as a question number , the program says both "Question 1" and "That's not a number"
If you want to validate a whole line, the best thing to do is read a line using fgets, then parse it with sscanf.
Say
Code:
char buff[BUFSIZ]; // look it up
while ( fgets( buff, BUFSIZ, stdin ) != NULL ) { // look it up
int a;
char c;
if ( sscanf( buff, "%d%c", &a, &c ) == 2 && c == '\n' ) {
// an integer, a newline - nothing more, nothing less
} else {
// error
}
}