I have a file named "SCORES.TXT" that contains this:
Player - Score
--------------------
John Miles - 132
Henry - 90
Juliet P - 110
The program must show to the user the person name and the respective score, like:
John Miles has a score of 132
Henry has a score of 90
Juliet P has a score of 110
I have the following code but it is not working properly. The variable nickname only gets the first name, and if I add a string variable to get the second name, the programm will not work in the lines that only have one name.
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
string fileName = "SCORES.TXT", line, nickname;
char c;
unsigned int score;
unsigned short int i = 0;
ifstream file(fileName);
while (getline(file, line)) {
if (i < 2) { //
i++; // ignoring the header
} //
else {
stringstream s(line);
s >> nickname >> c >> score;
cout << nickname << " has a score of " << score;
}
}
file.close();
return 0;
}