Hey. I need to read from a file called ranking.txt the name of the players and the respective score. The file is something like this:
Code:
24145 John Burt
12442 Kate Blair
1244 Jacob
Then, I need to save the data into a vector, best_scores, defined by a data struct.
Code:
struct Score {
string name;
int score;
};
vector<Score> best_scores;
Can you help me?
Btw, I did this:
Code:
#include <iostream>
#include <fstream>
#include<string>
#include<vector>
#include <sstream>
using namespace std;
struct Score{
string name;
int score;
}a, b, c, d, e, f, g, h, i, j;
vector<Score> best_scores;
int main () {
char c, str[256] = "ranking.txt";
ifstream is;
is.open (str); // open file
string b;
while (is.good()) // loop while extraction from file is possible
{
c = is.get(); // get character from file
if (isdigit(c))
{
b += c;
}
else
if (isalpha(c))
{
a.name += c;
}
stringstream s(b);
s >> a.score;
}
cout << a.score<< endl;
cout << a.name << endl;
is.close();
return 0;
}