I think your problem is that you don't understand what scanf returns. Here's what a good reference has to say:
Originally Posted by
man 3 scanf
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error.
Rather than EOF, which as far as I can tell is only returned when the file is broken or empty, it is safer to compare against something else:
Code:
while ( scanf( "%d", &userid ) != 1 ) {
/* get rid of garbage from bad scan */
}
Since 1 will be returned when this succeeds and we want to keep trying when it's not that.