【发布时间】:2018-01-16 06:47:18
【问题描述】:
正如标题所说,我正在尝试为我制作的网站创建排行榜。我的 c++ 程序的目标是对每个玩家的分数进行排序,然后从最高到最低显示分数,显示与他们的分数相关的名称。我需要帮助的问题是在我对代码中的点进行排序后,在对它们进行排序后,玩家的姓名不再与正确的人匹配。 我无法弄清楚如何在球员数组与分数数组排序后再次配对。所以如果有人能看到我能做什么或任何提示会很棒。
此外,分数来自外部来源,因此每次运行此程序时我都手动输入分数。
#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;
}
【问题讨论】: