2017-07-01 99 views
-5

我繼續得到這個錯誤,我已經多次查看了我的代碼,並且我看不到我的錯誤回合。該程序應該有5個用戶輸入一個評分,然後找到最高和最低的輸入,並將它們與平均值一起返回給用戶。錯誤功能太多參數

在此先感謝。

#include <iostream> // whatever this is 
#include <iomanip> 
using namespace std; 

int getValue(); 
int findLowest(); 
int findHighest(); 
double findAverage(); 

int main(){ // main function 

    cout << setw(15)<<""<< "Product Review"; // displays this at the top 

    int ratingOne = getValue(); 
    int ratingTwo = getValue(); 
    int ratingThree = getValue(); 
    int ratingFour = getValue(); 
    int ratingFive = getValue(); 
    int low = findLowest(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive); 
    int high = findHighest(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive); 
    double average = findAverage(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive); 

    cout << "\nThe highest rating received was: " << high; 
    cout << "\nThe lowest rating received was: " << low; 
    cout << std::fixed << std::setprecision(2) << "\nThe average rating received among 5 users was: " << average; 
} 

int getValue(){ 
    int rating; 

    do { 
     cout << "\nOn a scale of 1 to 10, how would you rate our product? "; 
     cin >> rating; 

     if (rating < 1 || rating > 10) {cout << "\nYou have entered an invalid number. Please try again.";} 
    } while (rating < 1 || rating > 10); 
    return rating; 
} 

int findLowest(){ 
    int low = 11; 
    if (ratingOne < low) { low = ratingOne } 
    if (ratingTwo < low) { low = ratingTwo } 
    if (ratingThree < low) { low = ratingThree } 
    if (ratingFour < low) { low = ratingFour } 
    if (ratingFive < low) { low = ratingFive } 
    return low; 
} 
int findHighest(){ 
    int high = 0; 
    if (ratingOne > high) { high = ratingOne } 
    if (ratingTwo > high) { high = ratingTwo } 
    if (ratingThree > high) { high = ratingThree } 
    if (ratingFour > high) { high = ratingFour } 
    if (ratingFive > high) { high = ratingFive } 
    return high; 
} 
double findAverage(){ 
    double average; 
    average = ratingOne + ratingTwo + ratingThree + ratingFour + ratingFive; 
    average = average/5; 
    return average; 
} 
+4

'findlowest()'不帶任何參數,你用5個參數來回憶它。與'findHighest'和'findAverage'相同。 – Barmar

+0

請編輯您的問題以提供[mcve]。 –

+0

使'ratingOne','ratingTwo','ratingThree','ratingFour'和'ratingOne'全局並且在'main'中調用沒有任何參數的三個函數findLowest,findHighest和findAverage。或者改變函數簽名來接受五個'int'。 –

回答

0

功能findLowest和findHighest沒有任何參數,但您所提供5.要正確地做到這一點,你應該寫:

#include <iostream> // whatever this is 
#include <iomanip> 
using namespace std; 

int getValue(); 
int findLowest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive); 
int findHighest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive); 
double findAverage(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive); 


int main(){ // main function 

cout << setw(15)<<""<< "Product Review"; // displays this at the top  

int ratingOne = getValue(); 
int ratingTwo = getValue(); 
int ratingThree = getValue(); 
int ratingFour = getValue(); 
int ratingFive = getValue(); 
int low = findLowest(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive); 
int high = findHighest(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive); 
double average = findAverage(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive); 

cout << "\nThe highest rating received was: " << high; 
cout << "\nThe lowest rating received was: " << low; 
cout << std::fixed << std::setprecision(2) << "\nThe average rating received among 5 users was: " << average; 
} 

int getValue(){ 
int rating; 

do { 
cout << "\nOn a scale of 1 to 10, how would you rate our product? "; 
cin >> rating; 

if (rating < 1 || rating > 10) {cout << "\nYou have entered an invalid number. Please try again.";} 
} 

while (rating < 1 || rating > 10); 
return rating; 
} 

int findLowest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive){ 
int low = 11; 
if (ratingOne < low) { low = ratingOne; } 
if (ratingTwo < low) { low = ratingTwo; } 
if (ratingThree < low) { low = ratingThree; } 
if (ratingFour < low) { low = ratingFour; } 
if (ratingFive < low) { low = ratingFive; } 
return low; 
} 
int findHighest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive){ 
int high = 0; 
if (ratingOne > high) { high = ratingOne; } 
if (ratingTwo > high) { high = ratingTwo; } 
if (ratingThree > high) { high = ratingThree; } 
if (ratingFour > high) { high = ratingFour; } 
if (ratingFive > high) { high = ratingFive; } 
return high; 
} 
double findAverage(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive){ 
double average; 
average = ratingOne + ratingTwo + ratingThree + ratingFour + ratingFive; 
average = average/5; 
return average; 
} 
+0

