[英]I'm having issue with my code, in that I can't figure out why I'm receiving an error. Here is the code:
#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;
}
My error reads: Error 2 error C2660: 'calcAverage' : function does not take 5 arguments 我的错误显示为:错误2错误C2660:'calcAverage':函数未采用5个参数
It shows in lines 18 and 26, but I am not sure what exactly is wrong with it. 它在第18和26行中显示,但我不确定它到底有什么问题。 May someone help explain this to me?
有人可以帮我解释一下吗?
You declare this : 您声明:
void calcAverage(double );
instead of : 代替 :
void calcAverage(int num1, int num2, int num3, int num4, int num5)
You say to the compilator : "I am going to make a function with name calcAverage that will take 1 argument" and then implement a function calcAverage with 5 argument, so he throw an error. 您对编译器说:“我要创建一个名称为calcAverage的函数,该函数将带有1个参数”,然后实现一个具有5个参数的函数calcAverage,因此他抛出错误。
void calcAverage(double );
Here you declare your function calcAverage
to take one argument of type double
but then you try calling it like: 在这里,您声明函数
calcAverage
接受一个类型为double
参数,然后尝试像这样调用它:
calcAverage(num1, num2, num3, num4, num5);
You're function definition at the bottom of the file is taking 5 integers like you wanted, the problem is that the c++ compiler scans the file from top to bottom. 您在文件底部的函数定义像您想要的那样采用5个整数,问题是c ++编译器从上到下扫描文件。 That means when it gets to the part where you're trying to call
calcAverage
with 5 arguments it only saw the function declaration at the top of the file taking a single argument, it didn't even see the definition of it yet. 这意味着,当您到达尝试使用5个参数调用
calcAverage
的部分时,它仅在文件顶部看到带有单个参数的函数声明,甚至看不到它的定义。
To fix it simply change the declaration to take the same arguments like your definition: 要解决此问题,只需更改声明以使用与定义相同的参数即可:
void calcAverage(double );
to: 至:
void calcAverage(int num1, int num2, int num3, int num4, int num5);
at the top of the file. 在文件的顶部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.