C++:用户输入姓名和分数,姓名数量未知,比较分数
C++ : User Input of Names and Scores, unknown number of names, compare scores
我需要一些家庭作业方面的帮助。我需要创建一个程序,从用户那里获得名字,然后要求为这些名字打5分。输入的姓名数量未知。用户输入完每个名字和分数后,程序需要计算掉高分和低分后的平均分数。例如,用户输入"Nick"w/scores 8,7,6,5,4。分数8和4将被降低,平均值将从5、6、7中计算出来。我的问题是:如何将输入的名称与其相应的分数联系起来?如何修复此错误"变量'scoreArray'周围的堆栈已损坏?"以下是我迄今为止的代码。
#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;
}
更正代码:
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
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, y, arrayPos;
int scoreArray[5] = {};
vector<string> names{};
double avgScore, maxAvg;
vector<double> avgScoreVec{};
cout << "Enter the name of the star. Enter Done if no more stars.n";
cin >> contestant;
while (contestant != "Done" && contestant != "done")
{
names.push_back(contestant);
arrayPos = 0;
for (i = 1; i < 6; i++)
{
cout << "Enter judge " << i << " score: ";
cin >> score;
score = checkValid(score);
scoreArray[arrayPos] = score;
++arrayPos;
}
avgScore = calcAvgScore(scoreArray[0], scoreArray[1], scoreArray[2],
scoreArray[3], scoreArray[4]);
avgScoreVec.push_back(avgScore);
cout << endl;
cout << "Enter the name of the star. Enter Done if no more stars.n";
cin >> contestant;
}
maxAvg = *max_element(avgScoreVec.begin(), avgScoreVec.end());
y = find(avgScoreVec.begin(), avgScoreVec.end(), maxAvg) - avgScoreVec.begin();
cout << "... and the winner is " << names[y] << " with a score of " <<
setprecision(2) << fixed << maxAvg << endl;
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::map<string, double>
从名称映射到输入分数(如果您想要映射到平均分数)。
我如何修复这个错误"变量‘scoreArray’周围的堆栈已损坏?"
您应该将arrayPos
变量设置为零,此时:
arrayPos = 0;
for (i = 1; i < 6; i++) {
// ...
}
现在,arrayPos
变量可以很容易地越界(即变为>4),并且您将在堆栈上写入(即分配了scoreArray
的位置),尽管不允许这样做。
您应该学习使用调试器并逐步完成代码,这样您就可以了解发生了什么。环路
while (contestant != "Done" && contestant != "done")
{
for (i = 1; i < 6; i++)
{
cout << "Enter judge " << i << " score: ";
cin >> score;
checkValid(score);
scoreArray[arrayPos] = score;
++arrayPos;
}
// ....
是可疑的。首先,如果您不键入"done"或"done",您将不断递增arrayPos
,其值将大于4,因此您最终会访问越界。其次,checkValid(score);
应该是score = checkValid(score);
,否则score
保持不变(它是函数checkValid()
的本地)。如果您想要一个可调整大小的数组,请考虑使用std::vector
。也可以考虑使用std::map
。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么比较运算符如此快速
- 继承期间显示未知行为的子类
- 我可以使用 g++ 进行三种比较 (<=>) 吗?
- 比较字符数组
- 将模板化的类型与C++中的某些类/类型进行比较
- 输入中的字符串数未知(以字母表示)
- C++自定义比较函数
- 如何比较自定义类的std::变体
- 具有未知值时的时间复杂性
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- std::设置自定义比较器
- 布尔比较运算符是如何在C++中工作的
- C++将目录中的所有文件与::filesystem进行比较
- shell排序中的交换和比较
- 如何在C++中比较两个char数组
- catch框架有没有办法比较流或文件
- 比较两个整数在C++中与未知 int 类型的相等性
- 将字符串中的元素与字符进行比较时出现未知错误
- C++:用户输入姓名和分数,姓名数量未知,比较分数