0

Let me preface by saying this question is in regards to Programming 1 class lab. I am NOT looking for someone to provide a line for line code for my work. However, this being an online class, I'm left a bit directionless and would really appreciate some guidance in terms of converting concepts to coding operators.

The lab is for taking lap times from a .txt file and performing different arithmetic on the values such as organizing from 1st to 3rd, etc. I'm confident on my ability to manipulate the data however the lab wants. What I'm having issue with are ways of storing this data so that it can be manipulated. The file has 7 students but can go up to 20.

The Results.txt is formatted like this:

Lap 1:  
Akano 1:43  
Wes 1:45  
Kye 1:52  
Edward 2:05  
Jess 2:14  
Ally 2:26  
Wilt 2:30  

Lap 2:  
Edward 1:50  
Akano 2:00  
Wes 2:03  
Kye 2:15  
Jess 2:16  
Ally 2:23  
Wilt 2:54

Lap 3:  
Kye 2:01  
Akano 2:06  
Ally 2:54  
Wes 3:03  
Wilt 3:11  
Jess 3:15  
Edward 3:21  

The first part of my instructions are thusly:

"Objective 1: Output the final times of all the students. I also want to know who placed 1st, 2nd and 3rd overall. (Though if you have them all in order that will be sufficient)."

I've developed some preliminary code for taking a string and separating it into 3 categories. Name (p1L1), Minutes (p1L1min) and Seconds (p1L1sec). However, after creating it it occurred to me that I would need some sort of way of organizing and grouping each of the runners for each of the laps, considering their order is pretty random in the Results.txt. I figured the best way to accomplish that would be to take each line as a single string, organize them alphabetically, and then break them apart.

However, I'm having trouble determining how to sort a string alphabetically. I'm not even sure that's the best way of doing things. I'm also not sure how to keep them grouped. If all of my data is coming from the file, how can I make sure that Akano's name variable, always shows with Akano's time variable, after I've broken them up.

Any guidance or advice I can get at this point would be extremely helpful. I'll paste the above referenced code below:

int main()
{

    ifstream lapSource;
    lapSource.open("Results.txt");
    string lap, lapNumber;

    lapSource >> lap >> lapNumber;

    string p1L1, p1L1secstring;
    int p1L1min, p1L1sec;

    lapSource >> p1L1 >> p1L1min >> p1L1secstring;

    p1L1secstring.erase(0, 1);
    p1L1sec = stoi(p1L1secstring);

    return 0;
}
MaxV
  • 2,601
  • 3
  • 18
  • 25
  • 1
    You don't read the colons from the file. I recommend that you [read whole lines](https://en.cppreference.com/w/cpp/string/basic_string/getline) into a string, and then try to parse the string. – Some programmer dude Oct 26 '20 at 23:26

1 Answers1

0

You can use map<string, pair<int,int>> data structure to store names as well as their timings, where first member of pair will be minutes and the second member will be for seconds. This will store the names of players in lexicographical order.

You can also use vector of struct. You can define a struct of player like below:

typedef struct player{
  string name;
  int min;
  int sec;
}player;

Now create a vector of player:

vector<player> p;

Take input from file and store them in above vector. While taking input you can try reading whole line and then parse it(but it depends on you how you are going to do it). You can sort this vector according to names of player. Refer this sorting a vector of structs .

dark_prince
  • 237
  • 2
  • 12