I'm trying to read in a line of the file, print out parts of the line, and repeat the process for the next line of the file.
but it doesn't work properly, when I push the output to cmd line it becomes apparent that the while loop is never exitting.
I also tried the same process just using getline() instead, but I have the same problem, it just reads and re-reads the file infinitely.
Here is my code:
fstream scores;
scores.open("scores.txt");
string playerName;
string playerScore;
int i = 0;
while (scores >> playerName >> playerScore && i < 8) {
int line = i * 40;
int rank = i + 1;
// testing
cout << rank << " " << playerName << " " << playerScore << "\n";
char plRank[1];
sprintf(plRank, "%i", (rank));
char plName[8];
sprintf(plName, "%s", "Name");
char plScore[8];
sprintf(plScore, "%s", "score");
DrawScreenString(GAX1 + 20, GAY1 + 120 + line, plRank, 0x505050, pBody);
DrawScreenString(GAX1 + 320, GAY1 + 120 + line, plName, 0x505050, pBody);
DrawScreenString(GAX1 + 570, GAY1 + 120 + line, plScore, 0x505050, pBody);
i++;
}
scores.close();
Here is an extract from the cmd line output.
1 Edward 100
2 Jodi 80
3 Tom 50
4 Emma 40
1 Edward 100
2 Jodi 80
3 Tom 50
4 Emma 40
1 Edward 100
2 Jodi 80
3 Tom 50
4 Emma 40
1 Edward 100
2 Jodi 80
3 Tom 50
4 Emma 40
1 Edward 100
2 Jodi 80
3 Tom 50
4 Emma 40
1 Edward 100
2 Jodi 80
3 Tom 50
4 Emma 40
1 Edward 100
2 Jodi 80
3 Tom 50
4 Emma 40
...
this is what the scores.txt file cotains
Edward 100
Jodi 80
Tom 50
Emma 40
Any suggestions of why it infinitely loops or fixes would be greatly appreciated!