home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap05 / switch.c < prev   
Encoding:
C/C++ Source or Header  |  1988-04-05  |  1.5 KB  |  51 lines

  1. /* switch.c -- demonstrates switch statement */
  2. /*             prints values according       */
  3. /*             to user's choice              */
  4.  
  5. #include <math.h> /* for sqrt() */
  6. #define TRUE 1
  7. main()
  8. {
  9.     char choice;   /* routine wanted by user   */
  10.     int number;    /* number entered by user   */
  11.  
  12.     while (TRUE)   /* endless loop */
  13.     {
  14.         printf("\nSelect value wanted:\n");
  15.         printf("o = octal  h = hex   s = square\n");
  16.         printf("r = square root  q = quit: ");
  17.         choice = getche(); printf("\n");
  18.  
  19.         if (choice == 'q')
  20.             break; /* exits WHILE loop; ends program */
  21.  
  22.         /* rest of program executed if base <> 'q' */
  23.             printf("Enter a whole number: ");
  24.             scanf("%d", &number);
  25.  
  26.         switch (choice) /* print according to */
  27.                         /* choice requested   */
  28.             {
  29.             case 'o':   /* print octal */
  30.                 printf("Result: %o\n", number);
  31.                 break;  /* break here in each case    */
  32.                         /* exits the switch statement */
  33.  
  34.             case 'h':   /* print hex */
  35.                 printf("Result: %x\n", number);
  36.                 break;
  37.  
  38.             case 's':   /* square */
  39.                 printf("Result: %d\n", number * number);
  40.                 break;
  41.  
  42.             case 'r':   /* square root */
  43.                 printf("Result: %f\n", sqrt(number) );
  44.                 break;
  45.  
  46.             default:
  47.                 printf("Choice must be o, h, s, r, or q\n");
  48.             }
  49.      }
  50. }
  51.