10 Replies - 1438 Views - Last Post: 01 December 2012 - 07:36 PM Rate Topic: -----

#1 VictorP89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-October 12

Process data read in Random Access File

Posted 28 November 2012 - 01:18 PM

Hey

So here is my problem. I am trying to make a program that can calculate some statistics by reading a random access file and hereby process the information in order to calculate the, numberofredcards and total goals for a soccer season. The file is already made but i can't figure out how to process the information in the file. the program will print and read the file but how will i be able to use that data in the file? I know the program already is reading and recording from the file. Hope somebody could help.

struct clientData {
    int numberofgoals;
    char teamname[ 30 ];
    double numberofredcards;
};
int main( void ) {
    FILE *cfPtr; 

    struct clientData client = { 0, "", 0.0 };
    
    if ( ( cfPtr = fopen( "credit.dat", "rb" ) ) == NULL ) {
        printf( "File could not be opened.\n" );
    }
    else {
        printf( "%-6s%-16s%-11s\n", "Number of goals", "Teamname", "Number of red cards" );
 
        while ( !feof( cfPtr ) ) {
            fread( &client, sizeof( struct clientData ), 1, cfPtr );
          
            if( client.numberofgoals != 0 ) {
                printf( "%-6d%-16s%-11f\n",
                       client.numberofgoals, client.teamname, client.numberofredcards );
            } 
        } 
        fclose( cfPtr ); 
    }
return 0;
}


thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Process data read in Random Access File

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4007
  • View blog
  • Posts: 12,361
  • Joined: 25-December 09

Re: Process data read in Random Access File

Posted 28 November 2012 - 01:29 PM

Quote

order to calculate the, numberofredcards and total goals for a soccer season. The file is already made but i can't figure out how to process the information in the file. the program will print and read the file but how will i be able to use that data in the file?

Well the hard part is taken care of, now you need to decide how you want to use the data. What kind of calculations do you need to preform on your data?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 VictorP89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-October 12

Re: Process data read in Random Access File

Posted 28 November 2012 - 02:00 PM

View Postjimblumberg, on 28 November 2012 - 01:29 PM, said:

Quote

order to calculate the, numberofredcards and total goals for a soccer season. The file is already made but i can't figure out how to process the information in the file. the program will print and read the file but how will i be able to use that data in the file?

Well the hard part is taken care of, now you need to decide how you want to use the data. What kind of calculations do you need to preform on your data?

Jim


The calculations are very simple actually. I might improve them after some time. But the purpose is to calculate the sum of totalnumberofgoals, totalnumberofredcards and i will probably also add averagenumberofgoals, so actually just add the numbers up which is stored in the file and after finding the average. My output could look like this

Number of goals Teamname Number of red cards
1 Man. U 2
4 Chealsea 1
2 Arsenal 3
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4007
  • View blog
  • Posts: 12,361
  • Joined: 25-December 09

Re: Process data read in Random Access File

Posted 28 November 2012 - 02:06 PM

Okay so it looks like you need to have some variables to "sum" these values. So after you read your file maybe you could do some summing of the values you read?

Note: We are not going to give you the code, you need to show what you have tried. Then ask specific questions. We don't know what your file contains, what stats you are interested in or anything else.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 VictorP89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-October 12

Re: Process data read in Random Access File

Posted 30 November 2012 - 12:42 AM

So here is my try to make it work. Basically the first part of the program is making a file consisting of a string of integers such as:
1
2
3
4
5
6

