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

  1. /* rolo.c  --  demonstrates pointers to structures     */
  2.  
  3. #include <stdio.h>      /* for NULL  and stdin */
  4. #include <string.h>     /* for strdup()        */
  5.  
  6. #define MAXN 79
  7. #define MAXCARDS 3
  8.  
  9. struct cardstruct {                 /* global pattern */
  10.     char first[MAXN],
  11.          last[MAXN],
  12.          middle[MAXN];
  13.     unsigned long street_no;
  14.     char street[MAXN],
  15.          city[MAXN],
  16.          state[MAXN];
  17.     unsigned long zip;
  18.     unsigned int area;
  19.     unsigned long phone;
  20. };
  21.  
  22. struct cardstruct cards[MAXCARDS];
  23.  
  24. main()
  25. {
  26.     int i;
  27.  
  28.     for (i = 0; i < MAXCARDS; ++i)
  29.         {
  30.         printf("\n<card %d of %d>\n", i + 1, MAXCARDS);
  31.         Input(&cards[i]);
  32.         }
  33.     for (i = 0; i < MAXCARDS; ++i)
  34.         {
  35.         printf("\n<%d> ", i + 1);
  36.         Showcard(&cards[i]);
  37.         }
  38. }
  39.  
  40. Input(struct cardstruct *cardp)
  41. {
  42.     char *Str_Input();
  43.     long Lint_Input();
  44.  
  45.     strcpy(cardp->first,Str_Input("First Name"));
  46.     strcpy(cardp->last,Str_Input("Last Name"));
  47.     strcpy(cardp->middle,Str_Input("Middle Name"));
  48.     cardp->street_no = Lint_Input("Street Number");
  49.     strcpy(cardp->street,Str_Input("Street"));
  50.     strcpy(cardp->city,Str_Input("City"));
  51.     strcpy(cardp->state,Str_Input("State"));
  52.     cardp->zip = Lint_Input("Zip Code");
  53.     cardp->area = (int)Lint_Input("Area Code");
  54.     cardp->phone = Lint_Input("Phone Number");
  55. }
  56.  
  57. char *Str_Input(char *prompt)
  58. {
  59.     char buf[MAXN + 1], *ptr;
  60.  
  61.     printf("%s: ", prompt);
  62.     if (fgets(buf, MAXN, stdin) == NULL)
  63.         exit(0);
  64.     buf[strlen(buf) - 1] = '\0'; /* strip '\n' */
  65.     if (strlen(buf) == 0)
  66.         exit(0);
  67.     if ((ptr = strdup(buf)) == NULL)
  68.         exit(0);
  69.     return (ptr);
  70. }
  71.  
  72. long Lint_Input(char *prompt)
  73. {
  74.     char buf[MAXN + 1];
  75.     long  num;
  76.  
  77.     printf("%s: ", prompt);
  78.     if (fgets(buf, MAXN, stdin) == NULL)
  79.         exit(0);
  80.     if (sscanf(buf, "%ld", &num) != 1)
  81.         exit(0);
  82.     return (num);
  83. }
  84.  
  85. Showcard(struct cardstruct *cardptr) 
  86. {
  87.     printf("\n\n");
  88.     printf("%s %s %s\n", cardptr->first, cardptr->middle, 
  89.             cardptr->last);
  90.     printf("%ld %s, %s, %s %ld\n", cardptr->street_no, 
  91.             cardptr->street, cardptr->city, cardptr->state,
  92.             cardptr->zip);
  93.     printf("(%d) %ld\n", cardptr->area, cardptr->phone);
  94. }
  95.