0

I have to write a program to calculate 10 employees gross pay, deductions, net pay, and include overtime if applicable. I must use a structure to do this and the name can be a max of 20 characters and the ID is 6 characters. I know my main problem is how I am using array in my structure as I had this working fine with only one employee. Maybe I am just not understanding how to properly implement them into the structure. I have tried nesting it with another structure and that included my name [21] and pin[7] and doing it more as a string, and a few other things that sounded viable in my head but nothing has worked properly. Any help is appreciated, and be gentle I am new at this lol. Thank you in advance.

#include <stdio.h>
#include <stdlib.h>

#define MAX 10

struct payroll
{
 char name [MAX][21];
 char pin [MAX][7];
 float hours[MAX];
 float hourly_pay[MAX];
 float gross_pay[MAX];
 float net_pay[MAX];
 float overtime_pay[MAX];
 float deductions[MAX];
};

typedef struct payroll PAYROLL;

void funcoutput (PAYROLL);

int main( )
{
 PAYROLL employee;
 int i;

 for (i=0;i<=MAX;i++)
 {
    printf ("Enter the employees last name:\n");
    scanf("%s", &employee.name[i]);

    printf ("Enter the employees 6 character ID:\n");
    scanf("%s", &employee.pin[i]);

    printf ("Enter the employees hours for the week:\n");
    scanf ("%f", &employee.hours[i]);

    printf ("Enter the employees hourly rate of pay: \n");
    scanf ("%f", &employee.hourly_pay[i]);

    printf ("Enter any employee ovetime hours, hours exceeding 40: \n");
    scanf ("%f", &employee.overtime_pay[i]);

    employee.overtime_pay[i] = employee.overtime_pay[i] * 1.5;
    employee.gross_pay[i] = employee.hours[i] + employee.hourly_pay[i] +     employee.overtime_pay[i];
    employee.deductions[i] =  employee.gross_pay[i] * .25;
    employee.net_pay[i] = employee.gross_pay[i] - employee.deductions[i];
 }

 funcoutput (employee);

 return 0;
}

void funcoutput (PAYROLL employee1)
{
  int i;

  for (i=0;i<MAX;i++)
  {
    printf("Name:%s  ID:%s  Hours:%8.2f  Hourly rate:$%8.2f  Gross pay:$%8.2f  Deductions:$%8.2f    Net pay:$%8.2f\n", 
        employee1.name[i],employee1.pin[i],employee1.hours[i],employee1.hourly_pay[i],
        employee1.gross_pay[i],employee1.deductions[i],employee1.net_pay[i]);
  }

 return;
}
5
  • 4
    Unravel that in the other direction. Make a struct that contains a payroll entity (MAX appears nowhere in this declaration), then create an array of MAX length of those; PAYROLL emp[MAX]; In other words, don't use a struct with array members (name and pin not withstanding); use an array of structs. Does that make sense? Commented Jul 21, 2014 at 20:01
  • 1
    You made a typo in the first for loop. Replace <= with <. Commented Jul 21, 2014 at 20:13
  • I did end up figuring it out going back to my earlier attempts, and realizing my for statements didn't match one was <=MAX other was <MAX.I realize now you name the overall structure as a structure and not each element. struct name{int dec float number char [maxlength]}; Then typedef statement then in main and elsewhere you declare the array [MAX]. In my case would have been PAYROLL employee[MAX]; Is this right, it did run properly. Commented Jul 21, 2014 at 20:30
  • ahhh these comments just showed for me, thank you Commented Jul 21, 2014 at 20:31
  • other mistake I just found was wrong operator as I should of multiplied hours by hourly_pay, noticed this one when it finally ran. Commented Jul 21, 2014 at 20:36

1 Answer 1

3

You want the structure to represent a single instance of an employee, and then have an array of those structures. So your type definitions would be

#include <stdio.h>
#include <stdlib.h>

#define MAX 10

typedef struct PAYROLL
{
 char name [21];
 char pin [7];
 float hours;
 float hourly_pay;
 float gross_pay;
 float net_pay;
 float overtime_pay;
 float deductions;
} payroll;

And inside your main function, change your variable declaration to

PAYROLL employee[MAX];

Then, wherever you have employee.<field_name>[i] you would change it to employee[i].<field_name>

And finally, you'll have to change the definition of funcoutput

void funcoutput (PAYROLL employee1[])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.