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);
}
|