2014-03-29 65 views
0

我創建一個工資計算程序,這裏是我的代碼變量或字段宣告無效」和其他錯誤

#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宣佈無效

然後我也得到: 在功能voidPrintSummaryReport ...... 不能將文件轉換爲const車參數爲int的printf(爲const char * ...) 是以下行

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

你的函數調用是不正確 – mangusta

回答

0

do塊,你有這樣的語句:

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

取出void關鍵字,因爲你在那裏調用該函數。因爲錯誤說,我認爲你不能使用printfFILE*fprintf應該沒問題。您可以查看printf等的詳細信息。here。請注意,這是針對Linux的,但它也適用於在Windows上使用C。

0

錯誤:無關的「void」;失蹤 「;」:

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); 
+0

太謝謝你了!我的大部分錯誤都已修復,但現在我只剩下一個了。 http://puu.sh/7NHU5.png (只是第一個錯誤,第二個錯誤是由於你的幫助而修復的,至少我沒有收到錯誤!還沒有能夠編譯看到如果一切正常) – user3475151

+0

1)「在此輸入代碼」應在評論中。 EX:'/ *在這裏輸入代碼* /'2)你不能*用「reportFile」(一個FILE *指針)調用printf()。將它們更改爲「fprintf()」,或者刪除「reportFile」參數。 – FoggyDay