C++ : User Input of Names and Scores, 未知数量的名字,比较分数

标签 c++ arrays

我的作业需要一些帮助。我需要创建一个从用户那里获取名字的程序,然后要求这些名字的 5 分。输入的名称数量未知。用户输入完每个名字和分数后,程序需要计算出去掉高分和低分后的平均分。例如,用户输入得分为 8、7、6、5、4 的“Nick”。分数 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;
}

最佳答案

How do I connect the input names with their corresponding scores?

您可以使用 std::map<string, double>从名称映射到输入分数(如果平均分数是您要映射到的分数)。

How do I fix this error "Stack around the variable 'scoreArray' was corrupted?"

你应该将你的arrayPos归零变量,此时:

arrayPos = 0;
for (i = 1; i < 6; i++) {
    // ...
}

现在 arrayPos变量很容易越界(即变为 > 4),并且您将写入堆栈(即 scoreArray 已分配的位置),尽管您不允许这样做。

关于C++ : User Input of Names and Scores, 未知数量的名字,比较分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34032675/

相关文章:

c++ - 函数有损坏的返回值

c++ - 我的 std::exchange 不在命名空间 std 中?

检查是否分配了内存中的某个地址

ruby - 检查数组中是否包含多个项目的好方法

java - 将数组中的元素复制到不同类型

arrays - 看不懂CLRS问题4-2案例2

c++ - 取消设置最右边的设置位

c++ - Foreach 范围迭代 vector<int> - auto 或 auto&?

c++ - 如何在运行时检查 Win API 函数支持?

javascript - 使用 d3.js 在表中显示关联数组的数据