home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch7 / struct2.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  3KB  |  100 lines

  1. /*               struct2.c
  2.  *
  3.  *   Synopsis  - Initializes an array of structures with input 
  4.  *               from the terminal and displays the total 
  5.  *               inventory on standard output.
  6.  *
  7.  *   Objective - To illustrate arrays of structures, pointers to
  8.  *               structures, and passing structures to functions.
  9.  */
  10.  
  11. /* Include Files */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. /* Constant Definitions */
  17. #define GOT_ONE         1
  18. #define NONE_ENTERED    0
  19. #define ID_SIZE         8
  20. #define MAXPARTS        4
  21. #define BUF_SIZE      512
  22.  
  23. /* Type Descriptions */
  24. struct auto_part {
  25.      char id[ID_SIZE];
  26.      double price;
  27.      int cur_inv;
  28. };
  29.  
  30. /* Function Prototypes */
  31. int get_part( struct auto_part *partptr );
  32. /*   PRECONDITION:  partptr contains the address of a struct auto_part.
  33.  *   POSTCONDITION: Populates the struct auto_part, whose address is 
  34.  *                  contained in the parameter, with information read 
  35.  *                  from the keyboard then returns 1 if data was 
  36.  *                  entered by the user and 0 if no data was entered.
  37.  */
  38.  
  39. void put_part( struct auto_part part );
  40. /*   PRECONDITION:  part is a struct auto_part.
  41.  *   POSTCONDITION: Displays the contents of the struct auto_part 
  42.  *                  passed as a parameter.
  43.  */
  44.  
  45. int main( void )
  46. {
  47.      struct auto_part parts[MAXPARTS];                     /* Note 1 */
  48.      int j, num_parts = 0;
  49.  
  50.      printf( "Initializing Inventory\n" );
  51.      printf( "------------ ---------\n\n" );
  52.  
  53.                                                            /* Note 2 */
  54.      while ( get_part( parts+num_parts ) && ++num_parts < MAXPARTS )
  55.      ;
  56.      if ( num_parts == MAXPARTS )                          /* Note 3 */
  57.           printf( "Inventory full\n" );
  58.  
  59.      printf( "\n\nPrinting Inventory of Auto Parts\n" );
  60.      printf( "-------- --------- -- ---- -----\n\n" );
  61.      for ( j = 0; j < num_parts; j++ )
  62.           put_part( parts[j] );
  63.      return 0;
  64. }
  65. /*******************************get_part()**********************/
  66.  
  67. int get_part( struct auto_part *partptr )
  68. {
  69.      char instring[BUF_SIZE];
  70.  
  71.      printf( "Enter the part number :" );
  72.      fgets( instring, BUF_SIZE, stdin );
  73.      instring[ strlen(instring)-1 ] = '\0';
  74.      if ( strlen( instring ) > 0 ) {
  75.  
  76.           strncpy( partptr->id, instring, ID_SIZE-1 );
  77.           partptr->id[7] = '\0';
  78.  
  79.           printf( "Enter the price: " );
  80.           fgets( instring, BUF_SIZE, stdin );
  81.           partptr->price = atof( instring );
  82.  
  83.           printf( "Enter the amount in inventory : " );
  84.           fgets( instring, BUF_SIZE, stdin );
  85.           partptr->cur_inv = atoi( instring );
  86.           return( GOT_ONE );
  87.      }
  88.      else
  89.           return( NONE_ENTERED );
  90. }
  91.  
  92. /*******************************put_part()**********************/
  93.  
  94. void put_part( struct auto_part part )
  95. {
  96.      printf( "Part-id:  %8s\n", part.id );
  97.      printf( "Price :  $%8.2f\n", part.price );
  98.      printf( "Quantity: %8d\n", part.cur_inv );
  99. }
  100.