and the last part is reading the file and outputting the integers summed up and also taking the averages of all the integers. However i am not getting the correct results. Is it because the writing of the file is incorrect or is it the reading? Could use a hint. Also been thinking about the storing the integers in an Array and work from there instead??
#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{ 

    int  numberofgoals = 0;
    
    FILE *cfPtr; 
    
    /* fopen opens file. Exit program if unable to create file */
    if ( ( cfPtr = fopen( "Soccerstatistics1.dat", "w" ) ) == NULL ) { //fopen opens the file Soccerstatistics.dat which is defined by the pointer *cfptr. The w creates file ready for writing. If an error occurs fopen returns NULL and print the printf statement below. 
        printf( "File could not be opened\n" );
        } //ends if statement
    else {
        printf("Hello and welcome to Soccer Statistic Calculator!\nThis calculator well help you manage your statistics and be able to calculate:\n - Average goal scored pr. match\n - Total goals scored\n - Number of goals scored pr. team\n - Number of yellow cards recieved\n");
        printf("In order to start please input the team name, number of goals scored and number of red cards received (this have to be done for each game played)for each match followed by enter after each number of goals scored.\nEnter EOF to end input.\n"); //prompts user to input values used to create file
        printf("Enter Values: ");
        scanf( "%d",  &numberofgoals ); //scans user input and stores 
       
        while ( !feof( stdin )) {
            fprintf( cfPtr, "%d\n", numberofgoals ); //writes teamname, numberofgoals, numberofredcards into file Soccerstatistics.dat
            printf( "Enter Values: " ); //prompts user
            scanf( "%d",  &numberofgoals ); //scans user input and stores 
            } //ends while statement
     
        fclose( cfPtr ); //closes file refered by a pointer
        } // ends else statement
   
        int numberofinputs=0, i=0;
        double averagegoals =0, averageredcards=0;
        
        FILE *Soccerstatistics;
        
        if ( ( Soccerstatistics = fopen( "Soccerstatistics1.dat", "r" ) ) == NULL ) {
            printf( "File could not be opened.\n" );
        }
        while (fscanf(Soccerstatistics,"%d", &numberofinputs)!=EOF ) {
            
            numberofgoals += numberofinputs;
            
            i++;
            
            averagegoals = (numberofgoals/i);
        }
        printf("\nTotal number of goals scored: %d\n",numberofgoals);
        printf("Average goals scored: %f", averagegoals);
        fclose( Soccerstatistics );
        
    
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6053
  • View blog
  • Posts: 23,488
  • Joined: 23-August 08

Re: Process data read in Random Access File

Posted 30 November 2012 - 05:46 AM

You don't tell us how the results are wrong, but I suspect you're running into this.
Was This Post Helpful? 0
  • +
  • -

#7 VictorP89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-October 12

Re: Process data read in Random Access File

Posted 30 November 2012 - 01:10 PM

oh that is true. Well the average and total sum of integers is wrong in the output. For an example i am getting 26 total and 6 average for the following input
2
8
4
6

Will take a look at that!
Was This Post Helpful? 0
  • +
  • -

#8 VictorP89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-October 12

Re: Process data read in Random Access File

Posted 01 December 2012 - 01:49 PM

Hmm looked through your link and couldnt see what i dd wrong. I figured that the calculation of the average needs to outside of the while loop, but still gives me the wrong answer. Cant really see what i do wrong?

My output looks like this:

fisse Has scored 24 goals in total with an average of 6.00 goals pr. game

and the file has the following stats:
3
4
5
6

The sum is wrong and so is the average. Average is even without decimals.

 int i=0;
            double numberofinputs=0;
            
            if ( ( Soccerstatistics = fopen( "Soccerstatistics1.txt", "r" ) ) == NULL ) {
            printf( "File could not be opened.\n" );
            }
            while (fscanf(Soccerstatistics,"%lf", &numberofinputs)!=EOF ) {
                
                numberofgoals = numberofgoals + numberofinputs;
                
                i++;

                }//ends while loop
            fclose( Soccerstatistics ); //closes file
            
            averagegoals = (numberofgoals/i);
            printf("\n%s Has scored %d goals in total with an average of %.2f goals pr. game", teamname, numberofgoals,averagegoals);



Could really use some help been spending alot of time to get this to work!

Thanks!
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4007
  • View blog
  • Posts: 12,361
  • Joined: 25-December 09

Re: Process data read in Random Access File

Posted 01 December 2012 - 04:45 PM

So have you tried printing out the values you retrieved from the file to insure they are correct?

Print out the value you read inside the loop.

Where have you initialized numberofgoals before your loop, and what value did you assign to it?

What is the value of i after the loop?

Jim
Was This Post Helpful? 0
  • +
  • -

#10 VictorP89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-October 12

Re: Process data read in Random Access File

Posted 01 December 2012 - 07:03 PM

I have tried all of that and it is the sum that is calculated wrong. since it says 24 which is not correct.

i = 4

If i print the file i get
3
4
5
6

I initialized numberofgoals in the very beginning of the program with int numberofgoals=0;

It works now. I moved the numberofgoals from being a global to local variable. Now any ideas of how i the average to display decimals? if there is 7 inputs and the sum is 30 it just says the average is 4 and no decimals?
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4007
  • View blog
  • Posts: 12,361
  • Joined: 25-December 09

Re: Process data read in Random Access File

Posted 01 December 2012 - 07:36 PM

In your calculation you are using two integer values so you are doing integer math. In integer math there are no fractions. So you will need to cast at least one of the variables to a floating point type. Something like:

int test = 1;
int div = 2;
double number = static_cast<double>(test) / div;



Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1