简体   繁体   English

我在通过C中的地址传递时无法取平均值

[英]I'm having trouble taking averages while passing by address in C

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). 在功能3.6中,我在计算我的总薪水(pr)和我的雇员总数(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. 修复这些错误后,我的输出为0。我对编程很陌生,所以在尝试回答任何问题时请多多包涵。 The function I am having trouble with is the second to last function in the code(3.6 AddDetailToAccumulator). 我遇到的功能是代码(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,&reg,&reghours,&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 = empCount +1; empCount = empCount +1;

and here 和这里

*avgpr = *pr / empCount ; * avgpr = * pr / empCount;

As the previous answer pointed out, you were missing the * operator. 正如前面的答案指出的那样,您缺少*运算符。 This is known as the dereference operator. 这称为dereference运算符。

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. 换句话说,如果指针的值具有地址0x01 ,则上一行将访问内存位置0x01处的整数并将其加1。 The original code would just increment the pointer to the next integer (Would increment to 0x05 for a 32-bit integer). 原始代码只会将指针递增到下一个整数(对于32位整数,将递增到0x05 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM