2014-07-01 53 views
0

當前我試圖運行一個程序,您可以選擇一個整數或小數,並根據您的號碼被操縱。我的教授告訴我使用函數重載來實現這個功能。但每次我嘗試編譯它時,都會給出101個警告和2個錯誤,說:'pick' must return a value。我沒有正確地宣佈它嗎?我遵循我的教科書中的模板和一些我在網上找到的模板,但無法使其工作。必須返回值,函數重載

#include <iostream> 
#include <iomanip> 
#include <cmath> 

using namespace std; 

int main() 
{ 
    //Declarations 
    int userInt; 
    double userFloat; 
    char selection, ans; 
    static double counterInt, counterFloat; 

    //Prototypes 
    int pick(int); 
    double pick(double); 

    //Defaults 
    counterInt = 0; 
    counterFloat = 0; 
    ans = 'Y'; 

    //Game 
    do 
    { 
    cout << "Please choose an integer or a decimal number by typing in i or d" << endl; 
    cin >> selection; 

    if(selection == 'i') 
    { 
     cout << "Please enter an integer" << endl; 
     cin >> userInt; 
     cout << pick(userInt); 
     counterInt++;} 
    else 
    { 
     cout << "Please enter a decimal" << endl; 
     cin >> userFloat; 
     cout << pick(userFloat); 
     counterFloat++;} 

    //Play Again? 
    cout << "Would you like to play again? Please enter Y or N" << endl; 
    cin >> ans; 
    } 
    while ((ans == 'Y') || (ans == 'y')); 

    //End Game 
    if ((ans != 'Y') && (ans != 'y')) 
    { 
    cout << "Thank you for playing! You picked integers: " << counterInt << " times and you picked decimals: " << counterFloat << " times." << endl; 
    } 

    return 0; 
} 

//Function Overload 
int pick(int number) 
{ 
    if(number % 7 == 0) 
    { 
    cout << setw(7) << "7777777" << setw(7) << number << endl; 
    } 
    else 
    { 
    cout << setw(7) << "*******" << setw(7) << number << endl; 
    } 
}  

double pick(double number) 
{ 
    cout << setw(6) << "The square root of the number " << number << " is: " << setprecision(3) << pow(number,.5) << endl; 
} 

回答

1

您的兩個pick函數聲明爲返回各自或intdouble,但他們都沒有有一個return語句。

//Function Overload 
int pick(int number) 
{ 
    if(number % 7 == 0) 
    { 
     cout << setw(7) << "7777777" << setw(7) << number << endl; 
    } 
    else 
    { 
     cout << setw(7) << "*******" << setw(7) << number << endl; 
    } 
    return number; // <<<<<<<<<<<< 
}  


double pick(double number) 
{ 
    double sqrt = pow(number,.5); 
    cout << setw(6) << "The square root of the number " << number << " is: " << setprecision(3) << sqrt << endl; 
    return sqrt; // <<<<<<<<<<<< 
} 

或者,如果他們實際上並不需要任何回報,只是改變返回類型void

void pick (int number) { ... } 
void pick (double number) { ... } 
+0

修好了!謝謝!令人驚訝的是,如何讓一個小東西導致該程序找到101個警告給你關於整個代碼片段:( – user3763403

+1

是的,'C++'可以有點挑剔,但它非常強大,但我看到一些其他樣式問題在你的代碼中,我並不認爲所有的警告都只是爲了失去回報,但這不是處理恐懼的地方,我建議[寫一本好書](http: //stackoverflow.com/q/388242/1171191)。 – BoBTFish

1

你有一個函數調用取你告訴別人你正在返回編譯器該函數完成其算法時的整數。在你的函數中你永遠不會返回任何東西,你只需寫一個保存在var數字中的特定值。您必須將void函數(意味着沒有返回值)或返回任何值,即成功爲'1',失敗爲'-1'。

//Function Overload 
int pick(int number) 
{ 
    if(number % 7 == 0) 
    { 
     cout << setw(7) << "7777777" << setw(7) << number << endl; 
     return(1) 
    } 
    else 
    { 
     cout << setw(7) << "*******" << setw(7) << number << endl; 
     return(-1) 
    } 
} 

你也可以使用無效的,如果你不想返回任何值或變量

//Function Overload 
void pick(int number) 
{ 
    if(number % 7 == 0) 
    { 
     cout << setw(7) << "7777777" << setw(7) << number << endl; 
    } 
    else 
    { 
     cout << setw(7) << "*******" << setw(7) << number << endl; 
    } 
} 

所以每次你的函數之前寫一個類型(讓它成爲類,INT,雙,布爾..)你必須返回符合規格的值。類型

0

當函數定義爲int作爲其返回類型時,函數必須返回int。否則,當該函數被調用時,該程序可能具有未定義的行爲。

可以解決你的問題,有兩種方式:

  1. 更新功能返回一個值。

    int pick(int number) 
    { 
        if(number % 7 == 0) 
        { 
         cout << setw(7) << "7777777" << setw(7) << number << endl; 
        } 
        else 
        { 
         cout << setw(7) << "*******" << setw(7) << number << endl; 
        } 
        return 0; // Or something that makes more sense in your program. 
    }  
    
  2. 更新函數返回void

    void pick(int number) 
    { 
        if(number % 7 == 0) 
        { 
         cout << setw(7) << "7777777" << setw(7) << number << endl; 
        } 
        else 
        { 
         cout << setw(7) << "*******" << setw(7) << number << endl; 
        } 
    }  
    

您可以對其他重載函數類似的變化。

相關問題