无法使此功能显示

时间:2017-11-13 02:38:40

标签: c++ visual-studio function visual-c++

对于初学者我是新编码所以我相信这些代码看起来很讨厌,可能很难理解。该程序应该让用户输入5个班级,每个班级5个考试成绩,然后使用功能,我需要找到最低的考试成绩,最高的考试成绩,以及每个班级的平均分数,所有这些我已经完成了相应的功能。然后使用另一个功能显示所有,有我的问题。我认为这是一个简单的修复,但我不确定,我需要做的就是从main调用display函数,然后让它显示,但它每次都只给我语法错误。就像我说的,我认为这将是一个简单的解决方案,但我不确定。任何帮助将不胜感激!

#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int minfun(int lowest, int test[5][5])
{
    for (int a = 0; a < 5; a++)
    {
        for (int i = 0; i < 5; i++)
        {
            if (test[a][i] < lowest)
            {
                lowest = test[a][i];
            }
        }
    }
    return lowest;
}

int maxfun(int highest, int test[5][5])
{
    for (int a = 0; a < 5; a++)
    {
        for (int i = 0; i < 5; i++)
        {
            if (test[a][i] > highest)
            {
                highest = test[a][i];
            }
        }
    }
    return highest;
}

double classavg(double avg, int sum, int test[])
{
    for (int a = 0; a < 5; a++)
    {
        for (int i = 0; i < 5; i++)
        {
            sum = sum + test[a];
        }
    }
    avg = sum / 5;
    return avg;
}

void display(string classes[], int test[], int lowest, int highest, double avg)
{
    minfun(lowest, test[5]);
    maxfun(highest, test[5]);
    classavg(avg, sum, test[5]);
    for (int a = 0; a < 5; a++)
        {
        for (int i = 0; i < 5; i++)
            {
                cout << "=============================================================" << endl;
                cout << "Displaying stats for " << classes[a] << endl;
                cout << "Lowest test score for " << classes[a] << " is: " << lowest << endl;
                cout << "Highest test score for " << classes[a] << " is: " << highest << endl;
                cout << "The average for " << classes[a] << "is: " << avg << endl;
                cout << "=============================================================" << endl;
            }
        }
    //needs for loop to display stats for each class
}

int main()
{
    const int max_test = 5;
    int lowest = 100;
    int highest = 0;
    int sum = 0;
    double avg = 0;
    string classes[5];
    int test[5][5];

    cout << "Enter the name of your first class: ";
    getline (cin,classes[0]);
    cout << "Enter the name of your second class: ";
    getline (cin, classes[1]);
    cout << "Enter the name of your third class: ";
    getline(cin, classes[2]);
    cout << "Enter the name of your fourth class: ";
    getline(cin, classes[3]);
    cout << "Enter the name of your fifth class: ";
    getline(cin, classes[4]);
    cout << "=============================================================" << endl;
    for (int a = 0; a <= max_test; a++)
    {
        for (int i = 1; i <= max_test; i++)
        {
            cout << "Enter score for test " << i << " in " << classes[a] << " : ";
            cin >> test[a][i];
        }
        cout << "=============================================================" << endl;
    }
    cout << "=============================================================" << endl;
    display(lowest, highest, avg);
    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:0)

你有问题将参数传递给函数,我建议你看看.. 顺便说一句,我已经使你的变量变得静态并修复了一些错误......它似乎有效 奖励..

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
static string classes[5];
static int test[5][5];
static const int max_test = 5;
static int lowest = 100;
static int highest = 0;
static int sum = 0;
static double avg = 0;

int minfun(int lowest)
{
    for (int a = 0; a < 5; a++)
    {
        for (int i = 0; i < 5; i++)
        {
            if (test[a][i] < lowest)
            {
                lowest = test[a][i];
            }
        }
    }
    return lowest;
}

int maxfun(int highest)
{
    for (int a = 0; a < 5; a++)
    {
        for (int i = 0; i < 5; i++)
        {
            if (test[a][i] > highest)
            {
                highest = test[a][i];
            }
        }
    }
    return highest;
}

double classavg(double avg)
{
    for (int a = 0; a < 5; a++)
    {
        for (int i = 0; i < 5; i++)
        {
            sum = sum + test[a][i];
        }
    }
    avg = sum / 5;
    return avg;
}

void display(int lowest, int highest, double avg)
{
    minfun(lowest);
    maxfun(highest);
    classavg(avg);
    for (int a = 0; a < 5; a++)
        {
        for (int i = 0; i < 5; i++)
            {
                cout << "=============================================================" << endl;
                cout << "Displaying stats for " << classes[a] << endl;
                cout << "Lowest test score for " << classes[a] << " is: " << lowest << endl;
                cout << "Highest test score for " << classes[a] << " is: " << highest << endl;
                cout << "The average for " << classes[a] << "is: " << avg << endl;
                cout << "=============================================================" << endl;
            }
        }
    //needs for loop to display stats for each class
}

int main()
{


    cout << "Enter the name of your first class: ";
    getline (cin,classes[0]);
    cout << "Enter the name of your second class: ";
    getline (cin, classes[1]);
    cout << "Enter the name of your third class: ";
    getline(cin, classes[2]);
    cout << "Enter the name of your fourth class: ";
    getline(cin, classes[3]);
    cout << "Enter the name of your fifth class: ";
    getline(cin, classes[4]);
    cout << "=============================================================" << endl;
    for (int a = 0; a <= max_test; a++)
    {
        for (int i = 1; i <= max_test; i++)
        {
            cout << "Enter score for test " << i << " in " << classes[a] << " : ";
            cin >> test[a][i];
        }
        cout << "=============================================================" << endl;
    }
    cout << "=============================================================" << endl;
    display(lowest, highest, avg);
    system("pause");
    return 0;
}