I'm trying to read from this file file.txt which contains contents of the contestants who participated in the long jump event during the Olympic games.
The file is in the format of [First Name] [Last Name] [Nationality] [Distance]
There are 40 contestants in this file. I'm trying to organize them such that there is a vector of pointers of athletes, 40 to be precise. Then dynamically allocate them to the heap. Each athlete is one object in the vector.
Once each athlete object is entered into the vector, I wish to output all the contents of the vector onto the console through a for loop.
However, as it currently stands, in my code I do have 40 objects allocated to the vector, but its the same one being repeated 40 times. The last object is also being repeated twice for some reason.
Any help would be greatly appreciated! Thanks in advance.
Test.cpp
#include <iostream>
#include<fstream>
#include<vector>
#include<string>
#include "Person.h"
#include "Athlete.h"
using namespace std;
vector<Athlete*> athletesList(40);
//overload the operator << to be used for printing the record Objects
ostream& operator<<(ostream& out, Athlete& a) {
out << a.getFirstName() << " " << a.getLastName() << " " << a.getNationality() << " " << a.getDistance() << "\n";
return out;
}
void readAthletesFromFile() {
fstream athlethesFile;
athlethesFile.open("file.txt");
string tmpFirstName;
string tmpLastName;
string tmpNationality;
string tmpDoubleDistance;
while (!athlethesFile.eof()) {
athlethesFile >> tmpFirstName;
athlethesFile >> tmpLastName;
athlethesFile >> tmpNationality;
athlethesFile >> tmpDoubleDistance;
double tmpDistance = stod(tmpDoubleDistance);
for (int i = 0; i < 40; i++) {
athletesList[i] = new Athlete(tmpFirstName, tmpLastName, tmpNationality, tmpDistance);
}
cout << *athletesList[0];
}
}
int main()
{
readAthletesFromFile();
system("pause");
}
File.txt
Aleksandr Menkov Russia 8.09
Aleksandr Petrov Russia 7.89
Alyn Camara Germany 7.72
Arsen Sargsyan Armenia 7.62
Boleslav Skhirtladze Georgia 7.26
Christian Reif Germany 7.92
Christopher Tomlinson Great_Britain 8.06
Damar Forbes Jamaica 7.79
Eusebio Caceres Spain 7.92
George Kitchens United_States 6.84
Godfrey Khotso-Mokoena South_Africa 8.02
Greg Rutherford Great_Britain 8.08
Henry Frayne Australia 7.95
Ignisious Gaisah Ghana 7.79
Li Jinzhe China 7.77
Lin Ching-Hsuan-Taipei China 7.38
Louis Tsatoumas Greece 7.53
Luis Rivera Mexico 7.42
Marcos Chuva Portugal 7.55
Marquise Goodwin United_States 8.11
Mauro-Vinicius da-Silva Brazil 8.11
Michel Torneus Sweden 8.03
Mitchell Watt Australia 7.99
Mohamed Fathalla-Difallah Egypt 7.08
Mohammad Arzandeh Iran 7.84
Ndiss Kaba-Badji Senegal 7.66
Povilas Mykolaitis Lithuania 7.61
Raymond Higgs Bahamas 7.76
Roman Novotny Czech-Republic 6.96
Salim Sdiri France 7.71
Sebastian Bayer Germany 7.92
Sergey Morgunov Russia 7.87
Stanley Gbagbeke Nigeria 7.59
Stepan Wagner Czech-Republic 7.5
Supanara Sukhasvasti Thailand 7.38
Tyrone Smith Bermuda 7.97
Vardan Pahlevanyan Armenia 6.55
Viktor Kuznyetsov Ukraine 7.5
Will Claye United_States 7.99
Zhang Xiaoyi China 7.25
Expected Output
Ex: *athletesList[0] = Aleksandr Menkov Russia 8.09
*athletesList[10]= Godfrey Khotso-Mokoena South_Africa 8.02
Current Output
Ex: *athletesList[0] =
Aleksandr Menkov Russia 8.09
Aleksandr Petrov Russia 7.89
Alyn Camara Germany 7.72
Arsen Sargsyan Armenia 7.62
Boleslav Skhirtladze Georgia 7.26
Christian Reif Germany 7.92
Christopher Tomlinson Great_Britain 8.06
Damar Forbes Jamaica 7.79
Eusebio Caceres Spain 7.92
George Kitchens United_States 6.84
Godfrey Khotso-Mokoena South_Africa 8.02
Greg Rutherford Great_Britain 8.08
Henry Frayne Australia 7.95
Ignisious Gaisah Ghana 7.79
Li Jinzhe China 7.77
Lin Ching-Hsuan-Taipei China 7.38
Louis Tsatoumas Greece 7.53
Luis Rivera Mexico 7.42
Marcos Chuva Portugal 7.55
Marquise Goodwin United_States 8.11
Mauro-Vinicius da-Silva Brazil 8.11
Michel Torneus Sweden 8.03
Mitchell Watt Australia 7.99
Mohamed Fathalla-Difallah Egypt 7.08
Mohammad Arzandeh Iran 7.84
Ndiss Kaba-Badji Senegal 7.66
Povilas Mykolaitis Lithuania 7.61
Raymond Higgs Bahamas 7.76
Roman Novotny Czech-Republic 6.96
Salim Sdiri France 7.71
Sebastian Bayer Germany 7.92
Sergey Morgunov Russia 7.87
Stanley Gbagbeke Nigeria 7.59
Stepan Wagner Czech-Republic 7.5
Supanara Sukhasvasti Thailand 7.38
Tyrone Smith Bermuda 7.97
Vardan Pahlevanyan Armenia 6.55
Viktor Kuznyetsov Ukraine 7.5
Will Claye United_States 7.99
Zhang Xiaoyi China 7.25
Zhang Xiaoyi China 7.25