Code:
#include<stdio.h>
#include<stdlib.h>
void NewPage(FILE, float, float, float, int);
float CalcGross(float, float);
float CalcFICA(float, float);
int main(){
FILE *inFile;
FILE *outFile;
char resp = 'y';
char fileInName[30];
char fileOutName[30];
float ficaTax;
#define ficaRate .6078
while(resp == 'Y' || resp == 'y'){
printf("Please enter the file you'd like to read from: ");
scanf("%s", fileInName);
if((fopen(fileInName, "r")) == NULL){
printf("Could not open requested file. Would you like to enter another file? [Y/N]: ");
scanf("%c", &resp);
}
inFile = fopen(fileInName, "r");
resp = 'n';
}
while(resp != 'Y' && resp != 'y'){
printf("Please enter the file you'd like to write to: ");
scanf("%s", fileOutName);
if(fopen(fileOutName, "r") != NULL){
printf("The file you are attempting to write to already exists! Overwrite?? [Y/N]: ");
scanf("%*c %c", &resp);
if(resp == 'Y' || resp == 'y')
outFile = fopen(fileOutName, "w");
}
resp = 'y';
}
if(fopen(fileOutName, "r") == NULL)
outFile = fopen(fileOutName, "w");
int PK = 1, LK = 0;
int maxLines = 20;
float pageGross = 0.00, pageFICA = 0.00, pageNet = 0.00;
float totalGross = 0.00, totalFICA = 0.00, totalNet = 0.00;
if (PK == 1){
fprintf(outFile, "\t\t\t\t\t<Company Name>\n\n\n\t\t\t\t\t<Tax Reports>\n\n");
fprintf(outFile, "---------------------------------------------------------------------------\n");
}
int empNum;
char firstName[15], lastName[15], dept[7];
float ytd, payrate, hrsWkd;
while(fscanf(inFile, "%4d%s%s%s%f%f%f", &empNum, firstName, lastName, dept, &ytd, &payrate, &hrsWkd) != EOF){
//fprintf(outFile, "\tEmployee number: %d\tName: %s %s\tDept: %s\tYTD: %.2f\tPayrate: %.2f\tHours: %f\n", empNum, firstName, lastName, dept, ytd, payrate, hrsWkd);
float gross = CalcGross(hrsWkd, payrate);
float fica = CalcFICA(ytd, gross);
float net = ((ytd + gross) - fica);
if(LK<maxLines){
fprintf(outFile, "Emp #: %d\tName: %s %s\tDept: %s\t YTD: %.2f\tGross: %.2f\tFICA: %.2f\tNet: %.2f\t\n", empNum, firstName, lastName, dept, ytd, gross, fica, net);
LK++;
pageGross += gross;
pageFICA += fica;
pageNet += net;
}
else NewPage(*outFile, pageGross, pageFICA, pageNet, PK);
}
}
float CalcGross(float hrsWkd, float payRt)
{
float gross;
if(hrsWkd <= 40)
gross = (hrsWkd * payRt);
else gross = (hrsWkd * (payRt * 1.5));
return gross;
}
float CalcFICA(float ytd, float gross)
{
float ficaTax;
if(ytd >= 67000.00)
ficaTax = 0.00;
else if ((ytd + gross) <= 67000.00)
ficaTax = (gross * ficaRate);
else ficaTax = (67000.00 - ytd) * ficaRate;
return ficaTax;
}
void NewPage(FILE *outToFile, float pageGross, float pageFICA, float pageNet, int pageCount)
{
fprintf(outToFile, "Page Totals: \t\t\t\t\t%.2f\t%.2f\t%.2f", pageGross, pageFICA, pageNet);
fprintf(outToFile, "\n\t\t\t\t\t\t\t\t\t\tPage: %d", pageCount);
fprintf(outToFile, "------------------------------------PAGE BREAK---------------------------------------");
/*float totalGross += pageGross;
float totalFICA += pageFICA;
float totalNet += pageNet;
PK++;
LK = 0;
pageGross = 0.00;
pageFICA = 0.00;
pageNet = 0.00;*/
}