get segmentation fault

This is a discussion on get segmentation fault within the C Programming forums, part of the General Programming Boards category; i want to dynamically locate 5 spaces for spa struct inside of client struct, get segmentation fault and core is ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    17

    get segmentation fault

    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++;
        }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about narrowing it down for us? When do you get a seg fault? What're you doing when it happens, and/or, what's the last thing you see on the screen?

    Also, don't use gets. It's prone to buffer overflows.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21