It's a portion of code for a way to track DVDs which includes the title, comment and the rating. The one I'm wondering is the rating part.
Without Flush(), if I do not enter a number, and I hit enter, it goes through an endless loop repeating "Enter DVD Rating". But with the flush, it will prompt again till it gets the number.Code:do { num = 0; printf( "Enter DVD Rating (1-10): " ); scanf( "%d", &num ); Flush(); } while ( ( num < 1 ) || ( num > 10 ) ); void Flush( void ) { while ( getchar() != '\n' ) ; }
I don't get how this Flush() function prevents the infinite loop and keeps prompting for the number.