C++:用户输入姓名和分数,姓名数量未知,比较分数

C++ : User Input of Names and Scores, unknown number of names, compare scores

本文关键字:比较 未知 用户 输入 C++      更新时间:2023-10-16

我需要一些家庭作业方面的帮助。我需要创建一个程序,从用户那里获得名字,然后要求为这些名字打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