Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,208 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,312 people online right now. Registration is fast and FREE... Join Now!




check player name ?!?!?!

 
Reply to this topicStart new topic

check player name ?!?!?!

quaresma
20 May, 2008 - 02:44 PM
Post #1

New D.I.C Head
*

Joined: 5 May, 2008
Posts: 19

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


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:51PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month