简体   繁体   中英

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

It shows in lines 18 and 26, but I am not sure what exactly is wrong with it. 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.

void calcAverage(double );

Here you declare your function calcAverage to take one argument of type double but then you try calling it like:

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM