OK it seems to me I have a problem with reading the file.
I hard coded some values to be passed into the calc_gross_to_pay() function and when I output to file it looks like this:
As you can see the data in the Emp# column is from the first line of the data file that I was trying to read. Is there anything I can do to fix the problem in reading the data?
main()
{
FILE *in;
FILE *out;
double payRates[NUMRATES];
int i, iMax = 0, iTotEmp;
char empNum[5];
char lName[13];
char fName[13];
char lineC[50];
int pGrp;
double hrsWorked;
double grossPay, LTB, EI, CPP, FT, ded, netPay, junk, tNetPay, tGrossPay;
get_PR(payRates);
in = fopen("employees.dat", "r");
if(in == NULL)
{
printf("Error! Cant find the file employees.dat!\n");
exit(1);
}
fgets(lineC, sizeof(lineC) - 1 ,in);
while (! feof(in))
{
iMax++;
fgets(lineC, sizeof(lineC) - 1 ,in);
}
rewind(in);
out = fopen("payroll.dat", "w");
if(in == NULL)
{
printf("Error! Cant find the file payroll.dat!\n");
exit(1);
}
else
{
fprintf(out, "Emp# Last Name First Name Gross Pay LTB EI CPP FT Deductions Net Pay\n");
}
for(i = 0; i<iMax;i++)
{
fscanf(in, "%4[^:]:%12[ ^:]:%12[ ^:]:%d:%lf\n", empNum, lName, fName, &pGrp, &hrsWorked);
grossPay = calc_gross_ot_pay(PRATE, HRSWRKED);
//PRATE and HRSWRKED are defined using #define
LTB = calc_LTB(grossPay);
EI = calc_EI(grossPay);
CPP = calc_CPP(grossPay);
FT = calc_FT(grossPay);
ded = LTB + EI + CPP + FT;
netPay = grossPay - ded;
tNetPay += netPay;
tGrossPay +=grossPay;
fprintf(out, "%-5s %-13s %-13s %11.2lf %11.2lf %11.2lf %11.2lf %11.2lf %11.2lf %11.2lf\n", empNum, lName, fName, grossPay, LTB, EI, CPP, FT, ded, netPay);
}
printf("# of employees: %d Total Net Pay %lf Total Gross Pay %lf", iMax, tNetPay, tGrossPay);
fclose(out);
fclose(in);
return 1;
}