【发布时间】:2017-11-13 02:38:39
【问题描述】:
对于初学者来说,我是编码新手,所以我确信其中一些代码看起来很讨厌并且可能难以理解。程序应该让用户输入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;
}
【问题讨论】:
-
你的意思是这个错误吗?
error: invalid conversion from ‘int’ to ‘int (*)[5]’ [-fpermissive] minfun(lowest, test[5]); -
“这是我的问题”,它在哪里?如果您在屏幕上指出问题所在,这并不能真正帮助其他可能想要阅读这么大段代码的人。
-
这是我的问题只有在你实际解释了一个你没有解释过的问题时才有用,而且它每次都会给我语法错误 i> 绝对没用,除非你告诉我们那个语法错误是什么 - 它就在你面前的屏幕上,所以你绝对没有理由不能在你的帖子中提供给我们好。显然你已经看到了,因为你知道这是一个语法错误。它是屏幕上的文本,就像您的代码一样。
-
调用
display函数的一个问题是该函数需要5个参数,而你只给它3个。 -
我遇到的问题来源于显示功能,也来源于显示(lowest,highest,avg);我在 main 的底部有一行。 Arash 是正确的,我在 display 函数中得到这两行的错误: minfun(lowest, test[5]);maxfun(highest, test[5]);
标签: c++ visual-studio function visual-c++