递归函数不返回指定值 [英] Recursive function does not return specified value
本文介绍了递归函数不返回指定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图调试用于验证用户输入的递归函数,并在输入正确时返回值。该函数如下所示:
I am trying to debug a recursive function used to validate user input and return a value when the input is OK. The function looks like this:
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;
}
}
方法set_price / strong>类如下
The method set_price() of Goods class looks as follows
bool Goods::set_price(double price)
{
if (price> 0)
{
priceSingle_ = price;
priceTotal_ = price* amount_;
return true;
}
return false;
}
我试图在纸上绘制问题,但我的所有图表似乎看起来方式我的功能已经看起来像。我认为返回有一些问题,但我不知道在哪里。
I tried drawing the problem on a paper but all my diagrams seem to look the way my function already looks like. I think there are some problems with returns, but I do not know where.
帮助将非常感激。
推荐答案
您没有使用递归调用的返回值。您需要:
You're not using the return value of the recursive call. You need to do:
return load_price();
这篇关于递归函数不返回指定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文