structs and passing information to and from functions

This is a discussion on structs and passing information to and from functions within the C Programming forums, part of the General Programming Boards category; [tag] This is for homework. I am having a hard time figuring out how to get the spa visit information ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Question structs and passing information to and from functions

    [tag]
    This is for homework. I am having a hard time figuring out how to get the spa visit information to print out. Could someone please help me?
    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.
    Thank you so much for your time, Sandy
    [/tag]

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    I don't understand the loop: "while ( dataptr->visits > 0 && dataptr->visits < 5)"

    If you want to loop through the first 5 things but there may not be 5 things in your array, then setup a limit for the for loop:
    Code:
    int max = 5;
    if (dataptr->visits < 5)
       max = dataptr->visits;
    ...
    for (index = 0; index < max; index++) ...
    gg

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    There are a few problems:
    Code:
    int input_data( struct client *dataptr )
    {
        ...
        //Passing a pointer which points to nothing, this pointer will never be update
        //by client_records, will cause an error if accessed. 	
        client_records( dataptr->visitptr, dataptr->visits );
        ...
    }
    
    void client_records ( struct spa *spaptr, int times )
    {
        //This will only update the local stack pointer and not the dataptr->visitptr
        //you need to pass the address of the pointer 	
        spaptr = ( struct spa * ) malloc ( 5 * sizeof (struct spa) );
        ...
        ...
        // Why are you freeing the memory?
        free ( ( void * ) spaptr );
    }
    Do something like this....
    Code:
    int input_data( struct client *dataptr )
    {
        ...	
        // Pass the address of the pointer
        client_records( &dataptr->visitptr, dataptr->visits );
        ...
    }
    void client_records ( struct spa **spaptr, int times )
    {
       //Update the pointers pointer	
       *spaptr = ( struct spa * ) malloc ( 5 * sizeof (struct spa) );
       // Don't free memory	
    }

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Smile Thank you

    [tag]
    Thank you for your quick and helpful replies. I do appreciate the help. I'm very new at this so I don't necessarily understand all of what you said, but thanks for the help. I was able to clear up some of the bugs thanks to your suggestions. I'll ask the teacher about the rest. I appreciate it. Sandy
    [/tag]

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Also, I saw it just now, you're using the gets() function to collect a string from a stream (stdin). gets() is a old and evil function (bugged too...) you should use instead the fgets() function.

Popular pages Recent additions subscribe to a feed

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