变量或字段声明无效"和其他错误

时间:2014-03-29 06:51:37

标签: c

我正在创建一个工资单程序,这是我的代码

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>  

    #define REPORTHEADING1 "     Employee              Pay      Hours     Gross     Tax            Net\n"
    #define REPORTHEADING2 "     Name                  Rate     Worked    Pay       Due       Pay\n"
    #define REPORTHEADING3 "     ===============       ====     ======    =====     ====      ====\n"
    #define REPORTHEADING4 "                           ====     ======    =====     ====      ====\n"
    #define REPORTLINEFORMAT1 "     %-20s%6.2f%11.2f%9.2f%9.2f%10.2f\n"
    #define REPORTLINEFORMAT2 "     Totals              %6.2f%11.2f%9.2f%9.2f%10.2f\n"
    #define REPORTLINEFORMAT3 "     Averages            %6.2f%11.2f%9.2f%9.2f%10.2f\n"

    #define COUNTLINEFORMAT "     Number of employees: %-10i\n\n"

    #define MAXREGHOURS 40
    #define OVERTIMERATE 1.5


    enter code here
    void PrintReportHeadings(FILE *reportFile); //printReportHeadings prototype

    void InitializeAccumulators(float *totRegHour,float *totOvtHours,float *totPayrate, 
        float *totGross,float *totdeferred,float *totFedtax,
        float *totStatetax,float *totSSItax,float *totNet,int *empCount);      //InitializeAccumulators prototype

    void InputEmployeeData(char *firstName,char *lastName,
            float *hours,float *payrate,float *deferred); //InputEmployeeData   prototype

    void CalculateGross(float hours,float payrate,float *regHours,float *ovtHours,
            float *gross); //CalculateGross prototype

    extern void CalculateTaxes(float gross,float deferred,float * fedtax,
                float * statetax,float * ssitax); //CalculateTaxes prototype    (external)

    float CalculateNetPay(float gross,float fedtax,float statetax,float ssitax,
                float deferred);

    void AddDetailToAccumulators(float regHours,float ovtHours,float payrate,
        float gross,float deferred,float fedtax,float statetax,
        float ssitax,float net,float *totRegHours,float *totOvtHours,
        float *totPayrate,float *totGross,float *totdeferred, 
        float *totFedtax,float *totStatetax,float *totSSItax,
        float *totNet);

    void PrintSummaryReport(FILE *reportFile,char fullName,float regHours,float ovtHours,
            float payrate,float gross,float deferred,float fedtax,
            float statetax,float ssitax,float net);

    int main(void)
     {
    float ft,st,ssit;
    char firstName[10+1];
    char lastName[15+1];
    char fullName[25+1];
    float regHours, ovtHours, hours, payrate, deferred, gross, netpay;
    float totRegHours, totOvtHours, totPayrate, totGross,totdeferred, 
        totFedtax, totStatetax, totSSItax, totNet;
    int empcount;
    char answer;
    FILE * reportFile;

    reportFile = fopen("./report.txt","wt");
    if(reportFile = NULL) 
    {
        printf(" Report open request failed...\n");
        while(getchar() != '\n');
        exit(-90);// reqs <stdlib.h>
    }

    PrintReportHeadings(reportFile);

    InitializeAccumulators(&totRegHours,&totOvtHours,&totPayrate,&totGross,
        &totdeferred,&totFedtax,&totStatetax,&totSSItax,&totNet,
        &empcount);//set all accumulators to 0

    do
    {
    InputEmployeeData(firstName,lastName,&hours,&payrate,&deferred);
    CalculateGross(hours, payrate, &regHours, &ovtHours, &gross);
    CalculateTaxes(gross,deferred,&ft,&st,&ssit);
    netpay = CalculateNetPay(gross,ft,st,ssit,deferred);
    strcpy(fullName,lastName);
    strcat(fullName,", ");
    strcat(fullName,firstName);

    AddDetailToAccumulators(regHours,ovtHours,payrate,gross,deferred,ft,st,
        ssit,netpay,&totRegHours,&totOvtHours,&totPayrate,&totGross,
        &totdeferred,&totFedtax,&totStatetax,&totSSItax,&totNet);

    void PrintSummaryReport(reportFile,fullName,regHours,ovtHours,payrate,gross,deferred,
            ft,st,ssit,netpay)

    empcount++;
    printf(COUNTLINEFORMAT,empcount);

    printf("  do you have anymore? (Y/N): ");
    while(getchar() != '\n');
    answer = getchar();
    printf("\n");

    }
    while(answer != 'N' && answer != 'n');

    while (getchar()!= '\n');
    getchar();
    return 0;
}

void PrintReportHeadings(FILE *reportFile)
{
        reportFile = fopen("./report.txt","wt");
        fprintf(reportFile,REPORTHEADING1);
        fprintf(reportFile,REPORTHEADING2);
        fprintf(reportFile,REPORTHEADING3);
}


