Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // include for defintion of strcmp(), etc..
#define SIZE 5 // why define SIZE here, then use "5" everywhere you should put "SIZE"???
int main(void /* in C put void as parameter when function expects no parameters */)
{
//variables
// should define constant for MAX_EMPLOYEE_NAME_LEN or some such
float rate[5], hours[5]; // changed type "double" to "float", due to a bug when reading via "%f" printf format. If want to use double, use different format specificer!
float overtimerate = 1.5; // should define a constant for this, as it is NOT a variable that can change!
char name[5][20] = { 0 } ;
float grosspay[5], overtimepay[5], basepay[5], taxpay[5], netpay[5];
int num_employees = 0;
// take employee data input
for (int loop = 0; loop < 5; loop++) // use loop-local variable in "for" loop, also dont name your variable "loop"
{
//use if to break out of loop after input if -1 is entered
printf("Enter your name:\n");
scanf("%20s", &name[loop]);
if (strcmp(name[loop], "-1") == 0) // name is the array of name strings, name[loop] is the current name string
break;
// removed the check for "-1" being entered for pay rate and # of hours worked, since it doesnt make
// any sense to not enter these once the user is already entering an employee's info!
// In other words, it should be a mandatory input. If not, you can re-write this such that -1 entered for
// any value will abort the current employee data entry and continue to the next, or end input entirely...
printf("Enter your hourly rate:\n");
scanf("%f", &rate[loop]);
printf("Enter number of hours worked:\n");
scanf("%f", &hours[loop]);
++num_employees;
} // yeah, you missed that bracket!
//math calculations to calculate pay and taxes
//differentiate between overtime and non overtime pay
// I am sure all this math can be simplified a great deal.. I will leave that to you.
for (int loop = 0; loop < num_employees; loop++) { // only calculate for employees actually entered by user
if (hours[loop] > 40 /* again, define a constant MAX_NORMAL_PAY_HOURS or some such */) {
basepay[loop] = rate[loop] * 40;
overtimepay[loop] = (hours[loop] - 40)*(overtimerate*rate[loop]);
grosspay[loop] = ((hours[loop] - 40)*(rate[loop] * overtimerate)) + (40 * rate[loop]);
taxpay[loop] = .2*((hours[loop] - 40)*(rate[loop] * overtimerate) + (40 * rate[loop]));
netpay[loop] = (hours[loop] - 40)*(rate[loop] * overtimerate) + (40 * rate[loop]) - (.2*((hours[loop] - 40)*(rate[loop] * overtimerate) + (40 * rate[loop])));
}
else {
basepay[loop] = rate[loop] * hours[loop];
taxpay[loop] = .2*(rate[loop] * hours[loop]); // again, define 0.20 as a TAX_RATE constant or some such..
netpay[loop] = (rate[loop] * hours[loop]) - (.2*(rate[loop] * hours[loop]));
overtimepay[loop] = 0.0; // dont forget to initialize this, since it is printed later!
// whoops, forget to set grosspay? Also, whats the difference b/w base pay and gross pay here?
// whoops, forget to set anything else? How about calculate employee's non-overtime pay in all cases, then adjust for overtime in case hours > 40?
}
}
//output
for (int loop = 0; loop < num_employees; loop++) { // only do output for employees actually entered by user
printf("Employee: %s\n", name[loop]);
printf("Hourly rate: %.2f\n", rate[loop]);
printf("Hours worked: %.2f\n", hours[loop]);
printf("Base pay: %.2f\n", basepay[loop]);
printf("Overtime pay: %.2f\n", overtimepay[loop]);
printf("Your paycheck is: %.2f\n", grosspay[loop]);
printf("Taxes paid:%.2f\n", taxpay[loop]);
printf("Money taken home: %.2f\n", netpay[loop]);
} // whoops, had this bracket in the wrong spot again -- always printed last employee's data!
//system("pause"); // never use this!
return 0;
}
I have taken some time to go over the code and fix it to some extent, and I added comments for explanation. I did find quite a few bugs in here, not all of which are fixed, but as it was, the code did not even compile and there were some gross logical errors (which is OK and expected for a beginner), but also I found some more subtle bugs (such as the wrong usage of printf formatting specifiers). I hope this will put you on the right track..