Sorting Competitors and Printing out finalists

Hello guys so I need help on a project that reads from a .csv file into a dynamic 2d array or a vector. The CSV file contains details of a swimming competition where i am supposed to read both names times and distances for different distances and times. Then I am required to sort them using times and distances and a user should be able to search for a race based on distances and it should show him/her the fastest times for a certain distance.
Here is my code which doesn't sort values by time because it is still in string format despite my attempt to convert it to int
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

bool sortByTime(const vector<int>& v1,const vector<int>& v2 );
int main(){
    vector< vector<string> > heatLevels;
    ifstream in("code.csv");
    if(!in)
        cout<< "File not found"<< endl;

    string line,sector;

    vector<string> v;

    while(getline(in,line)){
        v.clear();
        stringstream ss(line);
         while (getline(ss,sector,','))  // break line into comma delimited sectors
        {
            v.push_back(sector);  // add each sector to the 1D array
        }

        heatLevels.push_back(v);  // add the 1D array to the 2D array
    }

    for(int i = 0; i< heatLevels.size(); i++)
    {
        for(int j = 0; j<heatLevels[i].size(); j++)
        {
            cout<<heatLevels[i][j]<< setw(12);
        }
        cout<< "\n";
    }
    for(int i = 1; i< heatLevels.size(); i++)
    {
        int j=5;
        stringstream converter(heatLevels[i][j]);
        float x =0.0;
        converter >> x;

    }

    //Sort table by time
    sort(heatLevels.begin()+1, heatLevels.end(),sortByTime);


}
bool sortByTime(const vector<int>& a,const vector<int>& b )
{
    return a[5] < b[5];
}
Please post a small sample of your input file and show the expected output.

Also if the file fails to open you should probably return an error code to the operating system.






My input file looks like this
code.csv
No,Name,DOB,Heat,Distance,Time,Status
1,Person1,1998,1,100m,0.59,Q
2,Person2,1998,4,200m,1.54,DQ
3,Person3,1999,3,400m,4.32,Q
4,Person4,1997,2,100m,0.52,Q

Expected output
When user selects 100m it should print out names of people who swam the 100m in order of time and status
No,Name,DOB,Heat,Distance,Time,Status
1,Person4,1997,2,100m,0.52,Q
2,Person1,1998,1,100m,0.59,Q
Okay, I suggest you use a class or struct to encapsulate each record (line).

Then use a vector of the class/struct so that you can hold the entire file in memory for easy searching/sorting.



Could you please show me an example

Thank you
Show you an example of what?

Do you know what a struct is and how to use one?

My knowledge on structs is basic and I don't know how to use a vector of the struct to hold the entire .csv file. That was why I requested an example.

Thank you
You can create a struct like this:
1
2
3
4
5
6
7
8
9
10
struct Swimmer
{
  int no;
  string name;
  int dob;
  int heat;
  int distance;
  double time;
  string status;
};
I don't know how to use a vector of the struct


You would create a vector of the struct the same way you created a vector<string>.

Topic archived. No new replies allowed.