home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 98.img / LCNOW2.ZIP / EXAMPLES / STATES.C < prev    next >
C/C++ Source or Header  |  1988-08-02  |  1KB  |  71 lines

  1. /*
  2.  * S T A T E S
  3.  *
  4.  * Use an array of structured variables to keep
  5.  * vital information about the United States.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "states.h"
  12.  
  13. #define SCBUFSIZE 2        /* State code input buffer */
  14.  
  15. int StateData(char *);
  16.  
  17. int
  18. main(void)
  19. {
  20.     char statecode[SCBUFSIZE + 1];
  21.  
  22.     /*
  23.      * Ask the user for a state code and display
  24.      * the data, if any, or an error message.
  25.      */
  26.     printf("State code (CO, MA, etc.): ");
  27.     scanf("%2s", statecode);
  28.     if (StateData(statecode)) {
  29.         fprintf(stderr, "%s: State not found\n", statecode);
  30.         exit (1);
  31.     }
  32.  
  33.     return (0);
  34. }
  35.  
  36. /*
  37.  * StateData()
  38.  *
  39.  * Display the data for a state specified by the state
  40.  * abbreviation (postal code) supplied as an argument.
  41.  */
  42.  
  43. int
  44. StateData(char *code)
  45. {
  46.     int rc = 0;     /* return code */
  47.     int index;      /* array index */
  48.  
  49.     for (index = 0; index < NSTATES; ++index) {
  50.         if (strcmp(strupr(code), State[index].code) == 0)
  51.             break;
  52.     }
  53.  
  54.     /*
  55.      * If the index reaches the number of states in the
  56.      * array, the code was not found.  Report an error.
  57.      */
  58.     if (index == NSTATES)
  59.         return (++rc);
  60.  
  61.     /*
  62.      * Found a state record.  Display the requested data.
  63.      */
  64.     printf("  State name: %s\n", State[index].name);
  65.     printf("Capital city: %s\n", State[index].capital);
  66.     printf("  Population: %u (thousands)\n", State[index].population);
  67.     printf("   Land area: %u sq. miles\n", State[index].area);
  68.  
  69.     return (0);
  70. }
  71.