Hey there, I'm trying to organize a file that contains the name of the player, and his score.

ex:
kunkunlol 473
marc 94
Susan 134

I have 2 functions, one to read the file (will show the top 10 players like above), and another to write on it (like above too), after the player finishes the game.

Altought, I can't see a way to organize from the biggest to the smallest score!

Any help would be very welcome! Thanks.

You will have to iterate through all the values and sort them.

By googling for half a minute I found this excellent piece of code to sort numbers:

sortme should contain the numbers to sort, and size should contain the count of numbers.

const int size = 0;
int temp;
int sortme[size];

for(int j = size; j > 0; j--)
{
    for(int i = 0; i < size; i++)
    {
        if(sortme[i] > sortme[j])
        {
            temp = sortme[i];
            sortme[i] = sortme[j];
            sortme[j] = temp;
        }
    }
}

Of course you will have to modify it a bit, so you can handle pairs of data (use std::map :)).

Create a structure :

struct Player{
 string name;
 int score;
 Player() : name(), score(){}
};

Define an extraction operator, to extract into the struct :

std::istream& operator >>(std::istream& inFile, Player& p){
 return inFile >> p.name >> p.score;
}

now create a vector of players :

std::vector<Player> players;

now define a binary boolean function to be used with std::sort.

bool sortByScore(const Player& lhs, const Player& rhs){
 return lhs.score < rhs.score;
}

Now read in from the file into the vector of players :

std::vector<Player> players;
ifstream inFile("filename.txt");
//error check
Player tmp;
 while(inFile >> tmp){ players.push_back(tmp); } //read all from file
 std::sort(players.begin(),players.end(),sortByScore); //sort it
 //now do whatever with the sorted player vector

none of the above is compiled, just an idea to get you started.

Create a structure :

struct Player{
 string name;
 int score;
 Player() : name(), score(){}
};

Define an extraction operator, to extract into the struct :

std::istream& operator >>(std::istream& inFile, Player& p){
 return inFile >> p.name >> p.score;
}

now create a vector of players :

std::vector<Player> players;

now define a binary boolean function to be used with std::sort.

bool sortByScore(const Player& lhs, const Player& rhs){
 return lhs.score < rhs.score;
}

Now read in from the file into the vector of players :

std::vector<Player> players;
ifstream inFile("filename.txt");
//error check
Player tmp;
 while(inFile >> tmp){ players.push_back(tmp); } //read all from file
 std::sort(players.begin(),players.end(),sortByScore); //sort it
 //now do whatever with the sorted player vector

none of the above is compiled, just an idea to get you started.

Well that response was througough. Good job :)

>>Well that response was througough. Good job

Do I get a cookie?

Sure, what's your address? :) Or do you have a 3D printer, then I could just do a 3D scan and send it over?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.