Thread: passing a file as a function parameter

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    42

    passing a file as a function parameter

    I posted a separate post with this code regarding a problem i was having implementing a particular function while attempting to pass a file as a parameter so that I could output it. Take a look and any advice would be greatly appreciated.

    errors: lab2.c: In function âmainâ:
    lab2.c:74: error: incompatible type for argument 1 of âNewPageâ
    lab2.c: At top level:
    lab2.c:106: error: conflicting types for âNewPageâ
    lab2.c:4: error: previous declaration of âNewPageâ was here


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void NewPage(FILE, float, float, float, int);
    float CalcGross(float, float);
    float CalcFICA(float, float);
    
    int main(){
    
            FILE *inFile;
            FILE *outFile;
    
            char resp = 'y';
            char fileInName[30];
            char fileOutName[30];
            float ficaTax;
            #define ficaRate .6078
    
    
    
    
            while(resp == 'Y' || resp == 'y'){
                    printf("Please enter the file you'd like to read from: ");
                    scanf("%s", fileInName);
                    if((fopen(fileInName, "r")) == NULL){
                            printf("Could not open requested file. Would you like to enter another file? [Y/N]: ");
                            scanf("%c", &resp);
                    }
                    inFile = fopen(fileInName, "r");
                    resp = 'n';
            }
    
            while(resp != 'Y' && resp != 'y'){
                    printf("Please enter the file you'd like to write to: ");
                    scanf("%s", fileOutName);
                    if(fopen(fileOutName, "r") != NULL){
                            printf("The file you are attempting to write to already exists! Overwrite?? [Y/N]: ");
                            scanf("%*c %c", &resp);
                            if(resp == 'Y' || resp == 'y')
                                    outFile = fopen(fileOutName, "w");
                    }
                    resp = 'y';
    
            }
            if(fopen(fileOutName, "r") == NULL)
                    outFile = fopen(fileOutName, "w");
    
    
            int PK = 1, LK = 0;
            int maxLines = 20;
            float pageGross = 0.00, pageFICA = 0.00, pageNet = 0.00;
            float totalGross = 0.00, totalFICA = 0.00, totalNet = 0.00;
    
            if (PK == 1){
                    fprintf(outFile, "\t\t\t\t\t<Company Name>\n\n\n\t\t\t\t\t<Tax Reports>\n\n");
                    fprintf(outFile, "---------------------------------------------------------------------------\n");
            }
    
            int empNum;
            char firstName[15], lastName[15], dept[7];
            float ytd, payrate, hrsWkd;
            while(fscanf(inFile, "%4d%s%s%s%f%f%f", &empNum, firstName, lastName, dept, &ytd, &payrate, &hrsWkd) != EOF){
                    //fprintf(outFile, "\tEmployee number: %d\tName: %s %s\tDept: %s\tYTD: %.2f\tPayrate: %.2f\tHours: %f\n", empNum, firstName, lastName, dept, ytd, payrate, hrsWkd);
                    float gross = CalcGross(hrsWkd, payrate);
                    float fica = CalcFICA(ytd, gross);
                    float net = ((ytd + gross) - fica);
                    if(LK<maxLines){
                    fprintf(outFile, "Emp #: %d\tName: %s %s\tDept: %s\t YTD: %.2f\tGross: %.2f\tFICA: %.2f\tNet: %.2f\t\n", empNum, firstName, lastName, dept, ytd, gross, fica, net);
                    LK++;
                    pageGross += gross;
                    pageFICA += fica;
                    pageNet += net;
                    }
                    else NewPage(*outFile, pageGross, pageFICA, pageNet, PK);
    
            }
    
    
    
    }
    
            float CalcGross(float hrsWkd, float payRt)
            {
                    float gross;
                    if(hrsWkd <= 40)
                            gross = (hrsWkd * payRt);
                    else gross = (hrsWkd * (payRt * 1.5));
                    return gross;
            }
    
            float CalcFICA(float ytd, float gross)
            {
                    float ficaTax;
    
                    if(ytd >= 67000.00)
                            ficaTax = 0.00;
                    else if ((ytd + gross) <= 67000.00)
                            ficaTax = (gross * ficaRate);
                         else ficaTax = (67000.00 - ytd) * ficaRate;
    
                    return ficaTax;
    
            }
    
            void NewPage(FILE *outToFile, float pageGross, float pageFICA, float pageNet, int pageCount)
            {
                    fprintf(outToFile, "Page Totals: \t\t\t\t\t%.2f\t%.2f\t%.2f", pageGross, pageFICA, pageNet);
                    fprintf(outToFile, "\n\t\t\t\t\t\t\t\t\t\tPage: %d", pageCount);
                    fprintf(outToFile, "------------------------------------PAGE BREAK---------------------------------------");
                    /*float totalGross += pageGross;
                    float totalFICA += pageFICA;
                    float totalNet += pageNet;
                    PK++;
                    LK = 0;
                    pageGross = 0.00;
                    pageFICA = 0.00;
                    pageNet = 0.00;*/
    
            }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void NewPage(FILE, float, float, float, int);
    Compare this with the implementation, which expects a FILE*

    Which is what it's complaining about here
    > error: incompatible type for argument 1
    You said argument 1 was one thing in one place, and another thing in another place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM
  3. Passing file stream as function parameter
    By simonc2 in forum C++ Programming
    Replies: 6
    Last Post: 12-22-2004, 12:12 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM