By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,261 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

High Score File I/O

P: n/a
I am trying to save the high scores of a game I made. Now Im stumped on
trying to search through the file. Should I use a string, array, or one of
the STL containers to manipulate the information read from the file?

I have tried the
stringname.find("Name");

This only returns the first occurence, and doesn't look for other
occurences.

I got better results just using
if(Name[i] = = NewName)
The file is a simple record like so:

Name wins loses //where wins and loses are int's
Bob 1 0 //example of format

Its to be like an old Pac-Man game record keeper.
I want to convert the name into lowercase and check to make sure only one
name is present in the file. If more than one name erase the others
otherwise check to see if WinsOnFile < CurrentWins.
If so record the new high score to the appropriate name.

This will only hold 10 high scores for different people.

Any thoughts would be helpful,
thanks
Dec 28 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"kyle christian" <sa*****@cox.netwrote in message
news:v8*******************@newsfe12.phx...
>I am trying to save the high scores of a game I made. Now Im stumped on
trying to search through the file. Should I use a string, array, or one of
the STL containers to manipulate the information read from the file?

I have tried the
stringname.find("Name");

This only returns the first occurence, and doesn't look for other
occurences.

I got better results just using
if(Name[i] = = NewName)
The file is a simple record like so:

Name wins loses //where wins and loses are int's
Bob 1 0 //example of format

Its to be like an old Pac-Man game record keeper.
I want to convert the name into lowercase and check to make sure only one
name is present in the file. If more than one name erase the others
otherwise check to see if WinsOnFile < CurrentWins.
If so record the new high score to the appropriate name.

This will only hold 10 high scores for different people.

std::string has all the functionality you need for this:

Read the file into a string.
Before writing a high score see if the name exists.
If so, move to the wins and losses part of the record
use std::string replace, or another one of its methods to replace wins and
losses for that name.
Write the string back to the file.
Done.

If you abide by that algorithm, I don't see more than one occurence of the
name ever being in the file.
If for some other reason it is than you can search in a loop until no
occurences are found. std::string has overloaded find methods that can take
an index to start from.

If you want to be more eligent, you could make a record class that holds
name, wins and losses. Then give it serialize and deserialize methods, which
read and write one line of text as a record. Provide a functor that returns
the name of the record. Then when the game loads you can read in the file
using a container of records searchable by name (std::map) . When the game
is over or when it exits. Write the map of records over the previous file.

gl,
Christopher

Dec 28 '07 #2

P: n/a
kyle christian wrote:
I am trying to save the high scores of a game I made. Now Im stumped
on trying to search through the file. Should I use a string, array,
or one of the STL containers to manipulate the information read from
the file?
I have tried the
stringname.find("Name");

This only returns the first occurence, and doesn't look for other
occurences.

I got better results just using
if(Name[i] = = NewName)
The file is a simple record like so:

Name wins loses //where wins and loses are int's
Bob 1 0 //example of format

Its to be like an old Pac-Man game record keeper.
I want to convert the name into lowercase and check to make sure only
one name is present in the file. If more than one name erase the
others otherwise check to see if WinsOnFile < CurrentWins.
If so record the new high score to the appropriate name.

This will only hold 10 high scores for different people.

Any thoughts would be helpful,
thanks
One easy way is to use a std::map. std::map has the advantage that if you
use code like this:

std::map<std::string, MyClassData;
Data["bob"] /*...*/
If "bob" doesn't already exist in the map, it will add it. Otherwise it
will refer to the one that already exists. For simplicity sake lets say you
were just recording number of wins.

std::map<std::string, intScores;
std::string Name;
int Wins;
/* Read name and Wins from file */
Scores[name] += Wins;

Now if there are 20 "Bob"s in the file, they will be added to one Score for
wins.

Things to do:
1. Convert name read to lowercase.
2. Store a structure or std::pair to represent wins and loses and not just
loses.
--
Jim Langston
ta*******@rocketmail.com
Dec 28 '07 #3