void InitializeAccumulators(float *totRegHour,float *totOvtHours,float *totPayrate,
                float *totGross,float *totdeferred,float *totFedtax,
                float *totStatetax,float *totSSItax,float *totNet,int *empCount)
{
        totRegHour, totOvtHours, totPayrate, totGross,totdeferred,
                totFedtax, totStatetax, totSSItax, totNet, empCount = 0;
}

void InputEmployeeData(char *firstName,char *lastName,float *hours,
            float *payrate,float *deferred)
{
    printf("  Enter employee first name : ");
    scanf("%s",firstName);
    printf("  Enter employee last name : ");
    scanf("%s",lastName);
    printf("  Enter %s's hours worked : ",firstName);
    scanf("%f",hours);
    printf("  Enter %s's pay rate : ",firstName);
    scanf("%f",payrate);
    printf("  Enter %s's amount deferred : ",firstName);
    scanf("%f",deferred);
}

void CalculateGross(float hours,float payrate,float *regHours,float *ovtHours,float *gross)
{
float overtimeHours(float hours);

    if(hours <= MAXREGHOURS)
    {
        *regHours = hours;
        *gross = hours * payrate;
    }
    else
    {
        *regHours = MAXREGHOURS;
        *ovtHours = overtimeHours(hours);
        *gross = payrate * MAXREGHOURS + OVERTIMERATE * payrate * (hours - MAXREGHOURS);
    }
}

float overtimeHours(float hours)
{
    return hours - MAXREGHOURS;
}
float CalculateNetPay(float gross,float fedtax,float statetax,float ssitax,
        float deferred)
{
    return gross - (fedtax + statetax + ssitax + deferred);
}
void AddDetailtoAccumulators(float regHours,float ovtHours,float payrate,
                float gross,float deferred,float fedtax,float statetax,
                float ssitax,float netpay,float *totRegHours,float *totOvtHours,
                float *totPayrate,float *totGross,float *totDeferred,
                float *totFedtax,float *totStatetax,float *totSSItax,
                float *totNet)
{
    *totRegHours =+ regHours;
    *totOvtHours =+ ovtHours;
    *totPayrate =+ payrate;
    *totGross =+ gross;
    *totDeferred =+ deferred;
    *totFedtax =+ fedtax;
    *totStatetax =+ statetax;
    *totSSItax =+ ssitax;
    *totNet =+ netpay;
}

void PrintSummaryReport(FILE *reportFile,char fullName,float regHours,float ovtHours,
                        float payrate,float gross,float deferred,float fedtax,
                        float statetax,float ssitax,float netpay)
{
    reportFile = fopen("./report.txt","wt");

    fprintf(reportFile,REPORTLINEFORMAT1,fullName,payrate,regHours,gross,fedtax,
        ssitax,netpay);
    fprintf(reportFile,REPORTLINEFORMAT2,ovtHours,statetax,deferred);

    printf(reportFile,REPORTLINEFORMAT1,fullName,payrate,regHours,gross,fedtax,
        ssitax,netpay);
    printf(reportFile,REPORTLINEFORMAT2,ovtHours,statetax,deferred);
}

我得到的错误是: 变量或字段PrintSummaryReport声明为void

然后我也得到: 在函数voidPrintSummaryReport中... 不能将FILE转换为const car for int printf(const char * ...) 这是以下几行

printf(reportFile,REPORTLINEFORMAT1,fullName,payrate,regHours,gross,fedtax,
        ssitax,netpay);
    printf(reportFile,REPORTLINEFORMAT2,ovtHours,statetax,deferred);

2 个答案:

答案 0 :(得分:0)

do区块中,您有以下声明:

void PrintSummaryReport(reportFile,fullName,regHours,ovtHours,payrate,gross,deferred,
        ft,st,ssit,netpay)

删除void关键字,因为您在那里调用该函数。

然后,关于printf中的错误,我认为您不能将printfFILE*一起使用,如错误所示。 fprintf应该没问题。您可以查看printfhere的详细信息。请注意,这适用于Linux,但它也适用于在Windows上使用C.

答案 1 :(得分:0)

错误:无关的“无效”;缺少“;”:

   void PrintSummaryReport(reportFile,fullName,regHours,ovtHours,payrate,gross,deferred,
            ft,st,ssit,netpay)

正确:

   PrintSummaryReport(reportFile,fullName,regHours,ovtHours,payrate,gross,deferred,
            ft,st,ssit,netpay);

问:为什么要在main()中打开“reportFile”...然后在“PrintReportHeading()”中再次打开它?又在“PrintSummaryReport()”中?只需打开一次,然后在任何需要的地方使用它。

最后:

错误:“printf()”不接受FILE *参数; “fprintf()”确实:

printf(reportFile,REPORTLINEFORMAT1,fullName,payrate,regHours,gross,fedtax,
    ssitax,netpay);
printf(reportFile,REPORTLINEFORMAT2,ovtHours,statetax,deferred);

正确:

fprintf(reportFile,REPORTLINEFORMAT1,fullName,payrate,regHours,gross,fedtax,
    ssitax,netpay);
fprintf(reportFile,REPORTLINEFORMAT2,ovtHours,statetax,deferred);