c++ 控制台未打印预期输出

jm81lzqq  于 2022-11-27  发布在  其他
关注(0)|答案(1)|浏览(163)

我试图通过实现二维数组来扩展以前的代码,但是我一直遇到控制台不输出正确值的问题。控制台在计算平均值时没有接受正确的值,输出0而不是预期值。运行代码时,显示高和低分数的部分总是显示输入的第一个数字。
工作有限制。
1.调整逻辑,去掉高分和低分,取每位学生剩余三分的平均值。学生成绩基于中间三分的平均值。
1.所有数据都从键盘读入。
1.可以使用两个全局常数:一个用于学生数量,一个用于测试数量。
1.以表格形式显示学生姓名、5次考试成绩、平均分和等级。在表格中包括一个标题,分别标记每一列。
1.使用iomanip和setw()格式化输出。

  1. Main应该由变量声明和函数调用组成。这意味着处理数组的for循环驻留在函数中,而不是main中。
    1.必须遵循基本代码。
    `
using namespace std;

const int SCORES = 5;
const int NUM_STUDENTS = 3;

int main()
{
    string name[NUM_STUDENTS];
    int test[NUM_STUDENTS][SCORES];
    char grade[NUM_STUDENTS];
    float avg{};
    int total = 0;
    int hiIndex{}, loIndex{};

    calcData(name, test, grade, total, hiIndex, loIndex, avg);

    //display results
    displayResults(name, test, grade, avg, loIndex, hiIndex);

    system("pause");
    return 0;
}

void calcData(string name[], int test[][SCORES], char grade[], int total, int hiIndex, int loIndex, float& avg)
{
    for (int counter = 0; counter < NUM_STUDENTS; counter++)
    {

        getInput(name, test, counter, total);
        cin.ignore();

        //find index of the highest score and lowest score
        findHiAndLow(test, hiIndex, loIndex, counter);

        //assign letter grade
        assignGrade(avg, grade, counter);

        //calculate the class average
        calcAvg(total - (test[counter][hiIndex] + test[counter][loIndex]), avg, SCORES - 2);

    }

}

void getInput(string arrOne[], int arrTwo[][SCORES], int size, int& t)
{
    //get student name
    cout << "Input the student name and press enter\n";
    getline(cin, arrOne[size]);

    for (int i = 0; i < SCORES; i++)
    {
        //get student test score 
        cout << "Input the score for the midterm test\n";
        cin >> arrTwo[size][i];

        //(accumulate scores) total of all scores
        t += arrTwo[size][i];
    }

    cout << endl;
}

int findHiAndLow(int t[][SCORES], int& h, int& l, int row)
{
    for (int i = 0; i < SCORES; i++)
    {
        if (t[row][h] < t[row][i])
            h = row;
        if (t[row][l] > t[row][i])
            l = row;

    }
    return h, l;
}

float calcAvg(int t, float a, int size)
{
    a = static_cast<float>(t) / size;

    return a;
}

void displayResults(string n[], int t[][SCORES], char g[], float a, int low, int high)
{
    for (int counter = 0; counter < NUM_STUDENTS; counter++)
    {
        cout << left << setw(10) << n[counter] << ":";
        for (int i = 0; i < SCORES; i++)
        {
            cout << setw(10) << t[counter][i];
        }
        cout << endl;
    }

    cout << "\n\nThe class average for this test = " << a << endl << endl;
    for (int i = 0; i < NUM_STUDENTS; i++)
    {
        cout << n[i] << " your highest test score = " << t[i][high] << endl;
        cout << n[i] << " your lowest test score = " << t[i][low] << endl << endl;
    }

}

`
预期的结果是程序从给出的初始5个分数中去掉最高和最低分数后,取剩下的3个中间分数的平均值。()并获取输入()。我已经尝试为getInput同时使用两个for循环(),并切换回在外部(在calcData()内)有一个函数,以包含其他函数,目的是让它为每个学生循环。
我希望控制台打印出三个中间分数的平均值,而不包括高分和低分,我也希望控制台打印出学生的高分和低分,但它只打印第一个分数。
如果我的数字是,例如,12,89,45,100,23;我们的期望是,它会去掉12和100,留给我89、45和23。然后取这3个数字的平均值,理论上应该得到52.34,结果是“F”,但是它打印出0。并且因为第一次键入的数字是12,所以最小和最大数字将被列为12。应该分别是12和100。

apeeds0o

apeeds0o1#

这是另一个令人难以置信的常见的新手对从函数返回值的困惑。
这是你的职责

float calcAvg(int t, float a, int size)
{
    a = static_cast<float>(t) / size;

    return a;
}

您试图计算平均值并返回结果,但是由于某种原因,您将a声明为参数,而不是局部变量。

float calcAvg(int t, int size)
{
    float a = static_cast<float>(t) / size;

    return a;
}

一旦您看到,您应该会发现它可以进一步简化,完全消除a

float calcAvg(int t, int size)
{
    return static_cast<float>(t) / size;
}

现在看看如何调用calcAvg

calcAvg(total - (test[counter][hiIndex] + test[counter][loIndex]), 
    avg, SCORES - 2);

你正在调用函数,但没有对返回值做任何处理。

avg = calcAvg(total - (test[counter][hiIndex] + test[counter][loIndex]), 
    SCORES - 2);

现在calcAvg的返回值被赋值给变量avg,改变它的值。这显然是你想要的。如果你想用函数返回值改变变量的值,语法是x = func();而不是func(x)
真的不知道为什么这对新手来说是一个绊脚石。正确的代码对我来说总是很自然和简单。但是,在任何情况下,请记住 * 参数 * 和 * 返回值 * 是不同的东西,具有不同的语法和不同的用途。

相关问题