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 >
Wrap
C/C++ Source or Header
|
2005-06-16
|
3KB
|
89 lines
/* struct1.c
*
* Synopsis - Accepts input of an auto_part from standard
* input and echoes it to standard output.
*
* Objective - To illustrate a pointer to a structure and
* passing a pointer to a structure and a
* structure as parameters to a function.
*/
/* Include Files */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Constant Definitions */
#define GOT_ONE 1 /* Note 1 */
#define NONE_ENTERED 0
#define ID_SIZE 8
#define BUF_SIZE 512
/* Type descriptions */
struct auto_part { /* Note 2 */
char id[ID_SIZE];
double price;
int cur_inv;
};
/* Function Prototypes */
void put_part( struct auto_part part ); /* Note 3 */
/* PRECONDITION: part is a struct auto_part.
* POSTCONDITION: Displays the contents of the struct auto_part
* passed as a parameter.
*/
int get_part( struct auto_part *partptr ); /* Note 4 */
/* PRECONDITION: partptr contains the address of a struct auto_part.
* POSTCONDITION: Populates the struct auto_part, whose address is
* contained in the parameter, with information read
* from the keyboard then returns 1 if data was
* entered by the user and 0 if no data was entered.
*/
int main( void )
{
struct auto_part part; /* Note 5 */
int retval;
retval = get_part( &part ); /* Note 6 */
if ( retval == GOT_ONE )
put_part( part );
return 0;
}
/*******************************get_part()**********************/
int get_part( struct auto_part *partptr ) /* Note 4 */
{
char instring[BUF_SIZE];
printf( "Enter the part number :" );
fgets( instring, BUF_SIZE, stdin );
instring[ strlen(instring)-1 ] = '\0';
if ( strlen( instring ) > 0 ) {
strncpy( partptr->id, instring, 7);
partptr->id[7] = '\0';
printf( "Enter the price: " );
fgets( instring, BUF_SIZE, stdin );
partptr->price = atof( instring );
printf( "Enter the amount in inventory : " );
fgets( instring, BUF_SIZE, stdin );
partptr->cur_inv = atoi( instring );
return( GOT_ONE );
}
else
return( NONE_ENTERED );
}
/*******************************put_part()**********************/
void put_part( struct auto_part part ) /* Note 3 */
{
printf( "Part-id: %8s\n", part.id ); /* Note 7 */
printf( "Price : $%8.2f\n", part.price ); /* Note 7 */
printf( "Quantity: %8d\n", part.cur_inv ); /* Note 7 */
}