passing variables into function and printing them

This is a discussion on passing variables into function and printing them within the C Programming forums, part of the General Programming Boards category; In my function HighTotoalSales i want to print the highest average sale along with the saleperson who made it....i can't ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    6

    Question passing variables into function and printing them

    In my function HighTotoalSales i want to print the
    highest average sale along with the saleperson who
    made it....i can't seem to pass highsalnum and high
    into the ReportFile function...this made be something obvious
    that i'm just not getting...can anybody give me some help???


    /CODE STARTS HERE//

    #include <stdio.h>
    #define MAXSALESPERSON 5

    void ProcessData( int salefilenum, float saleamt, int salesnum[], float salesamt[], int numofsales[]);
    void ReportFile(int salesnum[], float salesamt[], int numofsales[], float totalsales, int totalsalesnum, float AverageSale, float persaleavg[], int highsalnum, float high);
    void ErrorData(int salefilenum);
    float Average(float totalsales, int totalnumsales);
    float CalcTotalSales(float salesamt[]);
    int CalcTotalNumSales(int numofsales[]);
    float TotalPerSalePerson (int numofsales[], float salesamt[], float persaleavg[]);
    float HighTotSale(float salesamt[], int salesnum[]);
    /*void ErrorSales(float AverageSales); */

    main()
    {

    FILE *salefile_ptr;

    int salefilenum;
    float saleamt;
    int i;
    float AverageSale;
    float totalsales;
    int totalnumsales;
    int highsalnum;
    float high;

    int salesnum[MAXSALESPERSON] = {0};
    float salesamt[MAXSALESPERSON] = {0};
    int numofsales[MAXSALESPERSON] = {0};
    float persaleavg[MAXSALESPERSON] = {0};

    /* salefile_ptr = fopen("sales.txt", "r");a */

    salefile_ptr = fopen("sales.txt", "r");

    if(salefile_ptr != NULL)
    {
    while(fscanf(salefile_ptr, "%d %f", &salefilenum, &saleamt) != EOF)
    {
    ProcessData(salefilenum, saleamt, salesnum, salesamt, numofsales);
    TotalPerSalePerson(numofsales,salesamt, persaleavg);
    totalsales = CalcTotalSales(salesamt);
    totalnumsales = CalcTotalNumSales(numofsales);
    AverageSale = Average(totalsales, totalnumsales);
    HighTotSale(salesamt, salesnum);
    ReportFile(salesnum, salesamt, numofsales, totalsales, totalnumsales, AverageSale, persaleavg, highsalnum, high);
    }
    fclose(salefile_ptr);
    }
    else
    printf("\nReport file not opened!\n");


    printf("\nEnd of job..\n\n");
    }

    void ProcessData( int salefilenum, float saleamt, int salesnum[], float salesamt[], int numofsales[])
    {
    int num;
    if ( (salefilenum > MAXSALESPERSON) || (salefilenum <= 0) )
    {
    ErrorData(salefilenum);
    }
    else
    {
    salesnum[salefilenum - 1] = salefilenum;
    salesamt[salefilenum -1] += saleamt;
    numofsales[salefilenum - 1] += 1;
    }
    }

    float TotalPerSalePerson (int numofsales[], float salesamt[], float persaleavg[])
    {
    int num;

    for(num = 0; num < MAXSALESPERSON; num++)
    {
    persaleavg[num] = salesamt[num]/numofsales[num];
    }
    }

    void ReportFile(int salesnum[], float salesamt[], int numofsales[], float totalsales, int totalnumsales, float AverageSale, float persaleavg[], int highsalnum, float high )
    {
    int j;
    FILE *reportfile_ptr;
    reportfile_ptr = fopen("report.txt", "w");

    fprintf(reportfile_ptr, "\t\t\tMelissa Clark");
    fprintf(reportfile_ptr, "\n\n\t\tCompany Fiscal Totals\n\n");
    fprintf(reportfile_ptr, "Num of Total Num of Total Amount of Company Avg \n");
    fprintf(reportfile_ptr, "Salespeople Company Sales Sales \tSale\n");
    fprintf(reportfile_ptr, "-----------------------------------------------------------\n");
    fprintf(reportfile_ptr, " %d\t\t %d \t$%2.2f\t$%2.2f\n", MAXSALESPERSON, totalnumsales, totalsales, AverageSale);
    fprintf(reportfile_ptr, "\n\n\t\tBreakout by each SalesPerson's Performance\n\n");
    fprintf(reportfile_ptr, "SalesPerson # Num of Sales Total Sales \tAverage Sale \n");
    fprintf(reportfile_ptr, "-------------------------------------------------------------\n");
    for (j = 0; j < MAXSALESPERSON; j++)
    {
    fprintf(reportfile_ptr, " %d\t\t %d\t\t$%2.2f \t\t$%2.2f\n", salesnum[j], numofsales[j], salesamt[j], persaleavg[j]);
    }
    fprintf(reportfile_ptr, "\nSaleperson %d has the highest sale average of %2.1f\n", highsalnum, high);
    fclose(reportfile_ptr);
    }


    void ErrorData(int salefilenum)
    {
    FILE *Error_ptr;
    Error_ptr = fopen("err_report.txt", "w");
    fprintf(Error_ptr, "%d is out of range\n", salefilenum);
    fclose(Error_ptr);
    }

    /*void ErrorSales(float AverageSales)
    {
    FILE *ErrorSale_ptr;
    ErrorSale_ptr = fopen("err_sale_report.txt", "w");
    fprintf(ErrorSale_ptr, "0 number of salesman. Returning %f average\n", AverageSales);
    fclose(ErrorSale_ptr);
    }
    */

    float CalcTotalSales(float salesamt[])
    {
    int num;
    float totalsales;
    totalsales = 0;
    for(num =0; num <MAXSALESPERSON; num++)
    {
    totalsales += salesamt[num];
    }
    return totalsales;
    }

    int CalcTotalNumSales(int numofsales[])
    {
    int num;
    int totalnumsales;
    totalnumsales = 0;
    for(num = 0; num < MAXSALESPERSON; num++)
    {
    totalnumsales += numofsales[num];
    }
    return totalnumsales;
    }

    float Average(float totalsales, int totalnumsales)
    {
    float AverageSale;

    AverageSale = totalsales/totalnumsales;
    return AverageSale;
    }

    float HighTotSale(float salesamt[], int salesnum[])
    {
    int num;
    int highsalnum;
    float high;

    high = salesamt[0];
    for(num = 1; num < MAXSALESPERSON; num++)
    if (salesamt[num] > high)
    {
    highsalnum = salesnum[num];
    high = salesamt[num];
    }
    ReportFile(highsalnum high);

    }


    //CODE ENDS HERE//

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Use code tags please.

    int main()

    Functions with a return type deserve a return statement (i.e. int main)

    ReportFile(highsalnum high);

    You forgot a comma...

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    hmm the comma was a copy/paste error
    andi changed the HighTotSales function from float to void...
    still does not work...

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Code:
    void ReportFile(int salesnum[], float salesamt[], int numofsales[],
    float totalsales, int totalsalesnum, float AverageSale,
     float persaleavg[], int highsalnum,
     float high); 
    
    
    ...................................
    int highsalnum;
    float high;
    
    ReportFile(highsalnum high);
    I think the problem is in the call here.
    Last edited by ronin; 04-29-2003 at 10:07 PM.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    retalitory flames??? only luv here

    i fixed my problem with the help of a c-programmer
    i passed my variables by reference for three of my functions
    prog prints the report peachy keen now....

    thanxs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-21-2008, 04:27 PM
  2. fprintf in void function Not Printing
    By thetinman in forum C Programming
    Replies: 5
    Last Post: 10-17-2006, 06:13 PM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. Passing by value and printing in function
    By pecymanski in forum C Programming
    Replies: 1
    Last Post: 03-17-2003, 04:27 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21