如何修复此程序的编译错误

时间:2015-12-11 01:42:40

标签: c++

我收到此错误,说明在此范围内未声明某些内容。我尝试通过在int main上面声明它来声明它,但是它给了我另一个错误,说我放入的东西也需要声明。这是代码:

#include <iostream>

using namespace std;

int matrix[5][5] = {0};

string DayOfWeek(int i) {
    if(i == 0)
        return "Monday";
    if(i == 1)
        return "Tuesday";
    if(i == 2)
        return "Wednesday";
    if(i == 3)
        return "Thursday";
    if(i == 4)
        return "Friday";
}

string Child(int i) {
    if(i == 0)
        return "Alex";
    if(i == 1)
        return "Brian";
    if(i == 2)
        return "Carla";
    if(i == 3)
        return "David";
    if(i == 4)
        return "Ellen";
}

bool CheckInput(int i) {
    if(i >= 0)
        return true;
    else
        return false;
}

void ReadInput(int child, int day) {
    cout<<"Enter food eaten by "<<Child(child)<< " on " <<DayOfWeek(day);
    int a;
    cin>>a;
    if (CheckInput(a))
        matrix[child][day] = a;
    else {
        cout<<"Invalid (negative) food quantity. Please re-enter a correct value\n";     
        ReadInput(child, day);
    }
}

float AverageDay(int day) {
    float average = 0;
    for (int i=0; i<5; i++)
        average += matrix[i][day];
    return average/5;
}

void LeastAmount(int &child, int &day) {
    child = 0; day = 0;
    for(int i=0; i<5; i++)
        for(int j=0; j<5; j++)
            if(matrix[child][day] > matrix[i][j]) {
                child = i; day = j;
            }
}

void MaxAmount(int &child, int &day) {
    child = 0; day = 0;
    for(int i=0; i<5; i++)
        for(int j=0; j<5; j++)
            if(matrix[child][day] < matrix[i][j]) {
                child = i; day = j;
            }
}

int main() {
    for(int i=0; i<5; i++)
        for(int j=0; j<5; j++)
            ReadInput(i, j);

    bool ok = true;
    while (ok) {
        cout<<"Average amount of food eaten per day by all children - press 1\n";
        cout<<"The least amount of food eaten during the week by any one child - press 2\n";      
        cout<<"The greatest amount of food eaten during the week by any one child - press 3\n";
        cout<<"To exit - press 0\n";

        int answer;
        cin >> answer;
        if (answer == 0)
            ok = false;
        if (answer == 1) {
            cout<<"Enter a day of the week [from 1 to 5]\n";
            int day;
            cin>>day;
            cout<<"Average food consumed on "<<DayOfWeek(day)<<": "<<Average(day)<<endl;
        }

        if(answer == 2) {
            int child, day;
            LeastAmount(child, day);
            cout<<"The least daily food consumed was by "<< Child(child)<<" on "<<DayOfWeek(day)<<endl;
        }

        if(answer == 3) {
            int child, day;
            MaxAmount(child, day);
            cout<<"The most daily food consumed was by "<< Child(child)<<" on "<<DayOfWeek(day)<<endl;
        }
    }

    return 0;
}

我在编译器中遇到的错误是

 In function 'int main()':
 108:81: error: 'Average' was not declared in this scope
 In function 'std::string DayOfWeek(int)':
 19:1: warning: control reaches end of non-void function [-Wreturn-type]
 In function 'std::string Child(int)':
 33:1: warning: control reaches end of non-void function [-Wreturn-type]

2 个答案:

答案 0 :(得分:1)

这可能是因为你将你的函数声明为AverageDay,但是当你调用它时,你调用了Average(day)。只需将其更改为AverageDay(day)即可。至于其他错误,您需要添加#include <string>

答案 1 :(得分:0)

函数名称错误(这应该编译):

#include <iostream>

using namespace std;

int matrix[5][5] = {0};

string DayOfWeek(int i) {
    if(i == 0)
        return "Monday";
    if(i == 1)
        return "Tuesday";
    if(i == 2)
        return "Wednesday";
    if(i == 3)
        return "Thursday";
    if(i == 4)
        return "Friday";
}

string Child(int i) {
    if(i == 0)
        return "Alex";
    if(i == 1)
        return "Brian";
    if(i == 2)
        return "Carla";
    if(i == 3)
        return "David";
    if(i == 4)
        return "Ellen";
}

bool CheckInput(int i) {
    if(i >= 0)
        return true;
    else
        return false;
}

void ReadInput(int child, int day) {
    cout<<"Enter food eaten by "<<Child(child)<< " on " <<DayOfWeek(day);
    int a;
    cin>>a;
    if (CheckInput(a))
        matrix[child][day] = a;
    else {
        cout<<"Invalid (negative) food quantity. Please re-enter a correct value\n";     
        ReadInput(child, day);
    }
}

float AverageDay(int day) {
    float average = 0;
    for (int i=0; i<5; i++)
        average += matrix[i][day];
    return average/5;
}

void LeastAmount(int &child, int &day) {
    child = 0; day = 0;
    for(int i=0; i<5; i++)
        for(int j=0; j<5; j++)
            if(matrix[child][day] > matrix[i][j]) {
                child = i; day = j;
            }
}

void MaxAmount(int &child, int &day) {
    child = 0; day = 0;
    for(int i=0; i<5; i++)
        for(int j=0; j<5; j++)
            if(matrix[child][day] < matrix[i][j]) {
                child = i; day = j;
            }
}

int main() {
    for(int i=0; i<5; i++)
        for(int j=0; j<5; j++)
            ReadInput(i, j);

    bool ok = true;
    while (ok) {
        cout<<"Average amount of food eaten per day by all children - press 1\n";
        cout<<"The least amount of food eaten during the week by any one child - press 2\n";      
        cout<<"The greatest amount of food eaten during the week by any one child - press 3\n";
        cout<<"To exit - press 0\n";

        int answer;
        cin >> answer;
        if (answer == 0)
            ok = false;
        if (answer == 1) {
            cout<<"Enter a day of the week [from 1 to 5]\n";
            int day;
            cin>>day;
            cout<<"Average food consumed on "<<DayOfWeek(day)<<": "<<AverageDay(day)<<endl;
        }

        if(answer == 2) {
            int child, day;
            LeastAmount(child, day);
            cout<<"The least daily food consumed was by "<< Child(child)<<" on "<<DayOfWeek(day)<<endl;
        }

        if(answer == 3) {
            int child, day;
            MaxAmount(child, day);
            cout<<"The most daily food consumed was by "<< Child(child)<<" on "<<DayOfWeek(day)<<endl;
        }
    }

    return 0;
}