2015-12-11 33 views
-2

我收到此錯誤,指出在此範圍內未聲明某些內容。我試着通過放入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] 
+3

所以你嘗試過在你的代碼查找「平均」? –

+4

調試的第一條規則:編譯器是正確的。 –

回答

1

那是因爲你聲明瞭功能AverageDay可能是,但是當你調用它,你叫Average(day)。只需將其更改爲AverageDay(day)即可。至於其他錯誤,您需要添加#include <string>

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