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 / struct1.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  3KB  |  89 lines

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