I cannot find where the vector is reaching beyond itself. I have a feeling it's in my token function but I cannot determine what I'm doing that I'm not allowed to.
I've tried the debugger in Visual Studio but its only confused me more.
Am I hung up in the cons or champ vectors? I think it's stopping during the tokens vector. I've clearly misunderstood something.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
// Function for split string for given token
vector<string> split(string str, char token) {
vector<string> pas;
string s = "";
for (int i = 0; i < str.size(); i++) {
if (str[i] == token) {
pas.push_back(s);
s = "";
}
else {
s += str[i];
}
}
pas.push_back(s);
return pas;
}
// Function for convert time to milliseconds
int convertToMilSeconds(string time) {
int m = 10 * (time[0] - '0') + time[1] - '0';
int s = 10 * (time[3] - '0') + time[4] - '0';
int h = 10 * (time[6] - '0') + time[7] - '0';
int milSecond = m * 60 * 100 + s * 100 + h;
return milSecond;
}
// Main method
int main() {
ifstream inFile;
string fileName;
cout << "Enter filename: ";
cin >> fileName;
inFile.open(fileName);
if (inFile.is_open()) {
string title, temp;
getline(inFile, title);
getline(inFile, temp);
map<int, string> data;
int n = stoi(temp);
for (int i = 0; i < n; i++) {
getline(inFile, temp);
vector<string> tokens = split(temp, ' ');
if (tokens[2] == "DQ") {
continue;
}
else {
int milSec = convertToMilSeconds(tokens[2]);
string name = tokens[1] + ", " + tokens[0] + " " + tokens[2];
data.insert(pair<int, string>(milSec, name));
}
}
vector<int> cons;
vector<int> champ;
int val = 1;
for (map<int, string>::iterator itr = data.begin(); itr != data.end(); itr++) {
if (val >= 1 && val <= 8) {
champ.push_back(itr->first);
}
if (val >= 9 && val <= 16) {
cons.push_back(itr->first);
}
val++;
}
cout << title << " " << "CONSOLATION FINALS" << endl;
int lane = 1;
for (int x = 6; x >= 0; x -= 2) {
map<int, string>::iterator itr = data.find(cons[x]);
cout << lane << " " << itr->second << endl;
lane++;
}
for (int x = 1; x < 8; x += 2) {
map<int, string>::iterator itr = data.find(cons[x]);
cout << lane << " " << itr->second << endl;
lane++;
}
cout << endl << title << " " << "CHAMPIONSHIP FINALS" << endl;
lane = 1;
for (int x = 6; x >= 0; x -= 2) {
map<int, string>::iterator itr = data.find(champ[x]);
cout << lane << " " << itr->second << endl;
lane++;
}
for (int x = 1; x < 8; x += 2) {
map<int, string>::iterator itr = data.find(champ[x]);
cout << lane << " " << itr->second << endl;
lane++;
}
}
else {
cout << "File not found" << endl;
}
return 0;
}