意外错误,不知道为什么

Unexpected errors, not sure why

本文关键字:为什么 不知道 错误 意外      更新时间:2023-10-16

我似乎无法让我的代码正常运行。我收到错误消息

"错误 1

错误 C2078:初始值设定项过多,行:17 列:1"

"错误 2 智能感知:预期为')"行:17 通信:21"

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void getScore(int &, int &, int &, int &,int &);
void calcAverage(int, int, int, int, int);
int findLowest(int, int, int, int, int);
int main()
{
    int num1, num2, num3, num4, num5;
    string response; 

    getScore(num1, num2, num3, num4, num5);
    int findlowest(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;
}

Cnange 此语句

   int findlowest(num1, num2, num3, num4, num5);

   int lowest = findLowest(num1, num2, num3, num4, num5);

或干脆作为

   findLowest(num1, num2, num3, num4, num5);

因为您不使用返回值。

函数的名称也是 findLowest .

还有这部分代码

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);
}

没有任何意义。

函数 findLowest 也是错误的。它找不到最低的。例如,当 num1, num2, num3, num4, num5 相应地具有 balues 5, 4, 3, 2, 1 时的情况。

甚至函数 calcAverage 也是错误的,因为它使用了未初始化的变量 lowest。

应该有

int lowest = findLowest(num1, num2, num3, num4, num5);

但是,由于函数 findLowest 的设计不佳,有关最低数字的消息将被输出两次。

另请注意,您将函数定义为int findLowest(int, int, int, int, int);带"L"你用int findlowest(num1, num2, num3, num4, num5);带"L":)的函数名称不同。