P: n/a
Thanks for the help guys. The map container works good. Heres what I got
after reading up on it so far:
//Gets user Name to use in file

void BlackJack::GetName(){
cout << "Enter your name: ";
cin >Name;
UserInput();
}
//Adds record to the file

void BlackJack::RecordStats(){
ofstream InFile("c:/Dev-Cpp/text.txt", ios::app);
if(InFile){InFile << Name << " " << Won;}
else if(!InFile) { cout << "\nCouldn't open file";}
InFile.close();

}
void BlackJack::GetStats(){
//Write stats to file

RecordStats();

//Read the file into the map if open for reading

ifstream OutFile("c:/Dev-Cpp/text.txt");
if(!OutFile){
cout << "\nCouldn't Read File";
}
else{
map<string, int>Score;
string N;
int W;
while(!OutFile.eof()){
OutFile >N >W;
Score.insert(make_pair(N,W));
}
OutFile.close();

//Since the map only allows one key with the same name and if two keys of
the same name, it takes the key with the greater value by default
//I simply read the file into the map container so it could organize it for
me, and wrote it back to the file. There is probably a better way
//just dont know it yet
map<string, int>::iterator itr,itr1;
itr = Score.begin();

ofstream InFile("c:/Dev-Cpp/text.txt");
while(itr != Score.end()){
InFile << itr->first << " " << itr->second << endl;
itr++;
}
InFile.close();

itr1 = Score.begin();
while(itr1 != Score.end()){
cout << itr1->first << " wins: " << itr1->second << endl;
itr1++;
}

}
}
void BlackJack::DisplayStats(){
cout << " VarWin: " << Won << " VarLost: " << Lost << endl;
GetStats();
}
Only thing I lack now is limiting the size to only the top 10 scores and
instead of the list being organized alphabetically I need the map to be
organized according to highest score.



"Jim Langston" <ta*******@rocketmail.comwrote in message
news:Gl************@newsfe06.lga...
kyle christian wrote:
>I am trying to save the high scores of a game I made. Now Im stumped
on trying to search through the file. Should I use a string, array,
or one of the STL containers to manipulate the information read from
the file?
I have tried the
stringname.find("Name");

This only returns the first occurence, and doesn't look for other
occurences.

I got better results just using
if(Name[i] = = NewName)
The file is a simple record like so:

Name wins loses //where wins and loses are int's
Bob 1 0 //example of format

Its to be like an old Pac-Man game record keeper.
I want to convert the name into lowercase and check to make sure only
one name is present in the file. If more than one name erase the
others otherwise check to see if WinsOnFile < CurrentWins.
If so record the new high score to the appropriate name.

This will only hold 10 high scores for different people.

Any thoughts would be helpful,
thanks

One easy way is to use a std::map. std::map has the advantage that if you
use code like this:

std::map<std::string, MyClassData;
Data["bob"] /*...*/
If "bob" doesn't already exist in the map, it will add it. Otherwise it
will refer to the one that already exists. For simplicity sake lets say
you were just recording number of wins.

std::map<std::string, intScores;
std::string Name;
int Wins;
/* Read name and Wins from file */
Scores[name] += Wins;

Now if there are 20 "Bob"s in the file, they will be added to one Score
for wins.

Things to do:
1. Convert name read to lowercase.
2. Store a structure or std::pair to represent wins and loses and not just
loses.
--
Jim Langston
ta*******@rocketmail.com

Dec 30 '07 #4

P: n/a
[snip]
Only thing I lack now is limiting the size to only the top 10 scores and
instead of the list being organized alphabetically I need the map to be
organized according to highest score.
[snip]

if( mymap.size() 10 )
{
// use some combination of mymap.find() and mymap.erase()
}

To sort by high score. Again, use std::sort and a mymap functor if needed.

,
Chris
Jan 1 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.