简体   繁体   English

我的代码有问题,因为我不知道为什么会收到错误。 这是代码:

[英]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.

相关问题 我不明白为什么我的代码出现段错误 - I can't figure out why I'm getting a seg fault in my code “错误:没有用于cin的运算符&gt;&gt;”我无法弄清楚我在做什么错 - “Error: no operator for cin>>” I can't figure out what I'm doing wrong here 我的代码没有编译,任何人都可以帮我弄清楚我做错了什么? - My Code isn't compiling, can anyone help me figure out what I'm doing wrong? 我正在阅读开源代码(C++),但不知道为什么他们这样放置枚举 - I'm reading open source code (C++) and can't figure out why they put enum this way 我不知道为什么我生成的随机数未正确存储 - I can't figure out why my random numbers I'm generating are not storing correctly 我的许多函数都收到“未定义的引用”错误,我不知道为什么 - I'm getting “undefined reference” errors for a bunch of my functions and I can't figure out why 我有一个错误与我的for循环,不能找出为什么,我有一个“预期声明”错误 - I am having an error with my for loop and can't figure out why, I have an “expected declaration” error 我收到“字符串下标超出范围错误”。 我不知道为什么 - I'm getting a “string subscript out of range error”. I can not figure out why 这是一个二进制搜索代码,我无法找出问题所在 - it is a binary search code and i m not able to figure out the problem 我不知道为什么我会遇到这个无限循环 - I can't figure out why I'm getting this infinite loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM