Reading strings from file to a 2d array?

I have a text file with scores and names.
I have to read the score into an array and the names into an arrays also

27,Tom Jefferson
23,Ozzie Osborne
18,Wilt Chamberlain
15,Marie Antoinette

I've gotten the score to display correctly, but i cant get the full names.
My function only reads the first names



Last edited on
This displays it like:
Tom 27
Ozzie 23
Wilt 18
Marie 15

I need to get the last names too!

Thank Yoi!
closed account (Dy7SLyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ifstream File("scores.txt");
string Line;
vector<string> Names(1);
int Counter = 0;
vector<int> Scores(1);

while(getline(File, Line))
{
	Names[Counter] = Line.substr(1, Line.find(",") - 1);
	Scores[Counter] = Line.substr(Line.size() - Line.find(","), Line.size());
	counter++;
	Names.resize(Counter);
	Scores.resize(Counter);
}
I don't really understand that.
closed account (Dy7SLyTq)
which lines?
Everything except the first and second line. The C++ i've learned so far is very basic. I couldn't tell you what Line.substr means.

But Thanks anyways.

Ill figure it out if i keep trying!
closed account (Dy7SLyTq)
vector is a better array string is a better char array
I've never heard of it.
Line.substr() is a function that returns a string from the position to the end. In this case he is finding the position at the coma, then the function returns everything from that point to the end of the line. He does the same thing with the next line.

He increments the line counter,
and resizes both vectors.

then he loops to do it again.

here is a reference link for std::string.substr(size_t, size_t):
http://www.cplusplus.com/reference/string/string/substr/
Topic archived. No new replies allowed.