2015-08-15 126 views
-2

這是我的代碼:結構和功能

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

FILE* openRead(char fName[]); 
FILE* openWrite(char fName[]); 
int setArray(FILE *fp, struct salesPerson sRec[]); 
void calcPay(struct salesPerson sRec[], float rate[], int recNum); 
void writePay(struct salesPerson sRec[], int recNum, FILE *filpay); 

main() { 
    float rate[5] = {0.02, 0.04, 0.06, 0.08, 0.10}; 
    int recNum=0; 
    struct salesPerson salesR[1000]; 
    FILE *filepsales, *fileppay; 
    filepsales=openRead("sales.dat"); 
    fileppay=openWrite("pay.txt"); 
    recNum=setArray(filepsales,salesR); 
    calcPay(salesR,rate,recNum); 
    writePay(salesR,recNum,fileppay); 
    fclose(filepsales); 
    fclose(fileppay); 

    return 0; 
} 

FILE* openRead(char fName[]){ 
    FILE *filepsales2=NULL; 
    filepsales2= fopen(fName, "r"); 
    if (filepsales2 == NULL) { 
     printf("Cannot open %s \n",fName); 
     exit(1); 
    } 
    printf("File: %s, opened",fName); 
    return filepsales2; 
} 

FILE* openWrite(char fName[]){ 
    FILE *fpPay=NULL; 
    fpPay= fopen(fName, "w"); 
    if (fpPay == NULL) { 
     printf("Cannot open %s , Exiting Program \n",fName); 
     exit(1); 
    } 
    printf("File: %s, opened\n",fName); 
    return fpPay; 
} 

int setArray(FILE *fp, struct salesPerson sRec[]) { 

    int j=0; 
    while(fscanf(fp,"%d:%[^:]:%[^:]:%d:%lf%*c",&sRec[j].salesNum, sRec[j].lastName, 
       sRec[j].firstName,&sRec[j].salesRate, &sRec[j].salesAmount)==5) 
     j++; 
    return j; 
} 

void calcPay(struct salesPerson sRec[], float rate[], int recNum) { 
    int j; 
    for(j = 0;j < recNum; j++){ 
     sRec[j].salesPay = 200 + (sRec[j].salesAmount * rate[sRec[j].salesRate]); 
    } 
} 

void writePay(struct salesPerson salesR[], int recNum, FILE *filep){ 
    int incriment; 
    fprintf(filep,"        Weekly Payment Report\n"); 
    fprintf(filep,"--------------------------------------------------------------------------------\n"); 
    fprintf(filep,"| SNum | Last Name   | First Name   | Lvl | Sales Amt | Week Pay |\n"); 
    fprintf(filep,"================================================================================\n"); 
    for(incriment= 0 ; incriment < recNum ; incriment++){ 
     fprintf(filep,"--------------------------------------------------------------------------------\n"); 
    } 
} 

而且我得到象這樣的錯誤:

C:\Users\billy\Desktop\New folder\Assignment3.c|33|warning: 'struct salesPerson' declared inside parameter list [enabled by default]| i dont understand whats happening?

C:\Users\billy\Desktop\New folder\Assignment3(1).c|43|error: type of formal parameter 2 is incomplete|

+2

看起來'struct salesPerson'在使用前沒有定義任何地方。 –

+1

我懷疑「salesPerson」中有一個拼寫錯誤,但無法確切地知道「A3sale.h」 – user3386109

+0

「A3sale.h」有什麼? –

回答

0

你的第一個錯誤struct內部函數的參數列表聲明

int setArray(FILE *fp, struct salesPerson sRec[]); 

所以我認爲你在這個原型之前沒有申報struct

另外關於您在定義它之前傾向於使用struct salesPerson sRec[],因爲這是一個不完整的類型。所以你應該在使用它之前完成這個類型。

+0

非常感謝你我在我的a3sale.h中有「SalesPerson」,但在我做salesPerson的函數中 –