0

I'm working on a payroll program that takes user input (first/last names, payrate, and hours worked) and outputs those details along with gross, tax, and net.

I have separate modules for input, calc gross, calc tax, and calc net; but I am unsure how to implement array storage so that it will include the data from each of those modules.

any help would be appreciated

*Edit: I need to be able to sort the data (alphabetically) by employee, so what I need ( I believe) is a record of the arrays. I have the record initialized within the main module and want the record to read something like:

lastName[1], firstName[1] payrate[1] hours[1] gross[1] tax[1] net[1]

lastName[2], firstName[2] payrate[2] hours[2] gross[2] tax[2] net[2]

I don't know how to pull the derived data from the other modules into this structure.

Community
  • 1
  • 1
  • Do you mean you want a single array to store the user's first name, last name, pay rate, hours worked, gross pay, etc? That can't be done in C; elements of an array all have to be the same type, so you can't store strings like first name in the same array as doubles/floats like gross pay. You could create a structure for each user and create an array or linked list of user structures. – verbose Jul 29 '13 at 23:01

4 Answers4

4

You can just make a global structure to hold that data instead.

struct payroll_info
{
    char *fname;
    char *lname;
    ...
};

You can then just make an array of this struct and sort using qsort()

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
1

In addition to the struct suggestions that have been offered...,
your question also has to do with visibility of data between files (modules).
Here is a great link that will clarify how to create and define information such as a struct in one file (usually a .h), assign values to the struct members and use them in another file (likely a .c) and modify the values in a third file. (likely also a .c).

As for the array of structs; Create it in a header file that will be included by any .c module that will use it. It could look something like:

      #define NUM_EMPL 10 //number of employees  

      typedef struct {
        char last[40];
        char first[40];
        float payrate;
        int hours;
        float gross;
        float tax;
        float net;
    } EMPLOYEE;

    extern EMPLOYEE empl[NUM_EMPL], *pEmpl; //'extern' keyword is used only in this file

Then, in all .c module(s), where this struct will be used, at top of file somewhere (i.e. not inside a function), create a new instance of the struct that is defined in the .h file:

EMPLOYEE empl[NUM_EMPL], *pEmpl; //same as in .h file except without 'extern' keyword    

Then, within functions you can either initialize the pointer version of the structure to the beginning of the struct definition, and populate the struct members with values;

void func1(void)
{
    Empl = &empl[0];  // '[0]' guarantees pointer location.

    //finally assign values   
    //(can be put into loop or similar to assign all n employess)
    strcpy(pEmpl[0].last, "Smith");
    strcpy(pEmpl[0].first, "John");
    pEmpl[0].payrate = 100.00;
    pEmpl[0].hours = 40;
    //and so on...
    //pass pointer to struct to another function
    func2(pEmpl);
}

Or, you receive a pointer to the struct as an argument:
(The following function can be in a different .c file to better demonstrate inter-file visibility of the data.)

void func2(EMPLOYEE *e)
{
    // assign values   
    //(can be put into loop or similar to assign all n employess)
    strcpy(e[0].last, pEmpl[0].last);
    strcpy(e[0].first, pEmpl[0].first);
    e[0].payrate = pEmpl[0].payrate;
    e[0].hours = pEmpl[0].hours;
    //and so on...
}
Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • `char last[40];` is dangerous, because I can guarantee you that there will be someone with a longer name somewhere, no matter how long you choose your buffer to be. I would always use `char* last;` and dynamically allocate the memory for it, preferably using `asprintf()` or `%as` format string to `scanf()` if I have the glibc available. – cmaster - reinstate monica Jul 31 '13 at 15:52
  • @cmaster, I was focusing on visibility of data between multiple c modules. But nevertheless, your point is well taken. Thnaks – ryyker Aug 03 '13 at 00:23
0

You should use structure to store all the datas about a user.

Something like :

 struct user 
 {
    char *lastName;
    char *firstName;
    //other variables
 }
Joyas
  • 433
  • 3
  • 6
0

If the modules are on different binary files,
then you can share the data using the extern keyword like so:

  1. Declare the array in a single place in the code float paychecks[12].
  2. In order to access the data from another file, declare it like so in the importing file
    extern float[] paycheks
  3. Do the above(2) in each file that you wish to use the paychecks array.

You should now have a single copy of the array - with everyone seeing/pointing-to the same array.

Trevor
  • 1,858
  • 4
  • 21
  • 28