C++ C++;:用户输入姓名和分数、姓名未知数、比较分数
我需要一些家庭作业方面的帮助。我需要创建一个程序,从用户那里获取名称,然后对这些名称要求5分。输入的名称数目未知。在用户输入完每个姓名和分数后,程序需要在删除高分和低分后计算平均分数。例如,用户输入“Nick”,分数为8,7,6,5,4。分数8和4将被删除,平均值将从5,6,7中计算出来。 我的问题是: 如何将输入名称与其对应的分数连接起来? 如何修复此错误“变量“scoreArray”周围的堆栈已损坏?” 下面是我到目前为止的代码C++ C++;:用户输入姓名和分数、姓名未知数、比较分数,c++,arrays,C++,Arrays,我需要一些家庭作业方面的帮助。我需要创建一个程序,从用户那里获取名称,然后对这些名称要求5分。输入的名称数目未知。在用户输入完每个姓名和分数后,程序需要在删除高分和低分后计算平均分数。例如,用户输入“Nick”,分数为8,7,6,5,4。分数8和4将被删除,平均值将从5,6,7中计算出来。 我的问题是: 如何将输入名称与其对应的分数连接起来? 如何修复此错误“变量“scoreArray”周围的堆栈已损坏?” 下面是我到目前为止的代码 #include <iostream> #incl
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
double calcAvgScore(int, int, int, int, int);
int checkValid(int);
int findLowest(int, int, int, int, int);
int findHighest(int, int, int, int, int);
int main() {
string contestant;
int score, i, arrayPos = 0;
int scoreArray[5] = {};
double avgScore;
cout << "Enter the name of the star. Enter Done if no more stars.\n";
cin >> contestant;
while (contestant != "Done" && contestant != "done")
{
for (i = 1; i < 6; i++)
{
cout << "Enter judge " << i << " score: ";
cin >> score;
checkValid(score);
scoreArray[arrayPos] = score;
++arrayPos;
}
avgScore = calcAvgScore(scoreArray[0], scoreArray[1], scoreArray[2],
scoreArray[3], scoreArray[4]);
cout << "Average Score " << setprecision(2) << fixed << avgScore << endl;
cout << endl;
cout << "Enter the name of the star. Enter Done if no more stars.\n";
cin >> contestant;
}
system("pause");
return 0;
}
double calcAvgScore(int score1, int score2, int score3, int score4,
int score5) {
int low, high, totalScore;
low = findLowest(score1, score2, score3, score4, score5);
high = findHighest(score1, score2, score3, score4, score5);
totalScore = score1 + score2 + score3 + score4 + score5 - low - high;
double avgScore = double(totalScore) / 3;
return avgScore;
}
int checkValid(int score) {
while (score < 1 || score > 10)
{
cout << "Please enter a valid score: ";
cin >> score;
}
return score;
}
int findLowest(int score1, int score2, int score3, int score4, int score5) {
int low = 11;
int array[5] = { score1, score2, score3, score4, score5 };
for (int i = 0; i < 5; i++)
{
if (array[i] < low)
{
low = array[i];
}
}
return low;
}
int findHighest(int score1, int score2, int score3, int score4, int score5) {
int high = 0;
int array[5] = { score1, score2, score3, score4, score5 };
for (int i = 0; i < 5; i++)
{
if (array[i] > high)
{
high = array[i];
}
}
return high;
}
#包括
#包括
#包括
使用名称空间std;
双VGSCORE(int,int,int,int,int,int);
int checkValid(int);
int findLowest(int,int,int,int,int,int);
int findHighest(int,int,int,int,int,int);
int main(){
弦乐选手;
积分i,arrayPos=0;
int scoreArray[5]={};
双avgScore;
参赛者;
while(参赛者!=“完成”&参赛者!=“完成”)
{
对于(i=1;i<6;i++)
{
cout您应该学习使用调试器并逐步完成代码,以便了解发生了什么。循环
while (contestant != "Done" && contestant != "done")
{
for (i = 1; i < 6; i++)
{
cout << "Enter judge " << i << " score: ";
cin >> score;
checkValid(score);
scoreArray[arrayPos] = score;
++arrayPos;
}
// ....
while(参赛者!=“完成”和参赛者!=“完成”)
{
对于(i=1;i<6;i++)
{
库特
如何将输入名称与其对应的分数连接起来
您可以使用std::map
从名称映射到输入分数(如果平均分数是您想要映射到的)
如何修复此错误“变量“scoreArray”周围的堆栈已损坏?”
此时,应将arrayPos
变量归零:
arrayPos = 0;
for (i = 1; i < 6; i++) {
// ...
}
arrayPos=0;
对于(i=1;i<6;i++){
// ...
}
现在arrayPos
变量可以很容易地越界(即变为>4),您将写入堆栈(即scoreArray
已分配的位置),尽管您不允许这样做。"变量“scoreArray”周围的堆栈已损坏?
对于什么输入,您会得到此错误?我使用了下面的建议,在For循环之前将arrayPos设置为0。这就纠正了它。更改后代码看起来很好。我对arrayPos执行了您的建议,它似乎已修复了我得到的错误。有没有办法避免使用ma我们的课本只写了几章(介绍课)它没有提到映射。好吧,你可以简单地将std::string
s存储在一个数组中,将分数存储在另一个数组中,这样相应的索引将匹配相同的对象/参赛者。好的,我认为这是一个更好的选择。那么我如何为未知数量的名称声明数组呢?目前visual studio正在显示“string”的错误name[];“好的,如果名称的数量未知,请使用std::vector
。好的,因此我插入了以下“vector names{};”,然后添加了“names.push_back(参赛者))在for循环开始之前。我假设这是将输入的名称添加到向量中的位置。我实际上如何链接向量和数组?在我看来,在for循环之后,刚刚写入的数组似乎被擦除以输入下一个数组。修复了我得到的错误。有没有办法避免使用map?我们只有几章在我们的课本(介绍课)上写了一篇文章,但没有提到map@WillEstes你需要“模拟”然后,查看关联数组、查找表或哈希表以获取更多信息。在您的示例中,您可以定义两个数组,一个带有名称,存储std::string
,一个带有分数,存储int
,并确保第i个名称被称为names[i]
,相应的分数为scores[i]
。因此,我所做的是创建两个向量,一个用于名称,一个用于平均分数。如何将这两个向量链接起来,并确定哪个名称的平均分数最大?例如,Vector1.at(0)的平均分数为Vector2.at(0).然后检查平均分数的所有条目,以确定最大的条目,并将其与相应的name@WillEstes好吧,你只要确保名字[i]
对应于分数[i]
,这就是你连接它们的方式。例如,玩家“约翰”由名字[0]
识别,并且有分数分数[0]
。我希望这是有意义的。如果你愿意,你甚至可以创建一个struct Player{string name;int score;};
并定义一个Player
s的向量,但为此你需要知道如何使用struct
s(或类)。不幸的是,对于这个类,我们没有进入类。我想我理解我必须做什么的概念,我只是不知道语法。我会使用for循环连接名称[I]
和分数[I]
?
arrayPos = 0;
for (i = 1; i < 6; i++) {
// ...
}