First off, I would like to say that this is an assignment for my CS161 class, so while a direct answer would be nice, a nice explanation will help me more than anything. We have covered structs this week, but I am having trouble with some of my code. My goal right now for the program is to read from a file called "QBInfo.txt" into an array of three structures.
struct QuarterBack{
string name;
int completions[kNumGames];
int attempts[kNumGames];
int yards[kNumGames];
int touchdowns[kNumGames];
int interceptions[kNumGames];
};
QuarterBack players[kNumPlayers] = {player1, player2, player3};
Also, if it helps I should say that kNumPlayers is a constant set to 3, and kNumGames is a constant set to 10.
This is the code I am using to read from my file. It doesn't seem to work right though, and I have been at a loss as to why, so I figured I might ask the community for some help.
ifstream myIF;
myIF.open("QBInfo.txt");
string trash;
string temp;
getline(myIF, trash);
for(int i = 0; i < kNumPlayers; i++){
getline(myIF, temp);
players[i].name = temp;
for(int j = 0; i < kNumGames; j++){
myIF >> players[i].completions[j];
myIF >> players[i].attempts[j];
myIF >> players[i].yards[j];
myIF >> players[i].touchdowns[j];
myIF >> players[i].interceptions[j];
}
}
Here is the test case file that my instructor has provided. The first number is for a challenge portion of the assignment that I will be attempting later due to time restrictions. It is the reason for the getline(myIF, trash)
portion of code before my loop begins.
3
Peyton Manning
27 42 462 7 0
30 43 307 2 0
32 37 374 3 0
28 34 327 4 0
33 42 414 4 1
28 42 295 2 1
29 49 386 3 1
30 44 354 4 3
25 36 330 4 0
24 40 323 1 0
Tom Brady
29 52 288 2 1
19 39 185 1 0
25 36 225 2 1
20 31 316 2 0
18 38 197 0 1
25 43 269 1 1
22 46 228 0 1
13 22 116 1 1
23 33 432 4 0
29 40 296 1 1
Drew Brees
26 35 357 2 1
26 46 322 1 2
29 46 342 3 1
30 39 413 4 0
29 35 288 2 0
17 36 236 2 1
26 34 332 5 0
30 51 382 2 2
34 41 392 4 0
30 43 305 1 1