home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap11 / card.c next >
Encoding:
C/C++ Source or Header  |  1988-04-06  |  1.9 KB  |  73 lines

  1. /* card.c  --  demonstrates how to declare structures  */
  2. /*             and how to use structure members        */
  3.  
  4. #include <stdio.h>      /* for NULL  and stdin */
  5. #include <string.h>     /* for strdup()        */
  6.  
  7. #define MAXN 79
  8.  
  9. struct cardstruct {                 /* global pattern */
  10.     char *first, *last, *middle;
  11.     long street_num;
  12.     char *street, *city, *state;
  13.     long zip;
  14.     int  area_code;
  15.     long phone;
  16. };
  17.  
  18. main()
  19. {
  20.     char *Str_Input();
  21.     long Lint_Input();
  22.     struct cardstruct card1;
  23.  
  24.     card1.first         = Str_Input("First Name");
  25.     card1.last          = Str_Input("Last Name");
  26.     card1.middle        = Str_Input("Middle Name");
  27.     card1.street_num    = Lint_Input("Street Number");
  28.     card1.street        = Str_Input("Street Name");
  29.     card1.city          = Str_Input("City");
  30.     card1.state         = Str_Input("State");
  31.     card1.zip           = Lint_Input("Zip Code");
  32.     card1.area_code     = (int)Lint_Input("Area Code");
  33.     card1.phone         = Lint_Input("Phone Number");
  34.  
  35.     printf("\n\n");
  36.     printf("%s %s %s\n", card1.first, card1.middle, 
  37.             card1.last);
  38.     printf("%ld %s, %s, %s %ld\n", card1.street_num, 
  39.             card1.street, card1.city, card1.state,
  40.             card1.zip);
  41.     printf("(%d) %ld\n", card1.area_code, card1.phone);
  42.  
  43.     return (0);
  44.     }
  45.  
  46. char *Str_Input(char *prompt)
  47.     {
  48.     char buf[MAXN+1], *ptr;
  49.  
  50.     printf("%s: ", prompt);
  51.     if (fgets(buf, MAXN, stdin) == NULL)
  52.         exit(0);
  53.     buf[strlen(buf) - 1] = '\0'; /* strip '\n' */
  54.     if (strlen(buf) == 0)
  55.         exit(0);
  56.     if ((ptr = strdup(buf)) == NULL)
  57.         exit(0);
  58.     return (ptr);
  59. }
  60.  
  61. long Lint_Input(char *prompt)
  62. {
  63.     char buf[MAXN + 1];
  64.     long num;
  65.  
  66.     printf("%s: ", prompt);
  67.     if (fgets(buf, MAXN, stdin) == NULL)
  68.         exit(0);
  69.     if (sscanf(buf, "%ld", &num) != 1)
  70.         exit(0);
  71.     return (num);
  72. }
  73.