Recursive function does not return specified value

前端 未结 2 1101
栀梦
栀梦 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<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;
       }
    }
    
    0 讨论(0)
  • 2020-11-22 04:47

    You're not using the return value of the recursive call. You need to do:

    return load_price();
    
    0 讨论(0)
提交回复
热议问题