The usual method is to keep a little data file (a simple text file would do nicely) that lists the top
n high-scores and player names and whatever else you want. An example file (with five high-scores) might look something like this:
1 2 3 4 5
|
23400 Snoopy
3600 Linus VanDerPelt
1200 Lucy VanDerPelt
900 Sally Brown
100 Charlie Brown
|
In your code, every time a game ends, update your list of high-scores (that you have in memory) and write them to the high-scores file (on disk).
When the game starts, initialize your list of high-scores to zero, no-name, etc., then look for the file on disk and, if it is there, read them into the list.
Hope this helps.
Oh yeah, almost forgot:
When reading strings from a text file, use
getline(), not
>>. So to read a score might be:
1 2 3 4 5 6 7 8 9 10
|
ifstream f( 'highscores.txt' );
string name;
for (
int i = 0;
f >> high_scores[ i ].score;
i++)
{
f.get(); // skip the space
getline( f, high_scores[ i ].name );
}
|
This example is completely devoid of error checking just to make things simple. Here it is with error checking:
1 2 3 4 5 6 7 8 9 10
|
ifstream f( 'highscores.txt' );
string name;
for (
int i = 0;
(i < (sizeof( high_scores ) / sizeof( high_scores[ 0 ] ))) and (f >> high_scores[ i ].score);
i++)
{
f.get(); // skip the space
getline( f, high_scores[ i ].name );
}
|
The sizeof/sizeof trick is to handle whatever the size of the array you create without allowing overflow.
Hope this helps.