太棒了,我只是在每個單獨的聲明前缺少「int」。我是否還需要在函數中聲明所有內容?我現在剛剛收到一個錯誤,指出「ratingOne」等未在此範圍內聲明 – jm21609

+0

我聲明爲「int ratingOne;」在每個功能等等,並且這個錯誤消失了。現在剩下的最後一個問題是:「所有這三個函數的'findlowest(int,int,int,int,int)'的undefined引用 – jm21609

+0

對不起......應該在我發佈之前真正編譯。不會再發生:) –

0

的數量和參數類型的函數原型

int findLowest(); 
    int findHighest(); 
    double findAverage(); 

應該符合您的定義。他們沒有。看起來你想要將類型爲int的參數(5)傳遞給上面的函數,因此在原型中。你需要說明一點。

 int findLowest(int, int, int, int, int); 
     int findHighest(int, int, int, int, int); 
     double findAverage(int, int, int, int, int); 

和你的函數定義。

  int findLowest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive){...} 
      int findHighest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive){...} 
      double findAverage(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive){..} 

以及調用函數時的參數和類型的數量也應該與您的函數定義匹配。

+0

我將原型改爲 int findLowest(int ratingOne,int ratingTwo,int ratingThree,int ratingFour,int ratingFive); – jm21609

+0

該程序只是現在給我一個錯誤,說明「findlowest(int,int,int,int,int)'undefined引用' – jm21609

+0

@ jm21其確定。在原型中聲明參數的標識符是爲了鼓勵可讀性。但是在小程序中,參數的類型就足夠了。 –

0

好吧,所以我想通了,但我不知道爲什麼。這是我的最終代碼。

#include <iostream> // whatever this is 
#include <iomanip> 
using namespace std; 

int getValue(); 
int findLowest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive); 
int findHighest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive); 
double findAverage(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive); 


int main(){ // main function 

cout << setw(15)<<""<< "Product Review"; // displays this at the top 

int ratingOne = getValue(); 
int ratingTwo = getValue(); 
int ratingThree = getValue(); 
int ratingFour = getValue(); 
int ratingFive = getValue(); 
int low = findLowest(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive); 
int high = findHighest(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive); 
double average = findAverage(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive); 

cout << "\nThe highest rating received was: " << high; 
cout << "\nThe lowest rating received was: " << low; 
cout << std::fixed << std::setprecision(2) << "\nThe average rating received among 5 users was: " << average; 
} 

int getValue(){ 
int rating; 

do { 
cout << "\nOn a scale of 1 to 10, how would you rate our product? "; 
cin >> rating; 

    if (rating < 1 || rating > 10) {cout << "\nYou have entered an invalid number. Please try again.";} 
} 

while (rating < 1 || rating > 10); 
return rating; 
} 

int findLowest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive){ 
int low = 11; 
if (ratingOne < low) { low = ratingOne; } 
if (ratingTwo < low) { low = ratingTwo; } 
if (ratingThree < low) { low = ratingThree; } 
if (ratingFour < low) { low = ratingFour; } 
if (ratingFive < low) { low = ratingFive;} 
return low; 
} 
int findHighest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive){ 
int high = 0; 
if (ratingOne > high) { high = ratingOne; } 
if (ratingTwo > high) { high = ratingTwo; } 
if (ratingThree > high) { high = ratingThree; } 
if (ratingFour > high) { high = ratingFour; } 
if (ratingFive > high) { high = ratingFive; } 
return high; 
} 
double findAverage(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive){ 
double average; 
average = ratingOne + ratingTwo + ratingThree + ratingFour + ratingFive; 
average = average/5; 
return average; 
} 

我以前曾將此作爲我的代碼,它給了我上面討論的錯誤。我也有程序來編譯,只是在()中說「int」5次,但是這些值在天文學上並不重要。我會輸入5個數字1-10,結果類似24908140489,-301213901398,489723498234或其他。不知道爲什麼發生這種情況,但也許這是存儲號碼的默認值,並且出現了錯誤。應該保存該編輯以回顧它,因爲現在我不知道是什麼導致它發生。至少它現在有效,謝謝你的幫助!