#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void getScore(int &, int &, int &, int &,int &);
void calcAverage(double );
int findLowest(int , int , int , int , int );
int main()
{
int num1, num2, num3, num4, num5;
string response;
getScore(num1, num2, num3, num4, num5);
calcAverage(num1, num2, num3, num4, num5);
cout << "Are there any more test scores?" << endl;
cin >> response;
cout << endl;
if (response == "yes")
{
getScore(num1, num2, num3, num4, num5);
calcAverage(num1, num2, num3, num4, num5);
}
system("pause");
return 0;
}
void getScore(int &num1, int &num2, int &num3, int &num4, int &num5)
{
cout << "What was your score for the first test?" << endl;
cin >> num1;
cout << endl;
if (num1 < 1 || num1 > 100)
{
cout<<"Scores must be between 1 and 100, re-enter the score" << endl;
cin >> num1;
}
cout << "What was your score for the second test?" << endl;
cin >> num2;
cout << endl;
if(num2 < 1 || num2 > 100)
{
cout<<"Scores must be between 1 and 100, re-enter the score" << endl;
cin >> num1;
}
cout << "What was your score for the third test?" << endl;
cin >> num3;
cout << endl;
if(num3 < 1 || num3 > 100)
{
cout<<"Scores must be between 1 and 100, re-enter the score" << endl;
cin >> num1;
}
cout << "What was your score for the fourth test?" << endl;
cin >> num4;
cout << endl;
if(num4 < 1 || num4 > 100)
{
cout<<"Scores must be between 1 and 100, re-enter the score" << endl;
cin >> num1;
}
cout << "What was your score for the fifth test?" << endl;
cin >> num5;
cout << endl;
if(num5 < 1 || num5 > 100)
{
cout<<"Scores must be between 1 and 100, re-enter the score" << endl;
cin >> num1;
}
}
int findLowest(int num1, int num2, int num3, int num4, int num5)
{
int lowest;
lowest = num1;
if (num2 < lowest)
{
lowest = num2;
}
else if (num3 < lowest)
{
lowest = num3;
}
else if (num4 < lowest)
{
lowest = num4;
}
else if (num5 < lowest)
{
lowest = num5;
}
cout << "the lowest test score is " << lowest << endl;
return lowest;
}
void calcAverage(int num1, int num2, int num3, int num4, int num5)
{
int findLowest(int, int, int, int, int);
int lowest;
double average;
findLowest(num1, num2, num3, num4, num5);
cout << lowest << endl;
average = (((float)num1 + num2 + num3 + num4 + num5) - lowest) / 4.0;
cout << showpoint << setprecision(8) << average << endl;
}
我的错误是: 错误2错误C2660:'calcAverage':函数不带5个参数
它显示在第18和26行,但我不确定它到底出了什么问题。愿有人帮我解释一下吗?
答案 0 :(得分:1)
你宣布:
void calcAverage(double );
而不是:
void calcAverage(int num1, int num2, int num3, int num4, int num5)
你对编译器说:&#34;我将创建一个名为calcAverage的函数,它将采用1个参数&#34;然后用5参数实现一个函数calcAverage,这样就会抛出一个错误。
答案 1 :(得分:1)
void calcAverage(double );
在这里,您声明函数calcAverage
采用double
类型的一个参数,但之后尝试调用它:
calcAverage(num1, num2, num3, num4, num5);
你在文件底部的函数定义是你想要的5个整数,问题是c ++编译器从上到下扫描文件。这意味着当它到达你试图用5个参数调用calcAverage
的部分时,它只看到文件顶部的函数声明只有一个参数,它甚至看不到它的定义。
要修复它,只需更改声明以获取与定义相同的参数:
void calcAverage(double );
为:
void calcAverage(int num1, int num2, int num3, int num4, int num5);
位于文件顶部。