0

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;
    }
}

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.

Help would be greatly appreciated.

7
  • 1
    Where do you think the recursed result of load_price(); is going? Commented Dec 29, 2014 at 15:25
  • 1
    Don’t, I repeat don’t, use pointers and new here. It’s completely meaningless, and it actually introduces a memory leak into your code. Commented Dec 29, 2014 at 15:26
  • @WhozCraig I honestly have no idea. It seems to be burried somewhere within the function itself. Commented Dec 29, 2014 at 15:28
  • 1
    @KonradRudolph Thanks for the tip, m8, replaced the Goods object on heap with a stack object. Commented Dec 29, 2014 at 15:29
  • @WhozCraig: The correct answer is: nowhere. Commented Dec 29, 2014 at 17:11

2 Answers 2

5

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

return load_price();
Sign up to request clarification or add additional context in comments.

3 Comments

Shall my return price; be replace with said code, Barmar?
@OndřejŠimon No, all of your recursive calls should be replaced.
@CaptainGiraffe Oh I see, replacing the recursive call load_price() with return load_price(); as Barmar recommended worked. Thank you, guys.
1

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;
   }
}

3 Comments

Thank you for the iterative solution. To be honest I was trying to find one but could not wrap my head around it. I will use yours, if you don't mind. I do not really like recursion, just could not figure the iterative solution out.
Of course, feel free to use it. I won't post anything here if I don't want people to use it.
Thank you once again, Hans. I altered my other 2 recursive methods using your template, methods I used to check for inserted ID and amount of Goods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.