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 / if.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  1KB  |  51 lines

  1. /*                         if.c
  2.  *
  3.  *   Synopsis  - Plays a one-time guessing game with the user. 
  4.  *               The user enters a number, which is compared 
  5.  *               with TARGET. The computer issues a diagnostic 
  6.  *               message and then the correct result.
  7.  *
  8.  *   Objective - Illustrates the if-else statement.
  9.  */
  10.  
  11. /* Include Files */
  12. #include <stdio.h>
  13.  
  14. /* Constant Definitions */
  15. #define TARGET 17
  16.  
  17. /* Function Prototypes */
  18. void test( int guess, int target );
  19. /*   PRECONDITION:  guess can be any integer.
  20.  *
  21.  *   POSTCONDITION: The value of its parameter is compared to target, 
  22.  *                  and the result is displayed.
  23.  */
  24.  
  25. int main( void )
  26. {
  27.      int a_guess;
  28.  
  29.      printf( "I'm thinking of an integer.\n" );
  30.      printf( "Try to guess it now. " );
  31.  
  32.      scanf( "%d", &a_guess );
  33.  
  34.      test( a_guess, TARGET );
  35.  
  36.      printf( "The number was %d.\n", TARGET );
  37.      return 0;
  38. }
  39.  
  40. /*******************************test()**************************/
  41.  
  42. void test( int guess, int target )
  43. {
  44.      if ( guess < target )                      /* Note 1 */
  45.           printf( "Too low.\n" );
  46.      else if ( guess > target )                 /* Note 2 */
  47.           printf( "Too high.\n" );
  48.      else                                       /* Note 3 */
  49.           printf( "You guessed it!\n" );
  50. }
  51.