Thread: Highscore system

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    188

    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!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    How can i now compare the points?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    With < or > ?

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    27

    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...

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    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?
    Last edited by Livijn; 04-21-2007 at 03:37 AM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    I put there this code:
    Code:
              if (dd == 5) {
                   break;
              }
    and now all works fine! Super!

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operating System
    By Houssen in forum C Programming
    Replies: 18
    Last Post: 04-22-2008, 12:51 PM
  2. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 01:04 PM
  3. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 07:56 PM
  4. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM