當前我試圖運行一個程序,您可以選擇一個整數或小數,並根據您的號碼被操縱。我的教授告訴我使用函數重載來實現這個功能。但每次我嘗試編譯它時,都會給出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;
}
修好了!謝謝!令人驚訝的是,如何讓一個小東西導致該程序找到101個警告給你關於整個代碼片段:( – user3763403
是的,'C++'可以有點挑剔,但它非常強大,但我看到一些其他樣式問題在你的代碼中,我並不認爲所有的警告都只是爲了失去回報,但這不是處理恐懼的地方,我建議[寫一本好書](http: //stackoverflow.com/q/388242/1171191)。 – BoBTFish