Code:
// structure to hold the client's data
struct client
{
char last_name[50]; // client's last name
char first_name[50]; // client's first name
char birthdate[13]; // dd, mmm, yyyy
int height; // in cm
int weight; // in kg
int visits; // enter the number of visits the client has made
// to the spa
struct spa *visitptr; // pointer to spa struct to access spa data
};
struct spa // records of the spa visits
{
char visit_date[13]; // dd, mmm, yyyy
char treatment[100]; // treatment done at time of visit
double fee; // fee for each client
char notes[200]; // notes about client data
} *spaptr; // spa struct pointer
int main ( void )
{
// counter variable for the client struct array
int index = 0;
struct client data;
while ( input_data( &data ) == 0 )//&& index++ < 100 )
{
printf("In main again: ");
printf("Name: %s, %s \n", data.last_name, data.first_name);
printf ("Birthdate: %s\n", data.birthdate );
printf ("Leaving main to go to print_record\n");
print_record ( &data, data.visitptr );
}
printf ("End of main\n");
return 0;
}// end of main
// user input client data
int input_data( struct client *dataptr )
{
char temp[550];
int index = 0;
// prompt user for last name and place name into array
printf ( "Enter the client's last name > " );
gets ( temp );
// check to make sure that data is entered
if ( strlen ( temp ) > 0 )
{
strncpy ( dataptr->last_name, temp, 550 - strlen ( temp ) );
printf ( "\nLast name: %s", dataptr->last_name );
printf ( "\nEnter the client's first name > " );
gets ( temp );
strncpy ( dataptr->first_name, temp, 550 - strlen ( temp ) );
printf ( "\nFirst name: %s", dataptr->first_name );
printf ( "\nEnter the client's date of birth ");
printf ( "( example: 29, Sep, 1970) > ");
gets ( temp );
strncpy ( dataptr->birthdate, temp, strlen ( temp ) );
printf ( "\nBirth date: %s", dataptr->birthdate );
printf ( "\nEnter the client's height in cm > " );
dataptr->height = atoi ( gets ( temp ) );
printf ( "\nHeight: %d", dataptr->height );
printf ( "\nEnter the client's weight in kg > " );
dataptr->weight = atoi ( gets ( temp ) );
printf ("\nWeight: %d", dataptr->weight );
printf ( "\nEnter the total number of visits to the spa > ");
dataptr->visits = atoi ( gets ( temp ) );
printf("\nNumber of visits: %d", dataptr->visits );
printf ("Going into client_records\n");
if ( dataptr->visits > 0 )
// send number of visits to client_records to get information
//on last 5 visits
client_records( dataptr->visitptr, dataptr->visits );
printf ("Leaving input_data\n");
return 0;
}
else // if no data is entered return 1
return 1;
}
// initialize client records
void client_records ( struct spa *spaptr, int times )
{
int occur, count = 0;
printf("Inside client_records\n");
// allocate enough memory for 5 visits
spaptr = ( struct spa * ) malloc ( 5 * sizeof (struct spa) );
if ( times <= 5 )
occur = times;
else
occur = 5;
printf ( "\nEnter the spa visit date ( example: 3, May, 2002) > " );
while ( (gets( spaptr[count].visit_date ) != '\0')
&& ( count < occur) )
{
printf("\nVisit date: %s", spaptr[count].visit_date );
// prompt user for spa visit information
printf ( "\nEnter the treatment recieved on that date > ");
gets ( spaptr[count].treatment );
printf ( "\nTreatment: %s", spaptr[count].treatment );
spaptr[count].fee = calculate_fee ( count + 1);
// call the calculate fee function
printf ( "\nFee: $%3.2f", spaptr[count].fee );
printf ( "\nEnter notes for client > " );
gets ( spaptr[count].notes );
printf ("\nNotes: %s", spaptr[count].notes );
if ( count < occur - 1 )
{
// don't prompt the user for the next visit date unless there // are more to get
printf ( "\nEnter the spa visit date > " );
}
// increment the counter
count++;
}
printf ( "Leaving client_records\n");
// deallocate the memory allocated by malloc
free ( ( void * ) spaptr );
}
// print only the record for the specified client
void print_record ( struct client *dataptr, struct spa *spaptr)
{
int index;
printf("Inside print_record\n");
printf ("%s, %s, ", dataptr->last_name, dataptr->first_name);
printf ("%s, ", dataptr->birthdate);
printf ("%d, ", dataptr->height );
printf ("%d, %d\n", dataptr->weight, dataptr->visits );
// HERE IS WHERE I THINK MY PROBLEM IS AT:
// print information on last 5 visits
while ( dataptr->visits > 0 && dataptr->visits < 5)
{
for ( index = 0; index < 5; index++ )
{
printf ("%s, ", spaptr[index].visit_date );
printf ("\"%s\", ", spaptr[index].treatment);
printf ("%f, ", spaptr[index].fee );
printf ("\"%s\"\n", spaptr[index].notes );
}
}
}
I have identified where my problem is. My program crashes at this point. I believe I am calling the spa information wrong. What I need to know is how to call visit_date and all things inside the spa struct and how my function definition and call should read.