Problem with Files/Structures/Functions

I had been trying this week to finish the final project from my introduction class to C++. The purpose of the project has to start opening a file that contains the data in 4 columns and 150 rows. Then the program shall use an array of structure to stored the data read from the file and another array (or multiple arrays) of structure to hold the statistics computed by your program.

You must use functions and group related/common functions into separated files. Once you read the data and stored them in an array of the structure you designed, compute the following statistics from the data and display the results:
 The number of runners available in the file
 The average age of the runners
 The average time the runners took to complete the marathon
 The number of female runners
 The average age of the female runners
 The average time of the female runners
 A table with stats divided by age group (see sample output) for the female runners
 The number of male runners
 The average age of the male runners
 The average time of the male runners
 A table with stats divided by age group (see sample output) for the male runners

The first thing I tried was to check if the file could open and then create the structures.

This is what i got so far:

The structure on a Header for Runners

1
2
3
4
5
6
7
8
9
10
11
12
 #ifndef RUNNERS_H
#define	RUNNERS_H

struct Runners { 
    int position;
    int age;
    char gender;
    int time;
    
};

#endif 


On my main file I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

#include "Runners.h"

using namespace std;

int main(void) {
    
    ifstream ifs("marathon.txt");
    
    if (ifs.fail()) {
        cout << "Could not open the file!" << endl;
        return(1);
        
    }
    
    const int NUMBER_OF_ROWS = 250;
    const int NUMBER_OF_COLUMNS = 4;
       
    cout << setiosflags(ios::left);
    cout << setw(10) << "Position" << setw(5) << "Age" << setw(5) << "Gender" << setw(5) << "Time" << endl;
    
    for (int i = 0; i < 250; i++) {
        cout << setw(10) << Runners[i].position << " " <<
             << setw(5)  << Runners[i].age      << " " <<
             << setw(5)  << Runners[i].gender   << " " <<
             << setw(5)  << Runners[i].time     << endl;   
    }
    
    ifs.close();
        
    return(0);
    
}
Can you post a sample of about 5 lines of your data file ? mainly want to see what the time format looks like.

The first thing you want is to make something that will compile.
Then you want to make sure you can open the file and read each value, and output that to the screen. There is no reason to write the rest of the code until you can read in some values to work with.

Line 26-30 won't compile the way it's written.
Comment it out for now, until you can read the file.
On line 24, you did setw(5) however "Gender" is 6 char's so setw should be 7.
This is an excerpt of what the file has:

130 32 M 9631
265 39 M 10209
314 39 M 10351
490 36 M 10641
547 34 M 10723
708 28 M 10905
834 42 M 11061
944 46 M 11188
1084 32 M 11337
1086 34 M 11338

Last edited on
So this is what i had been able to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <fstream>
#include <iostream>
#include <iomanip>

//Structures included to use the data on functions
#include "Runners.h"
#include "AgeCategory.h"
#include "GeneralStats.h"
#include "LoadData.h"

using namespace std;

int main(void){
    
    ifstream infile("marathon.txt");
    
	if (infile.fail()) {  
	cout << "Could not open marathon.txt file!" << endl;  
	cout << "Check if the file exists!" << endl;  
	return 1; }
	
	 const int NUMBER_OF_ROWS = 250;     
	 
	 Runners runner[NUMBER_OF_ROWS];

	 float averege, avergeoftime, 
		 totalage = 0, totalageoffemale = 0,
		 totalageofmale = 0, averegeageoffemale,
		 averegeageofmale, averegetimeoffemale, averegetimeofmale;
	 
	 int totaltimeofmale = 0, totaltimeoffemale = 0,
		 totaltime = 0, female = 0,  male = 0, numberofrunners = 0;

	 for (int j = 0; !infile.eof(); j++) 
	 {
		 infile >> runner[j].position >> runner[j].age >> runner[j].gender >> runner[j].time;
		 numberofrunners++;
	 }
	 
	 // age 
	 for (int i = 0; i < numberofrunners; i++)
	 {
			totalage = totalage + runner[i].age;
		
		 if (runner[i].gender == 'F')
		 {
			 totalageoffemale = totalageoffemale + runner[i].age;
		 }

		 if (runner[i].gender == 'M') 
		 {
			 totalageofmale = totalageofmale + runner[i].age;
		 }
		
	 }
 
	 // time of all 
	 for (int i = 0; i < numberofrunners; i++)
	 {
		 totaltime = totaltime + runner[i].time;
		 
		 if (runner[i].gender == 'F')
		 {
			 totaltimeoffemale = totaltimeoffemale + runner[i].time;

		 }

		 if (runner[i].gender == 'M')
		 {
			 totaltimeofmale = totaltimeofmale + runner[i].time;
		 }
	 }
	 
	 	 // # of female and male
	 for (int i = 0; i < numberofrunners; i++)
	 {
		 if (runner[i].gender == 'F')
		 {
			 female++;
		 }
		 else
		 {
			 male++;
		 }
	 }
	 // all of the averege
	 averege = totalage / numberofrunners;
	 avergeoftime = totaltime / numberofrunners; 
	 averegetimeoffemale = totaltimeoffemale / female;
	 averegetimeofmale = totaltimeofmale / male;
	 averegeageoffemale = totalageoffemale / female;
	 averegeageofmale = totalageofmale / male;

	cout << " NY Marathon Stats" << endl;
	cout << " ===============================================" << endl; 
	cout << " Number of runners   : " << numberofrunners << endl;
	cout << " Overall average age : " << setiosflags(ios::fixed) << setprecision(2) << averege << endl;
	cout << " Overall average time: " << setiosflags(ios::fixed) << setprecision(0) << avergeoftime << " secs" << endl; 
	cout << endl;
	cout << " Female" << endl;
	cout << " ===============================================" << endl;
	cout << " Number of runners: " << female << endl; 
	cout << " Average age      : " << setiosflags(ios::fixed) << setprecision(2) << averegeageoffemale << endl;
	cout << " Average time     : " << setiosflags(ios::fixed) << setprecision(0) << averegetimeoffemale << " secs" << endl; 
	cout << endl;
	cout << " Male" << endl;
	cout << " ===============================================" << endl;
	cout << " Number of runners: " << male << endl;
	cout << " Average age      : " << setiosflags(ios::fixed) << setprecision(2) << averegeageofmale << endl;
	cout << " Average time     : " << setiosflags(ios::fixed) << setprecision(0) << averegetimeofmale << " secs" << endl; 
	cout << endl;

	 
    infile.close();
   
    return(0);

}


But I need to open the file and write the output data on "results.txt" on another .cpp
Topic archived. No new replies allowed.