-
Highscore system
Hi!
I have this code:
Code:
int file_write(int points)
{
int dd = 0;
string high;
ifstream in( "highscore.txt" );
string name[5];
string point[5];
while (in >> high) {
if (is_even(dd)) {
name[dd] = high;
} else {
point[dd] = high;
}
dd++;
}
cout << point;
in.close();
cin.get();
cin.get();
return 0;
}
I want to check if the point of the player is better than someone in the highscore list. How the hell do i do that? All that happen now is that it sais that it doesn't answer. :( The players points is calles points. And also, i store the highscore like this:
Fredrik, 5
Fredrik, 3
Erik, 3
Nobody, 1
Anybody, 1
Please help me! :D
-
Is your high score list sorted when it is read from the file? If so, you can compare the points passed into the function with the points you read in from the file until you find points that are lower than the ones passed in.
To do that, the first change you'll have to make is changing the point container to hold ints, not strings, since points are really integers. Then when you read in a point value you can compare it with the points variable passed in.
BTW, This code:
Code:
while (in >> high) {
if (is_even(dd)) {
name[dd] = high;
} else {
point[dd] = high;
}
dd++;
}
can be re-written as this:
Code:
while (in >> name[dd] >> point[dd]) {
dd++;
}
Although you'll want to make other changes to that loop to handle your current problem.
-
How can i now compare the points?
-
-
Uh...
Why is points called into the function and never used?
In your function header, it says "int points", but you never use that. THAT is probably why you're having a hard time if you're completely forgetting to use the point total of the player :S. Other than that, I didn't thouroghly check your code. Simple do what he said, skip the name when you read in by locating the comma, read the number, skip the name, read the number, ect... On top of that, it doesn't matter tons whether it's int or string, it'll be put into the text file the same, and read out the same way as he's currently reading it out, whether the output in the file was originally "string" or "int", since a text file doesn't know the difference...
-
Here's my code....
Code:
int file_write(int points, string names)
{
int dd = 0;
int po;
int done = 0;
string high;
ifstream in( "highscore.txt" );
string name[5];
string point[5];
while (in >> name[dd] >> point[dd]) {
StringToInt(point[dd], po);
if (points > po && done == 0) {
name[dd] = names;
point[dd] = points;
done = 1;
}
cout << name[dd] << " " << point[dd] << "\n";
dd++;
}
in.close();
cin.get();
cin.get();
return 0;
}
It's really wierd because the new points from the player becomes a heart. :S
Why? :o
-
Why are you using StringToInt? Why not just make point[5] an array of ints instead of strings? (BTW blurrymadness, it does matter, because the point input must be compared to the points parameter which is an int.)
You should add something to your while loop to not continue when dd is 5 because that is past the bounds of your array.
Also, you need to move down the name/points pair that got bumped. Right now you just write over it. If you enter your if, you should save the stored name and points, then save the current player's data, then, if there is room in the array, increment dd and place the bumped name and points into that next slot.
-
I put there this code:
Code:
if (dd == 5) {
break;
}
and now all works fine! :D Super!
-
Glad the code is working. Be careful, though. You could still get a crash depending on where you put it.
If you put it at the beginning of your loop, then you might be reading into name[5] and point[5] which is illegal. If you put it at the end of your loop, after the dd++, then you should be ok because dd will be 4 the previous time you use it and it will break the loop as soon as it is 5 (so you don't accidentally read into position 5).