C++ Hi! I\'m having a problem with printing the totals and averages. It gives me
ID: 3584835 • Letter: C
Question
C++
Hi! I'm having a problem with printing the totals and averages. It gives me strange numbers like below. Thank you!
Employee Pay Hours Gross Tax Net
Name Rate Worked Pay Due Pay
================== ======== ====== ===== ===== =====
df, dfg 324.00 1.00 324.00 48.60 275.40
wer, ffd 33.00 3.00 99.00 14.85 84.15
Totals 357.0034142318583644890000000000000000.00 423.00 63.4534142243630244073000000000000000.00
Averages 178.5017071159291822445000000000000000.00 211.50 31.7217071121815122037000000000000000.00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ADDR(var) &var
#define ReportHeading1 " Employee Pay Hours Gross Tax Net "
#define ReportHeading2 " Name Rate Worked Pay Due Pay "
#define ReportHeading3 " ================== ======== ====== ===== ===== ===== "
#define ReportHeading4 " Totals%8.2f%10.2f%10.2f%10.2f%10.2f "
#define ReportHeading5 " Averages%8.2f%10.2f%10.2f%10.2f%10.2f "
#define FORMAT " %-23s%8.2f%10.2f%10.2f%10.2f%10.2f "
#define Taxrate 0.15
int main(void)
{
char FullName[30+1];
char LastName[20+1];
char FirstName[10+1];
float Payrate,Hours,Gross;
float Tax,Net;
float TotalRate,TotalHours,TotalGross,TotalTax,TotalNet;
float avgRate,avgHours,avgGross,avgTax,avgNet;
char answer;
int empCount;
FILE * reportFile;
reportFile = fopen("c:\Assignment1 eport.txt","wt");
if (reportFile == NULL)
{
printf("The open request failed ... terminating application ");
printf("Press any key to end...");
fflush(stdin),getchar();
exit(-30);
}
printf(ReportHeading1);
printf(ReportHeading2);
printf(ReportHeading3);
fprintf(reportFile,ReportHeading1);
fprintf(reportFile,ReportHeading2);
fprintf(reportFile,ReportHeading3);
empCount = 0;
do
{
printf("Enter employee's first name ");
scanf("%s",FirstName);
printf("Enter employee's last name ");
scanf("%s",LastName);
printf("Enter hourly payrate ");
scanf("%f",ADDR(Payrate));
printf("Enter hours worked this pay period ");
scanf("%f",ADDR(Hours));
if (Hours <= 40)
Gross = (Hours * Payrate);
else
Gross = (40 * Payrate)+(1.5*Payrate*(Hours-40));
Tax= Gross * Taxrate;
Net= Gross - Tax;
strcpy(FullName,LastName);
strcat(FullName,", ");
strcat(FullName,FirstName);
printf(FORMAT,FullName,Payrate,Hours,Gross,Tax,Net);
fprintf(reportFile,FORMAT,FullName,Payrate,Hours,Gross,Tax,Net);
empCount = empCount + 1;
TotalRate+=Payrate;
TotalHours+=Hours;
TotalGross+=Gross;
TotalTax+=Tax;
TotalNet+=Net;
printf("Do you have another employee(Y/N)? ");
fflush(stdin);
scanf("%c",ADDR(answer));
} while (answer == 'Y' || answer == 'y');
avgRate = TotalRate / empCount;
avgHours = TotalHours / empCount;
avgGross = TotalGross / empCount;
avgTax = TotalTax / empCount;
avgNet = TotalNet / empCount;
printf(FORMAT,"Total",TotalRate,TotalHours,TotalGross,TotalTax,TotalNet);
fprintf(reportFile,FORMAT,"Totals",TotalRate,TotalHours,TotalGross,TotalTax,TotalNet);
printf(FORMAT,"Averages",avgRate,avgHours,avgGross,avgTax,avgNet);
fprintf(reportFile,FORMAT,"Averages",avgRate,avgHours,avgGross,avgTax,avgNet);
fclose(reportFile);
printf("You processed %d employees ",empCount);
fflush(stdin);
getchar();
return 0;
}
Explanation / Answer
Instead of using FORMAT to print the total values and average values, use ReportHeading4 and ReportHeading5 defined earlier in the program. Even if that doesn't work try print the TOTAL and AVERAGE without any formatting and check the result. Let me know if that doesn't work for you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.