home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch2 / errorchk.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  1KB  |  49 lines

  1. /*                        errorchk.c
  2.  *
  3.  *   Synopsis  - Prompts for and accepts input of an integer.
  4.  *               The integer is tested to see that it meets the 
  5.  *               stated criteria. If it doesn't, an error 
  6.  *               message is issued. If all criteria are met, 
  7.  *               execution terminates silently.  
  8.  *
  9.  *   Objective - Illustrates the necessary grouping of if and
  10.  *               associated else statements.
  11.  */
  12.  
  13. /* Include Files */
  14. #include <stdio.h>
  15.  
  16. /* Function Prototypes */
  17. int odd( int intvar );
  18. /*   PRECONDITION:  intvar can be any integer
  19.  *
  20.  *   POSTCONDITION: The value returned is 1 if the parameter is odd
  21.  *                  and 0 if the parameter is even
  22.  */
  23.  
  24. int main( void )
  25. {
  26.      int inputint;
  27.  
  28.      printf( "Enter a positive even " );
  29.      printf( "number that is less than 20.\n" );
  30.      scanf( "%d", &inputint );
  31.  
  32.      if ( inputint < 20 ) {                          /* Note 1 */
  33.           if ( odd( inputint ) )
  34.                printf( "Sorry that number wasn't even.\n" );
  35.           else if ( inputint <= 0 )
  36.                printf( "That number wasn't positive.\n" );
  37.      }                                               /* Note 2 */
  38.      else
  39.           printf( "That number was too big.\n" );
  40.      return 0;
  41. }
  42.  
  43. /*******************************odd()***************************/
  44.  
  45. int odd( int intvar )                                /* Note 3 */
  46. {
  47.     return( intvar % 2 );
  48. }
  49.