I thought OutOfRange was pretty self-explanitory.
Code:
static int OutOfRange ( int Val, int Lower, int Upper )
{
return Val < Lower || Upper < Val;
}
The function OutOfRange takes parameters Val, Lower, and Upper. It returns true (1) if Val is less than Lower or Val is greater than Upper. If Val is equal to or greater than Lower and Val is equal to or less than Upper, it returns false (0). Or simply, it returns 1 if Val is OutOfRange of Lower to Upper (inclusive).
Code:
static int Menu ( void )
{
int Option = 0;
while ( OutOfRange ( Option, 1, 4 ) )
When called int the function Menu, Option (whose value is passed to OutOfRange as the parameter Val) is initialized to 0. This will cause OutOfRange(Option,1,4) to return 1 and the while loop will be entered.
Code:
if ( scanf ( " %d", &Option ) != 1 )
{
/* Tidy up */
int Ch;
while ( ( Ch = getchar() ) != '\n' && Ch != EOF );
}
The scanf statement checks to see if it successfully scanned an integer into Option. If not, the "Tidy up" part consumes characters remaining in the stdin up to a newline. This might happen if the user enters "twelve".
Code:
if ( OutOfRange ( Option, 1, 4 ) )
printf ( "\nInvalid entry, please try again.\n" );
}
return Option;
Then the OutOfRange function is used again to display an error message if the new value of Option is out of range. The while loop keeps going as long as you enter a value that is OutOfRange. If you enter a valid Option, the loop is broken and the value of Option is returned.