Is the format of your file something like?
dad 1
fred flintstone 2
mickey mouse 3
... |
The problem is with
1 2
|
getline(Simon_HiScores,Simon_Names[x]);
Simon_HiScores >> Simon_Scores[x];
|
Is that getline reads the whole line into the string (e.g. "dad 1")
Then operator>> tries to extract an int value from the next line, but it finds a name (here, "fred"), which it cannot read into an int, so it puts the stream into a fail state.
As a result of this, all subsequent reads will also fail. The values you are printing out are just random data in your buffer (if you initialize it to zero you should have just blanks for the names and 0 for the scores).
One solution is to read the names into Simon_Names and then use assorted string methods to find, remove and convert the score, and store it in Simon_Scores (istreamstream comes from <sstream>). Like this (not so nice...):
1 2 3 4 5 6
|
getline(Simon_HiScores,Simon_Names[x]);
unsigned pos = Simon_Names[x].rfind(' '); // find last space
string temp = Simon_Names[x].substr(pos + 1); // copy from after space to temp
Simon_Names[x] = Simon_Names[x].substr(0, pos); // // remove last space onwards
istringstream iss(temp); // use number string to init stringstream
iss >> Simon_Scores[x]; // extract int
|
Note that this code would not remove extra spaces. If you had three spaces between fred flintsone and the score, the name string would end up 2 space, i.e. "fred flintstone ". As your program writes and reads the file, this might not be an issue for you.
But another possibility is to use a different delimiter between the name and the scores, like a colon, which probably more sense here, as it requires less code.
dad:1
fred flintstone:2
mickey mouse:3
... |
Then you can use
1 2 3
|
getline(Simon_HiScores,Simon_Names[x],':'); // read until :
Simon_HiScores >> Simon_Scores[x]; // read an int
Simon_HiScores.ignore(1024, '\n'); // skip rest of line
|
The ignore skips anything after the number until the end of the line, so that getline reads from the start of the next line. If you don't do this, getline will read everything from the first non-whitespace char if finds after the last number, up until the next :, endcluding the new line character, '\n'. So the second name would be (in C++ form) "\nfred flintstone" rather than "fred flintstone".
For future reference, mixing up formatted (extraction using operator>>) and unformatted (getline, get, ...) read operations can sometimes confuse istream/ifstream, so you might always get away with the most obvious "solution".
Andy