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

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