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

时间:2015-12-02 00:10:10

标签: c++ arrays

我需要一些家庭作业的帮助。我需要创建一个从用户获取名称的程序,然后要求获得这些名称的5个分数。输入的名称数量未知。在用户完成输入每个姓名和分数后,程序需要在降低高分和低分后计算平均分数。例如,用户输入“Nick”w /得分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;
}

2 个答案:

答案 0 :(得分:1)

您应该学习使用调试器并逐步执行代码,以便了解正在发生的事情。循环

while (contestant != "Done" && contestant != "done")
{
    for  (i = 1; i < 6; i++)
    {
        cout << "Enter judge " << i << " score: ";
        cin >> score;
        checkValid(score);
        scoreArray[arrayPos] = score;
        ++arrayPos;
    }
    // ....

是可疑的。首先,如果你没有输入“完成”或“完成”,你继续递增arrayPos,并且它的值大于4,所以你最终访问越界。其次,checkValid(score);应为score = checkValid(score);,否则score保持不变(它是函数checkValid()的本地)。如果您需要可调整大小的数组,请考虑使用std::vector。另请考虑使用std::map

答案 1 :(得分:1)

  

如何将输入名称与相应的分数相关联?

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

  

如何修复此错误“变量'scoreArray'周围的堆栈已损坏?”

此时你应该归零arrayPos变量

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

现在arrayPos变量很容易超出范围(即变为&gt; 4)并且您将写入堆栈(即已分配scoreArray的位置)尽管您不被允许