i want to dynamically locate 5 spaces for spa struct inside of client struct, get segmentation fault and core is dumped any help would be appreciated
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Welcome(int);
int client_info (struct client *D, int visit_num);
int visit_info( struct client *D, int visit_num);
struct client
{
char l_name[50];
char f_name[50];
char bday[15];
int height;
int weight;
int visits;
struct spa *r;
};
struct spa
{
char visit_date[15];
char treat[100];
double fee;
char notes[200];
};
int main (void)
{
int visit_num;
struct client D[100];
int i,x;
for (i=0;i<5;i++)
{
D[i].r = (struct spa *) malloc (5 * sizeof (struct spa));
}
/*declare variables*/
do /* loop to contine program */
{
x=Welcome(x);
switch (x)
{
case 1: client_info(D, D->visits);
break;
case 2:
break;
case 3:
break;
case 4:
break;
default: printf(" Please enter a valid selection\n\n");
}
}
while(x !=0);
return(system("pause"));
}
int Welcome (int value)
{
printf ("Welcome to JP Spa Service's\n\n");
printf ("Choose an option: \n");
printf ("(1) Enter New Client: \n");
printf ("(2) Search database by lastname: \n");
printf ("(3) Search database by date of birth: \n");
printf ("(4) Change Client database: \n");
printf ("(5) Print record of one client: \n");
printf ("(6) Print record of all clients: \n");
printf ("(7) Calculate fee of client: \n");
printf ("(0) To EXIT PROGRAM\n");
printf ("Please make a selection now: \n");
scanf ("%d", &value);
return value;
}
int client_info (struct client *D, int visit_num)
{
static int x=0;
char temp[550];
int temp1=0;
printf("\n Enter First Name:\n");
gets(temp);
strncpy (D[x].f_name, temp, 550-strlen(temp));
printf("\nEnter Last Name:\n");
gets(temp);
strncpy (D[x].l_name, temp, 550-strlen(temp));
printf("\nEnter Date of Birth: (exmpl :2, Oct, 1981)");
gets(temp);
strncpy (D[x].bday, temp, 550-strlen(temp));
printf("\nEnter Weight of Client:\n");
D[x].weight = atoi (gets(temp));
printf("\nEnter total number of visits:\n");
D[x].visits = atoi (gets(temp));
visit_num = D[x].visits;
visit_info(D, D->visits);
return visit_num;
}
int visit_info( struct client *D, int visit_num)
{
int occur=0;
int count=0;
printf ( "\nEnter the spa visit date ( example: 3, May, 2002) > " );
while ( (gets(D[count].r->visit_date ) != '\0')
&& ( count < occur) )
{
printf("\nVisit date: %s", (D[count].r)->visit_date );
// prompt user for spa visit information
printf ( "\nEnter the treatment recieved on that date > ");
gets ((D[count].r)->treat);
printf ( "\nTreatment: %s", (D[count].r)->treat);
/*spaptr[count].fee = calculate_fee ( count + 1);*/
// call the calculate fee function
printf ( "\nFee: $%3.2f", (D[count].r)->fee );
printf ( "\nEnter notes for client > " );
gets ((D[count].r)->notes);
printf ("\nNotes: %s", (D[count].r)->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++;
}
}