Recursive function does not return specified value

前端 未结 2 1094
栀梦
栀梦 2020-11-22 04:14

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_pric         


        
2条回答
  •  感情败类
    2020-11-22 04:34

    Who talked you into using recursion for that problem?

    #undef max
    double load_price()
    {
       for(;;) {
          double price;
          cin >> price;
          if (!cin)
          {
             cin.clear();
             std::cin.ignore(std::numeric_limits::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;
       }
    }
    

提交回复
热议问题