home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap08 / square2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-07  |  997 b   |  45 lines

  1.             /* LISTING 8-2 */
  2.  
  3. /* square.c  --  A quiz to demonstrate passing        */
  4. /*               pointers and addresses in functions. */
  5.  
  6. #include <stdio.h>
  7.  
  8. main()
  9.     {
  10.     int val, count, guess;
  11.  
  12.     for (count = 1; count < 255; ++count)
  13.         {
  14.         val = count;
  15.         printf("\nWhat is the square of %d?\n", val);
  16.         if (scanf(" %d", &guess) != 1)
  17.             return(0);           /* non-number exits   */
  18.  
  19.         if(Square(&val) != 0)    /* pass val's address */
  20.             {
  21.             printf("Range Error\n");
  22.             exit(1);
  23.             }
  24.  
  25.         if (val != guess)
  26.             printf("Wrong. It is %d.\n", val);
  27.         else
  28.             printf("Right!\n");
  29.  
  30.  
  31.         printf("Continue? ");
  32.         if (getche() != 'y')
  33.             break;
  34.         }
  35.     return (0);
  36.     }
  37.  
  38. int Square(int *where)
  39.     {
  40.     if (*where > 181 || *where < -181)
  41.         return (-1);
  42.     *where = (*where) * (*where);
  43.     return (0);
  44.     }
  45.