i made a function to check wheter the player name is found in the file before or not and if not it should append his name and score in file if he found his name just to update his score
but there is 2 problems her
1- it always append the name whether its found before or not
2- the name and score are attached to each other i want spaces between them in the file
hope to find the problem
and thnxxx
CODE
struct highscore // structure to have the name and score of each player
{
string name;
int score;
};
// End of the Structure
// Function to add the name and score for player in the level played
void append (string file, string target , int s)
{
highscore y;
y.name=target;
y.score=s;
ofstream x;
x.open((file.c_str()),ios::app);
x<<y.name<<y.score;
x.close();
}
// End of the function
// Function to check the player's name wheter found before or not
// If found update his score
// If not found create a new score for him
void check_player_name(string file, string target , int s)
{
int i=0;
ofstream q;
ifstream r;
r.open((file.c_str()));
highscore b[10];
while (r.is_open() && !r.eof())
{
r>> b[i].name >> b[i].score;
i++;
}
r.close();
for(i=0;i<10;i++)
{
if (target.compare(b[i].name) == 0)
break;
}
if ( i == 10)
append (file , target , s);
else
{
q.open((file.c_str()));
b[i].score<<s;
for (int j=0; j<10; j++)
{
q << b[j].name <<b[j].score;
}
q.close();
}
}
// End of the function