递归函数不返回指定的值。我正在调试一个递归函数,用于验证用户输入并在输入正常时返回值。该函数如下所示: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;
}}的设置价格()的方法货物类如下所示bool Goods::set_price(double price){
if (price> 0)
{
priceSingle_ = price;
priceTotal_ = price* amount_;
return true;
}
return false;}我试着在纸上画这个问题,但是我的所有图表看起来都和我的函数看起来一样。我认为回报有一些问题,但我不知道在哪里。我们将非常感谢您的帮助。
2 回答
侃侃无极
TA贡献2051条经验 获得超10个赞
#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 关注
- 426 浏览
添加回答
举报
0/150
提交
取消