1

So as the title says I am trying to create a leader board for a website I made. The goal of my c++ program was to sort the points for each player and then display the points from highest to lowest showing the name that correlates with their score. The problem that I need help with is after i sorted the points in the code, after they are sorted the player's name is no longer matched to the correct person. I cant figure out how to pair the player array with the score array again after they have been sorted. So if anyone could see what i could do or any tips would be amazing.

Also, the scores come from an outside source so i manually input scores each time this program is run.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>


const int MAX_NAMES = 41;

using namespace std;

int main()
{
int points[MAX_NAMES];
int j = 1;

// Array of names of each person on the leaderboard. 
string names[]
{
    "Austin ",
    "Jarred ",
    "Cameron ",
    "Mike ",
    "Blake ",
    "Mitch ",
    "Juan ",
    "Justus ",
    "Avery ",
    "Nick ",
    "Garrett ",
    "Dillion ",
    "Ryan ",
    "Andrew ",
    "Brendan ",
    "Justin ",
    "Jared ",
    "Steve ",
    "Dylan ",
    "Kylor ",
    "Ian ",
    "Josh ",
    "Jake ",
    "Kevin ",
    "Nick ",
    "Marco ",
    "Patrick ",
    "Danny ",
    "Jay ",
    "Bryson ",
    "Mitchell ",
    "Noah ",
    "Tyler ",
    "Andrew ",
    "Evan ",
    "Casey ",
    "Mikey ",
    "Hunter ",
    "Luke ",
    "Colton ",
    "Harbir ",
};

// 1. Manually input score for each person and saves it into array. 
cout << right << setw(50) << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" << endl;
cout << right << setw(55) << "INPUT TOTAL BETA POINT SCORE" << endl;
cout << right << setw(50) << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" << endl;

for (int i = 0; i < MAX_NAMES; i++)
{
    cout << right << setw(40) << names[i] << " : ";
    cin >> points[i];
}


// 2. organizes from highest to lowest
for (int k = 40; k >= 0; k--)
{
    for (int x = 0; x < MAX_NAMES; x++)
    {
        if (points[x] < points[x + 1])
        {

            int temp = points[x + 1];

            points[x + 1] = points[x];
            points[x] = temp;
        }

    }
}


cout << right << setw(50) << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" << endl;
cout << right << setw(35) << "SORTED" << endl;
cout << right << setw(50) << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" << endl;

for (int i = 1; i < MAX_NAMES; i++)
{
    cout << i << ") " << points[i] << endl;
}


// 3. Output totals into a html formatted file.
//ofstream outfile;
//outfile.open("total.txt")


system("pause");
return 0;
}
Kyle Knapp
  • 13
  • 3

3 Answers3

2

One solution to this problem is to create a pair<int, string> to store both the score and the player name in the same data element, then store those in whatever array type suits you (I would recommend std::vector, but I will use your choice of a simple array in my example). As noted in previous answers, pair<T1, T2> has an overloaded operator< defined that compares the first element first, then the second element. std::sort will take advantage of that:

#include<utility>   // for pair
#include<algorithm> // for sort
#include<string>
#include<iostream>

int main() {
    int MAX_NAMES = 3; // example
    std::pair<int, std::string> scoreboard[MAX_NAMES] = {
        {22, "Anna"}, {11, "Bo"}, {33, "Clare"} // using c++11 inline initialization
    };

    std::sort(scoreboard, scoreboard + MAX_NAMES); // sorts in place using operator<

    for (int i = 0; i < MAX_NAMES; ++i) {
        std::cout << i << ") " << scoreboard[i].second << " has " << scoreboard[i].first << " points" << std::endl;
    }
    return 0;
}

Result:

0) Bo has 11 points
1) Anna has 22 points
2) Clare has 33 points

You can reverse this list by simply iterating backward through scoreboard. You can see a working example here.

PaSTE
  • 4,050
  • 18
  • 26
1

Thinking in the future of your project, you should rely on encapsulation. Create a Player class with the properties name and points, in the future you might want to add more functionality to your players! And you don't want to break your code, so encapsulation is the way to go.

You should try to use the algorithm library, it will make your code more expressive, and thus easier to maintain.

Try to use standard containers to manage your memory. The usual recommendation is to use vector for dynamic memory.

Here you have an example of a simple implementation of these tips, try your own!

#include<algorithm>
#include<vector>
#include<string>
#include<iostream>

class Player {
    std::string name;
    int points;
    public:
    Player(std::string name, int points) : name(name), points(points) {}
    bool operator<(const Player &rhs) const {return points < rhs.points;}
    std::string get_name() const {return name;}
    int get_points() const {return points;}
};

int main() {
    std::vector<Player> players;
    players.push_back(Player("Pedro",50));
    players.push_back(Player("Andres",20));
    players.push_back(Player("Santiago",57));

    std::sort(players.begin(),players.end()); 

    for (const auto& player : players) {
        std::cout << player.get_name() << ": " << player.get_points() << "\n";
    }
}

Here I use the sort algorithm to sort the players in the vector. Sort uses the operator <, that's why you see the operator overload in the Player class. This tells how (player1 < player2) should behave (in our case, it's just comparing their points. But you could do anything, such as adding a bonus property to the Player class)

Another recommendation is to separate the Player's class into a different file to keep things more structured.

Blasco
  • 1,607
  • 16
  • 30
0

You need to sort the lists at the same time to maintain the relative order. Eg,

        int temp = points[x + 1];  

        points[x + 1] = points[x];  
        points[x] = temp;

        string temp_name = names[x + 1];

        names[x + 1] = names[x];
        names[x] = temp_name;
Jay
  • 636
  • 6
  • 19