c++ - 在球员评分程序中显示低于平均分数

标签 c++ arrays if-statement average

我正在尝试创建一个程序,用户可以在其中输入最多 100 个球员的姓名和分数,然后让它打印出所有球员的姓名和分数,然后是平均分数,最后,显示得分低于平均水平的玩家。除了最后一 block ,我已经设法完成了所有这些,显示低于平均分数。我有点不确定如何去做。在我的 DesplayBelowAverage 函数中,我试图让它读取当前玩家的分数并将其与平均值进行比较,看看是否应该将其打印为低于平均分数,但它似乎无法识别我创建的 averageScore 值在 CalculateAverageScores 函数中。这是我的代码:

#include <iostream>
#include <string>

using namespace std;

int InputData(string [], int [], int);
int CalculateAverageScores(int [], int);
void DisplayPlayerData(string [], int [], int);
void DisplayBelowAverage(string [], int [], int);


void main()
{
    string playerNames[100];
    int scores[100];


    int sizeOfArray = sizeof(scores);
    int sizeOfEachElement = sizeof(scores[0]);
    int numberOfElements = sizeOfArray / sizeOfEachElement;

    cout << numberOfElements << endl;

    int numberEntered = InputData(playerNames, scores, numberOfElements);

    DisplayPlayerData(playerNames, scores, numberEntered);

    CalculateAverageScores(scores, numberEntered);


    cin.ignore();
    cin.get();
}

int InputData(string playerNames[], int scores[], int size)
{
    int index;  

    for (index = 0; index < size; index++)
    {
        cout << "Enter Player Name (Q to quit): ";
        getline(cin, playerNames[index]);
        if (playerNames[index] == "Q")
        {
            break;
        }

        cout << "Enter score for " << playerNames[index] << ": ";
        cin >> scores[index];
        cin.ignore();
    }

    return index;
}


void DisplayPlayerData(string playerNames[], int scores[], int size)
{
    int index;

    cout << "Name     Score" << endl;

    for (index = 0; index < size; index++)
    {       
        cout << playerNames[index] << "     " << scores[index] << endl;     
    }
}

int CalculateAverageScores(int scores[], int size)
{
    int index;
    int totalScore = 0;
    int averageScore = 0;

    for (index = 0; index < size; index++)
    {       
        totalScore = (totalScore + scores[index]);              
    }
    averageScore = totalScore / size;
    cout << "Average Score: " << averageScore;

    return index;
}

void DisplayBelowAverage(string playerNames[], int scores[], int size)
{
    int index;

    cout << "Players who scored below average" << endl;
    cout << "Name     Score" << endl;

    for (index = 0; index < size; index++)
    {       
        if(scores[index] < averageScore)
        {
            cout << playerNames[index] << "     " << scores[index] << endl;
        }
    }
}

最佳答案

您正在计算 CalculateAverageScore 中的 averageScore 变量,它只是该函数的本地变量,因此 DisplayBelowAverage 不知道 >averageScore 值。这就是为什么您的逻辑不起作用。

为了解决这个问题,有两种选择:

  1. averageScore 声明为全局变量(尽管不建议使用全局变量)

  2. averageScore 作为参数传递给 DisplayBelowAverage。这是一个更好的方法。因此,您应该做的是返回您在 CalculateAverageScore 中计算的平均分数,并将其存储在某个变量中,然后将其作为参数传递给 DisplayBelowAverage 函数。

希望对你有帮助

关于c++ - 在球员评分程序中显示低于平均分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43176816/

相关文章:

java - 循环不会多次迭代 if 语句

javascript - ELSE 语句中的函数也会与 IF 语句一起执行,如何防止这种情况发生?

c++ - fmodl - long double 中的模数

c++ - Visual Studio 2012 缺点/优点的单元测试框架

arrays - 避免在输入中使用空格

java - 字符串加密有效,byte[]数组类型加密无效

java - 为什么 print 方法从数组索引中打印空数据?

检查 char 变量的内容 - C 编程

c++ - 错误 C3867 : function call missing argument list

c++ - 为什么qt不读取文件