Like Tree1Likes
  • 1 Post By quzah

Passing Function to Structure

This is a discussion on Passing Function to Structure within the C Programming forums, part of the General Programming Boards category; I am mid way through a program that will calculate the gross pay for a group of employees (output in ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    13

    Passing Function to Structure

    I am mid way through a program that will calculate the gross pay for a group of employees (output in table format). The function "Get_input" prompts the user to enter the hours worked per the id_number. However, I cannot get the values entered to return back to the main function (and structure?). Not really sure what I missing. Any help will be greatly appreciated.

    Code:
     
    #include <stdio.h>
     
    /* Define Constants */
    #define NUM_EMPL 5
    #define OVERTIME_RATE 1.5f
    #define STD_HOURS 40f
     
    {
      long  id_number;
      float wage;
      float hours;
      float overtime;
      float gross;
    };
        
    /* define prototypes here for each function except main */
     
        void Get_input (struct employee employeeData [], float hours []);
        void Output_results_screen (struct employee []);
     
     int main()
    {
      /* Variable Declaration and initialization */
            struct employee employeeData[NUM_EMPL] = {
            {98401, 10.60},
            {526488, 9.75},
            {765349, 10.50},
            {34645, 12.25},
            {127615, 8.35}
            }, hours[NUM_EMPL], overtime, gross;
         
      /* Function call to output results to the screen in table format. */
          Get_input (employeeData, hours);
          Output_results_screen (employeeData);
      
          system("PAUSE");
          return 0;
     
    } /* main */
    
    /*****************************************/
    /*                         Function: Get_input                   */
    /*****************************************/
     
    void Get_input (struct employee employeeData[], float hours[])
    {
    /*Local Variable Declaration*/
    int i; /* Variable for Loop Index*/
     
    /*Get Employees Hours and Store them in an array*/
    for (i = 0; i < NUM_EMPL; i++)
    {
    printf ("Enter the number of hours worked by Employee # %06li:", employeeData[i].id_number);
    scanf ("%f", &hours[i]);
    }
    }  /* Get_input */
    
    
    /*****************************************/
    /*              Function: Output_results_screen             */
    /*****************************************/
     
    void Output_results_screen (struct employee employeeData[])
    {
            int i;    /* loop index */
    
                  printf("\n\n---------------------------------------\n");
                  printf("Clock   Wage    Hours    OT    Gross \n");
                  printf("---------------------------------------\n");
       
            /* printf information about each employee */
            for (i = 0; i < NUM_EMPL ; ++i)
            {
                  printf("%06li  %5.2f   %4.1f   %4.1f   %6.2f\n",
                            employeeData[i].id_number, employeeData[i].wage,   employeeData[i].hours, employeeData[i].overtime, employeeData[i].gross);
            } /* for */
     
    } /* Output_results_screen */

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void Get_input (struct employee employeeData[], float hours[])
    {
    /*Local Variable Declaration*/
    int i; /* Variable for Loop Index*/
     
    /*Get Employees Hours and Store them in an array*/
    for (i = 0; i < NUM_EMPL; i++)
    {
    printf ("Enter the number of hours worked by Employee # %06li:", employeeData[i].id_number);
    scanf ("%f", &hours[i]);
    }
    }  /* Get_input */
    What line in there do you think is actually putting data into the structures?


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

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    13

    Passing Function to Structure

    The main function calls for the "Get_input function" using (employeeData, hours). I was hoping that this would pass back to the variable "hours" defined within struct employee employeeData[NUM_EMPL]. Code segments are posted below.

    Code:
    struct employee employeeData[NUM_EMPL] = {
            {98401, 10.60},
            {526488, 9.75},
            {765349, 10.50},
            {34645, 12.25},
            {127615, 8.35}
            }, hours[NUM_EMPL], overtime, gross; /* variable for hours */
     
      /* call Get_input function to get hours entered */
          Get_input (employeeData, hours);

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, well assuming that this:
    Code:
     
    {
      long  id_number;
      float wage;
      float hours;
      float overtime;
      float gross;
    };
    ...was supposed to have a struct employee to start it, then this:
    Code:
    void Get_input (struct employee employeeData[], float hours[])
    is a separate array, independent of your structure. You don't really need it, if all you wanted to do was to fill: employeeData[ x ].hours.


    Quzah.
    CommonTater likes this.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    That is it. Trying to fill employeeData[ x ].hours. But... passing it back the values from my "Get_input" function so that it is applied to the structure array. I am just having trouble making the connection in my head. I think I will just take a step back and rethink my approach.

    Thanks for your help.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
     
    {
      long  id_number;
      float wage;
      float hours;
      float overtime;
      float gross;
    };
    Your problem is that the code snippet above is nothing, it doesn't define anything... even as discrete variables they won't exist outside the scope created by the curly braces... You can't use them, you can't assign to them nothing...

    I rather suspect you wanted...
    Code:
    struct employee
    {
      long  id_number;
      float wage;
      float hours;
      float overtime;
      float gross;
    };
    Which then defines your employee struct for future use.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Sorry to waste everyones time. That part is in the code, I just missed it when copying and pasting into this forum.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by C-bob View Post
    Sorry to waste everyones time. That part is in the code, I just missed it when copying and pasting into this forum.
    How the heck did you miss a line in the middle of a scoop and poop operation? I could see you missing the first or last line ... but from the middle?

    In any case the answer to your problem is to post your input of hours directly to employeedata[i].hours... then run your calculations out of the struct itself.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by C-bob View Post
    Code:
    void Get_input (struct employee employeeData[], float hours[])
    {
    /*Local Variable Declaration*/
    int i; /* Variable for Loop Index*/
     
    /*Get Employees Hours and Store them in an array*/
    for (i = 0; i < NUM_EMPL; i++)
    {
    printf ("Enter the number of hours worked by Employee # %06li:", employeeData[i].id_number);
    scanf ("%f", &hours[i]);
    }
    }  /* Get_input */
    Ok, so change that up a bit:
    Code:
    void Get_input (struct employee employeeData[] )
    {
        int i;
     
        for (i = 0; i < NUM_EMPL; i++)
        {
            printf ("Enter the number of hours worked by Employee # %06li:", employeeData[i].id_number);
            scanf ("%f", &employeeData[ i ].hours);
        }
    }  /* Get_input */
    Just scan directly into the structure.


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

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    My function headers are long. I wanted to keep the code I was posting lean. It was a accidently delete when doing so. So much for trying to be considerate.

    Anyways, thanks for the advice. I will give that a shot. I think I see your point.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Did not see the last post from quzah until now. Thats what I figured out from the last post, but wanted to say, Thanks!

    Now just trying to figure out how to call it in the main function. I tried Get_input (employeeData), Get_input (employeeData, hours), Get_input (hours), and Get_input (stuct employee hours), but my compiler either says too few agruements or incompatible. Thoughts?

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Wait! Nevemind. I open and closed my compiler and Get_input (employeeData, hours) worked. Not sure why that works. Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing structure field to function
    By Lude in forum C Programming
    Replies: 5
    Last Post: 01-18-2008, 02:45 PM
  2. passing pointer to structure to a function
    By samual_van in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2005, 07:18 PM
  3. Passing structure to function
    By Sereby in forum C Programming
    Replies: 4
    Last Post: 07-28-2004, 10:05 PM
  4. Passing structure data to a function?
    By diddy02 in forum C Programming
    Replies: 8
    Last Post: 08-17-2002, 04:15 PM
  5. Passing a structure to a function?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2001, 03:28 AM

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