problems reading data into an array and printing output

This is a discussion on problems reading data into an array and printing output within the C Programming forums, part of the General Programming Boards category; i'm trying to learn how to reading data from files into an array and process the data. i can't figure ...

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

    Unhappy problems reading data into an array and printing output

    i'm trying to learn how to reading data from files into an array
    and process the data. i can't figure out what i'm doing wrong
    i have a sales.txt file that contains(two columns- salesman # and amount of a sale) that i'm reading into two arrays salesnum and salesamt. i want to store salesman number in salesnum array and salesamount in salesamt array..i'm trying to tally up the amount of sales and number of sales each salesperson has.
    when reading in the file to the arrays it only reads the first 5 lines
    not the next 10.
    my arrays can only be five elements because there are only 5
    salesmen but there are 15 sales....i don't know what i'm doing
    wrong basically...could use some help
    could someone look at this please


    #include <stdio.h>
    #define MAXSALESPERSON 5

    void ProcessData( int salefilenum, float saleamt, int salesnum[], float salesamt[]);
    void ReportFile(int salesnum[], float salesamt[]);
    void ErrorData(int salefilenum);

    main()
    {

    FILE *salefile_ptr;

    int salefilenum;
    float saleamt;
    int i;

    int salesnum[MAXSALESPERSON] = {0};
    float salesamt[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);
    ReportFile(salesnum, salesamt);
    }
    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[])
    {
    if( (salefilenum <= 0) || (salefilenum > MAXSALESPERSON) )
    {
    ErrorData(salefilenum);
    }
    else
    {
    salesnum[salefilenum - 1] = salefilenum;
    salesamt[salefilenum - 1] = saleamt;
    }
    }

    void ReportFile(int salesnum[], float salesamt[])
    {
    FILE *reportfile_ptr;
    reportfile_ptr = fopen("report.txt", "w");
    int j;
    for (j = 0; j < MAXSALESPERSON; j++)
    {
    fprintf(reportfile_ptr, "%d\t%2.1f\n", salesnum[j], salesamt[j]);
    }
    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);
    }

  2. #2
    scrlet7
    Guest
    > #define MAXSALESPERSON 5

    make it bigger

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    the salesnum array and salesamt array is suppose to be 5 elements because there are only 5 sales people..so basically everytime salesperson 1 makes a sale i need to increment the number of sales that person made and the amount increases also
    i can't figure out how to do that with the array

    i can get the total amount of sales for each person but not the
    total number of sales

    salesnum[salefilenum -1] += salesamt; /*total amount of sales
    for each salesperson */

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    You could add the totals like this
    Code:
      
    void ReportFile(int salesnum[], float salesamt[])
    {
    	FILE *reportfile_ptr;
    	float Totalamt = 0;
    
    	reportfile_ptr = fopen("report.txt", "w"); 
    	int j;
    	for (j = 0; j < MAXSALESPERSON; j++)
    	{
    		fprintf(reportfile_ptr, "%d\t%2.1f\n", salesnum[j], salesamt[j]);
    
    		Totalamt += salesamt[j];
    	}
    	fprintf(reportfile_ptr, "\nTotal \t%2.1f\n\n",  Totalamt);
    }
    Last edited by Scarlet7; 04-27-2003 at 04:06 PM.

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

    Thumbs up

    Thanks......that did the trick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 07:10 PM
  2. Having trouble printing from an array
    By Sir Andus in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 12:48 PM
  3. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 04:16 PM
  5. C Filling and Printing a 2-d array
    By Smoot in forum C Programming
    Replies: 3
    Last Post: 11-13-2003, 07:42 PM

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