[英]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;
}
我的錯誤顯示為:錯誤2錯誤C2660:'calcAverage':函數未采用5個參數
它在第18和26行中顯示,但我不確定它到底有什么問題。 有人可以幫我解釋一下嗎?
您聲明:
void calcAverage(double );
代替 :
void calcAverage(int num1, int num2, int num3, int num4, int num5)
您對編譯器說:“我要創建一個名稱為calcAverage的函數,該函數將帶有1個參數”,然后實現一個具有5個參數的函數calcAverage,因此他拋出錯誤。
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);
在文件的頂部。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.