home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap11 / udemo.c < prev   
Encoding:
C/C++ Source or Header  |  1988-04-06  |  2.4 KB  |  104 lines

  1. /* udemo.c  --  demonstrates a union at work */
  2.  
  3. #include <stdio.h>
  4.  
  5. char *Strings[6] = {
  6.         "Quit",
  7.         "line of text",
  8.         "floating point double value",
  9.         "long integer value",
  10.         "floating point value",
  11.         "integer value"
  12. };
  13.  
  14. struct Unitstruct {
  15.     union {
  16.         char   wtype[BUFSIZ];
  17.         double dtype;
  18.         long   ltype;
  19.         float  ftype;
  20.         int    itype;
  21.     } manyu;
  22.     int type_in_union;
  23. };
  24.  
  25. main()
  26. {
  27.     struct Unitstruct one_of_many;
  28.  
  29.     while ((one_of_many.type_in_union = Menu()) != 0)
  30.         {
  31.         Inputval(&one_of_many);
  32.         Printval(&one_of_many);
  33.         }
  34. }
  35.  
  36. Inputval(struct Unitstruct *one_of_many)
  37.     {
  38.     printf("\nEnter a %s: ", Strings[one_of_many->type_in_union]);
  39.     switch(one_of_many->type_in_union)
  40.         {
  41.         case 1: 
  42.             fgets(one_of_many->manyu.wtype, BUFSIZ, stdin);
  43.             break;
  44.         case 2:
  45.             scanf("%lf", &(one_of_many->manyu.dtype));
  46.             while (getchar() != '\n');
  47.             break;
  48.         case 3:
  49.             scanf("%ld", &(one_of_many->manyu.ltype));
  50.             while (getchar() != '\n');
  51.             break;
  52.         case 4:
  53.             scanf("%f", &(one_of_many->manyu.ftype));
  54.             while (getchar() != '\n');
  55.             break;
  56.         case 5:
  57.             scanf("%i", &(one_of_many->manyu.itype));
  58.             while (getchar() != '\n');
  59.             break;
  60.         }
  61. }
  62.  
  63. Printval(struct Unitstruct *one_of_many)
  64. {
  65.     printf("The %s you entered\nwas: ", Strings[one_of_many->type_in_union]);
  66.     switch (one_of_many->type_in_union)
  67.         {
  68.         case 1: 
  69.             fputs(one_of_many->manyu.wtype, stdout);
  70.             break;
  71.         case 2:
  72.             printf("%lf", one_of_many->manyu.dtype);
  73.             break;
  74.         case 3:
  75.             printf("%ld", one_of_many->manyu.ltype);
  76.             break;
  77.         case 4:
  78.             printf("%f", one_of_many->manyu.ftype);
  79.             break;
  80.         case 5:
  81.             printf("%i", one_of_many->manyu.itype);
  82.             break;
  83.         }
  84.     printf("\n\n");
  85. }
  86.  
  87. Menu()
  88. {
  89.     int i;
  90.     char ch;
  91.  
  92.     for (i = 0; i < 6; ++i)
  93.         {
  94.         printf("%d) %s\n", i, Strings[i]);
  95.         }
  96.     printf("Which: ");
  97.     do
  98.         {
  99.         ch = getch();
  100.         } while (ch < '0' || ch > '5');
  101.     printf("%c\n", ch);
  102.     return (ch - '0');
  103. }
  104.