Reputation:
So, I have a file that I’m reading from which includes players names, then space followed by the team name [Christian_Ponder KC]. For this, I made this struct:
struct qb_info {
string player;
string team;
};
Then, in main, I’m reading everything in one string vector: string str;
vector <string> players_and_team;
qb_info player_info;
vector <qb_info> player_data;
while(!data_file.eof()) {
data_file >> str;
players_and_team.push_back(str);
}
Then, If the index is even, its going to be a player, so I call the struct and store in player and push it in the vector of struct and if its odd it will be the team:
for(int i = 0; i < players_and_team.size(); i++) {
if(i % 2 == 0) {
player_info.player = players_and_team.at(i);
player_data.push_back(player_info);
} else {
player_info.team = players_and_team.at(i);
player_data.push_back(player_info);
}
}
But when I print it, everything is printed twice:
for(int i = 0; i < player_data.size() / 2; i++) {
cout << player_data.at(i).player << ", " << player_data.at(i).team << endl;
}
Here is the output:
Aaron_Rodgers,
Aaron_Rodgers, GB
Alex_Smith, GB
Alex_Smith, KC
Andrew_Luck, KC
Andrew_Luck, IND
Andy_Dalton, IND
Andy_Dalton, CIN
Austin_Davis, CIN
Austin_Davis, STL
Ben_Roethlisberger, STL
Ben_Roethlisberger, PIT
Blaine_Gabbert, PIT
Blaine_Gabbert, SF
Blake_Bortles, SF
Blake_Bortles, JAC
Brandon_Weeden, JAC
Brandon_Weeden, DAL
Brian_Hoyer, DAL
Brian_Hoyer, CLE
Brock_Osweiler, CLE
Brock_Osweiler, DEN
Cam_Newton, DEN
Cam_Newton, CAR
Carson_Palmer, CAR
Carson_Palmer, ARI
Case_Keenum, ARI
Case_Keenum, HOU
Chad_Henne, HOU
Chad_Henne, JAC
Charlie_Whitehurst, JAC
Charlie_Whitehurst, TEN
Chase_Daniel, TEN
Chase_Daniel, KC
Christian_Ponder, KC
Christian_Ponder, MIN
Colin_Kaepernick, MIN
Colin_Kaepernick, SF
Colt_McCoy, SF
Colt_McCoy, WAS
Connor_Shaw, WAS
Connor_Shaw, CLE
Derek_Anderson, CLE
Derek_Anderson, CAR
Derek_Carr, CAR
Derek_Carr, OAK
Drew_Brees, OAK
Drew_Brees, NO
Drew_Stanton, NO
Drew_Stanton, ARI
EJ_Manuel, ARI
EJ_Manuel, BUF
Eli_Manning, BUF
Eli_Manning, NYG
Geno_Smith, NYG
Geno_Smith, NYJ
Aaron_Rodgers, NYJ
Aaron_Rodgers, GB
AJ_McCarron, GB
AJ_McCarron, CIN
Alex_Smith, CIN
Alex_Smith, KC
Alex_Tanney, KC
Alex_Tanney, TEN
Andrew_Luck, TEN
Andrew_Luck, IND
Andy_Dalton, IND
Andy_Dalton, CIN
Austin_Davis, CIN
Austin_Davis, CLE
B.J._Daniels, CLE
B.J._Daniels, HOU
Ben_Roethlisberger, HOU
Ben_Roethlisberger, PIT
Blaine_Gabbert, PIT
Blaine_Gabbert, SF
Blake_Bortles, SF
Blake_Bortles, JAC
Brandon_Weeden, JAC
Brandon_Weeden, HOU
Brian_Hoyer, HOU
Brian_Hoyer, HOU
Brock_Osweiler, HOU
Brock_Osweiler, DEN
Cam_Newton, DEN
Cam_Newton, CAR
Carson_Palmer, CAR
Carson_Palmer, ARI
Case_Keenum, ARI
Case_Keenum, STL
Charlie_Whitehurst, STL
Charlie_Whitehurst, IND
Chase_Daniel, IND
Chase_Daniel, KC
Colin_Kaepernick, KC
Colin_Kaepernick, SF
Colt_McCoy, SF
Colt_McCoy, WAS
Dan_Orlovsky, WAS
Dan_Orlovsky, DET
Derek_Anderson, DET
Derek_Anderson, CAR
Derek_Carr, CAR
Derek_Carr, OAK
Drew_Brees, OAK
Drew_Brees, NO
Drew_Stanton, NO
Drew_Stanton, ARI
EJ_Manuel, ARI
EJ_Manuel, BUF
Eli_Manning, BUF
Eli_Manning, NYG
Geno_Smith, NYG
Geno_Smith, NYJ
Could someone point me to the right direction of what I’m doing wrong?
Upvotes: 1
Views: 1897
Reputation: 74018
What happens here is you reuse the already filled qb_info player_info
.
First you insert "Aaron_Rodgers" and print it
Aaron_Rodgers,
Next you add "GB" to player_info
Aaron_Rodgers, GB
Next you keep "GB" and insert "Alex_Smith"
Alex_Smith, GB
Next you keep "Alex_Smith" and add "KC"
Alex_Smith, KC
Next you keep "KC" and insert "...", and so on.
You may take @Someprogrammerdude's advice and read both values at once, which will simplify your program to just
std::vector<qb_info> player_data;
qb_info player_info;
while (data_file >> player_info.player >> player_info.team) {
player_data.emplace_back(player_info);
}
Printing can then be done in a simple loop over the vector
for (auto &i : player_data) {
std::cout << i.player << ", " << i.team << '\n';
}
Upvotes: 1
Reputation: 87
You want to push a data structure with two members into your vector, you cannot assign player name and their team separately like that, you only should push back your data structure into the vector when you have both values :
for(int i = 0; i < players_and_team.size(); i++) {
if(i % 2 == 0) {
player_info.player = players_and_team.at(i);
// here you don’t need to push back
} else {
player_info.team = players_and_team.at(i);
player_data.push_back(player_info); // now you have both values of player_info to push into vector
}
}
In this code, after player_info has both player and team data, it is pushed to the vector once.
Upvotes: 0