意外错误,不确定原因

时间:2014-04-01 15:28:05

标签: c++ visual-c++ initializer

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

"错误1错误C2078:初始化程序太多,行:17列:1"

"错误2智能感知:预期')'行:17 Comlumn: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;

}

2 个答案:

答案 0 :(得分:2)

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相应地具有平衡5,4,3,2,1的情况。

甚至函数calcAverage都是错误的,因为它使用未初始化的变量最低。

应该有

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

然而由于函数findLowest的设计不好,关于最小数字的消息将被输出两次。

答案 1 :(得分:0)

另请注意,您将函数定义为 int findLowest(int,int,int,int,int); 与&#34; L&#34; 你打电话给它 int findlowest(num1,num2,num3,num4,num5); 用&#34; l&#34; 功能名称不一样:)。