So below is the main portion of my code. There are a few external files that I am working with. In function 3.6, I am having trouble taking the average of my (in this case) total payrate(pr) and my total employees(empCount). Is there something that I am missing here? When I compile it, I get an error that says invalid operands. When I fix those, my output is 0. I am very new with programming, so bear with me while I try to answer any questions. The function I am having trouble with is the second to last function in the code(3.6 AddDetailToAccumulator). Thanks much.
#include <stdio.h>
#include <stdlib.h>
#include "EmployeeRecord.h"
#include "CalcTaxes.o"
#define ADDR(var) &var
#define REPORTCOLUMNHEADINGS1 "Employee Pay Reg Hrs Gross Fed SSI Net\n"
#define REPORTCOLUMNHEADINGS2 "Name Rate OVT Hrs Pay State Defr Pay\n"
#define BARS "======== ===== ======= ======= ====== ====== =======\n\n"
#define REPORTCOLUMN1 "%s, %s\t%8.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n"
#define REPORTCOLUMN2 "%32.2f%18.2f%8.2f\n\n"
void PrintReportHeadings(FILE * ReportFile); //3.1
void InputEmployeeData(int count,char * lastname,char * firstname,float * hours,float * payrate,float * defr); //3.3
float CalculateGross(float hours, float payrate, float reghours, float ovthours); //3.4 CalculateGross
extern void CalculateTaxes(float gross, float defr, float *ft, float *st, float *ssit); //3.5 CalculateTaxes
void AddDetailToAccumulator(int count,int *empCount,float *pr,float *payrate,float *reg,float *reghours,float *ovt,float *ovthours,float *gp,
float *gross,float *fedt,float *ft,float *stt,float *st,float *sst,float *ssit,float *def,
float *defr,float *np,float *net,float *avgpr);//3.6 AddDetailToAccumulator
void PrintSummaryReport(float pr,float reg,float ovt,float gp,float fedt,float stt,float sst,float def,float np,float *avgpr,float avgreg,float avgovt,
float avggp,float avgfedt,float avgstt,float avgsst,float avgdef,float avgnp,FILE * ReportFile); //3.7
int main()
{
EmployeeRecord r; // Call Employee Record Definitions
float reghours,ovthours;
float ft,st,ssit;
float pr,reg,ovt,gp,fedt,stt,sst,def,np;
float avgpr,avgreg,avgovt,avggp,avgfedt,avgstt,avgsst,avgdef,avgnp;
char answer;
int empCount,count;
FILE * ReportFile;
PrintReportHeadings(ReportFile); //Call 3.1 PrintReportHeadings
empCount = 0;// count initializations
pr = reg = ovt = gp = fedt = stt = def = sst = np = 0;
avgpr = avgreg = avgovt = avggp = avgfedt = avgstt = avgsst = avgdef = avgnp = 0;
do
{
InputEmployeeData(count,r.firstname,r.lastname,&r.payrate,&r.defr,&r.hours);//Call 3.3 InputEmployeeData
if (r.hours > 40)//Check for Overtime Hours
{
reghours = 40;
ovthours = r.hours - 40;
}
else {
reghours = r.hours;
ovthours = 0;
}
r.gross = CalculateGross(r.hours,r.payrate,reghours,ovthours); //3.4 CalculateGross
CalculateTaxes(r.gross,r.defr,&ft,&st,&ssit);//Call 3.5 CalculateTaxes
r.net = r.gross - ft - st - ssit;// Calculate Net Earnings
printf(REPORTCOLUMNHEADINGS1);
printf(REPORTCOLUMNHEADINGS2);
printf(BARS);
printf(REPORTCOLUMN1,r.lastname,r.firstname,r.payrate,reghours,r.gross,ft,ssit,r.net);
printf(REPORTCOLUMN2,ovthours,st,r.defr);
ReportFile = fopen("report.txt", "a");
fprintf(ReportFile,REPORTCOLUMN1,r.lastname,r.firstname,r.payrate,reghours,r.gross,ft,ssit,r.net);
fprintf(ReportFile,REPORTCOLUMN2,ovthours,st,r.defr);
fclose(ReportFile);
AddDetailToAccumulator(count,&empCount,&pr,&r.payrate,®,®hours,&ovt,&ovthours,&gp,&r.gross,&fedt,&ft,&stt,&st,&sst,&ssit,
&def,&r.defr,&np,&r.net,&avgpr);//3.6
while (getchar() != '\n');
printf(" Repeat (Y/N)? : ");
scanf("%c",ADDR(answer));
} while (answer == 'Y' || answer == 'y');
printf("\n"); // print one line for spacing
printf("\nTotals %17.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",pr,reg,gp,fedt,sst,np);
printf("%32.2f%18.2f%8.2f\n",ovt,stt,def);
printf("%d",count);
printf("\nAverages %15.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",avgpr,avgreg,avggp,avgfedt,avgstt,avgnp);
printf("%32.2f%18.2f%8.2f\n",avgovt,avgsst,avgdef);
PrintSummaryReport(pr,reg,ovt,gp,fedt,stt,sst,def,np,&avgpr,avgreg,avgovt,
avggp,avgfedt,avgstt,avgsst,avgdef,avgnp,ReportFile); //Call 3.7 PrintSummaryReport
fflush(stdin);
getchar();
return 0;
}
void PrintReportHeadings(FILE * ReportFile) //3.1
{
ReportFile = fopen("report.txt", "w");
fprintf(ReportFile,REPORTCOLUMNHEADINGS1);
fprintf(ReportFile,REPORTCOLUMNHEADINGS2);
fprintf(ReportFile,BARS);
fclose(ReportFile);
}
void InputEmployeeData(int count,char * lastname,char * firstname, float * payrate, float * defr, float * hours) //3.3
{
printf("Enter employee's name: "); // input section
scanf("%s%s",firstname,lastname);
printf("Enter hourly pay rate: ");
scanf("%f",payrate);
printf("Enter deferred amount: ");
scanf("%f",defr);
printf("Enter hours worked this pay period: ");
scanf("%f",hours);
}
float CalculateGross(float hours, float payrate, float reghours, float ovthours) // 3.4
{
return (reghours * payrate)+(ovthours * payrate * 1.5);
}
void AddDetailToAccumulator(int *empCount,float *pr,float *payrate,float *reg,float *reghours,float *ovt,float *ovthours,float *gp,
float *gross,float *fedt,float *ft,float *stt,float *st,float *sst,float *ssit,float *def,
float *defr,float *np,float *net,float *avgpr)//3.6
{
empCount = empCount +1;
*pr = *pr + *payrate;
*reg = *reg + *reghours;
*ovt = *ovt + *ovthours;
*gp = *gp + *gross;
*fedt = *fedt + *ft;
*stt = *stt + *st;
*sst = *sst + *ssit;
*def = *def + *defr;
*np = *np + *net;
*avgpr = *pr / empCount;
}
void PrintSummaryReport(float pr,float reg,float ovt,float gp,float fedt,float stt,float sst,float def,float np,float * avgpr,float avgreg,float avgovt,
float avggp,float avgfedt,float avgstt,float avgsst,float avgdef,float avgnp,FILE * ReportFile) //3.7
{
ReportFile = fopen("report.txt", "a");
fprintf(ReportFile,"\nTotals %17.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",pr,reg,gp,fedt,sst,np);
fprintf(ReportFile,"%32.2f%18.2f%8.2f\n",ovt,stt,def);
fprintf(ReportFile,"\nAverages %15.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",&avgpr,avgreg,avggp,avgfedt,avgsst,avgnp);
fprintf(ReportFile,"%32.2f%18.2f%8.2f\n",avgovt,avgstt,avgdef);
fclose(ReportFile);
}
You're missing the * on the empcount variable here:
empCount = empCount +1;
and here
*avgpr = *pr / empCount ;
As the previous answer pointed out, you were missing the *
operator. This is known as the dereference
operator.
What you were doing:
empCount = empCount +1;
// or
empCount++; // Same assignment
Here, you were performing pointer arithmetic . This assignment is advancing the pointer to the next integer.
What you needed to do:
(*empcount)++;
Dereferencing the pointer will increment the value that the pointer points to. In other words, if the value of the pointer had address 0x01 , the above line would access the integer at memory location 0x01 and add one to it. The original code would just increment the pointer to the next integer (Would increment to 0x05 for a 32-bit integer).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.