遞歸函數(shù)不返回指定的值。我正在調(diào)試一個(gè)遞歸函數(shù),用于驗(yàn)證用戶輸入并在輸入正常時(shí)返回值。該函數(shù)如下所示:double load_price(){
double price;
Goods * tempGd = new Goods();
cin >> price;
while (!cin)
{
cin.clear();#undef max
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << endl;
cout << "You didn't enter a number. Do so, please: ";
cin >> price;
} // endwhile
if (!tempGd->set_price(price))
{
cout << endl;
cout << "The price " << red << "must not" << white << " be negative." << endl;
cout << "Please, insert a new price: ";
load_price();
}
else
{
delete tempGd;
return price;
}}的設(shè)置價(jià)格()的方法貨物類如下所示bool Goods::set_price(double price){
if (price> 0)
{
priceSingle_ = price;
priceTotal_ = price* amount_;
return true;
}
return false;}我試著在紙上畫這個(gè)問題,但是我的所有圖表看起來都和我的函數(shù)看起來一樣。我認(rèn)為回報(bào)有一些問題,但我不知道在哪里。我們將非常感謝您的幫助。
2 回答

30秒到達(dá)戰(zhàn)場(chǎng)
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
return load_price();

侃侃無極
TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊
#undef maxdouble load_price(){ for(;;) { double price; cin >> price; if (!cin) { cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); cout << endl; cout << "You didn't enter a number. Do so, please: "; continue; } if (!Goods().set_price(price)) { cout << endl; cout << "The price " << red << "must not" << white << " be negative." << endl; cout << "Please, insert a new price: "; continue; } return price; }}
- 2 回答
- 0 關(guān)注
- 446 瀏覽
添加回答
舉報(bào)
0/150
提交
取消