Start New Discussion within our Software Development Community

This is another homework problem. I thought it was easier, and I wouldn't need help, but I keep getting a segmentation fault when running the program.
The assignment is to use a recursive function to output the amount of candybars we can buy with a user-inputted amount of money. Each candybar costs $1, and each candybar gives a coupon. 7 coupons can be redeemed for an additional candybar. The example he gave us is:

"For example, if we have $20 dollars then we can initially buy 20 candy bars. This gives us 20 coupons. We can redeem 14 coupons for 2 additional candy bars. These two additional
candy bars have 2 more coupons, so we now have a total of 8 coupons when added to the 6
left over from the original purchase. This gives us enough to redeem for 1 more candy bar.
As a result we now have 23 candy bars and 2 left over coupons."

Here is the code I have. When I don't get a segmentation fault, I get it returning zero.

//File Name: assg3.cpp

#include <iostream>
using namespace std;

int candybars=0;

int recursivecandy(int A, int B) //A is money, B is coupons
       candybars = candybars + A +(B/7);
       B = A + B/7, B%7;
       A = 0;
       return recursivecandy(A, B);
    }while (B>=7);

int main()
    int money;
    int coupons=0;
    cout << "Please enter the number of dollars you have: $";
    cin >> money;
    cout << "With " << money << " dollars, you can buy " << recursivecandy(money, coupons) << " candy bars.\n";
    return 0;

Fairly straightforward, I use a do-while loop to run inside the recursive function because I want it run at least once (you start with no coupons, which is my stopping case). Does anyone see anything wrong? Any help would be greatly appreciated.

I suggest using a debugger to step through the code. It will show you exactly where/when the program crashes, as well as you can look at the conditions (values of variables, etc) when it occurs.


I don't even need to ask how your program is supposed to work because it can't -- there is no way for recursivecandy to return that does not involve a recursive call, so there is no way for it ever to return a meaningful value.

What do you mean? I call the recursive function in the main function (line 25) and within the recursive function, I call the recursive function (line 15).

I mean that once your recursivecandy function has been called, there is no legitimate way to get out of it. Consider:

int foo()
    return foo();

There is no way out of this function, because in order to return, it must first call itself, and in order for that call to return, it must first call itself again, and so on. Let's try to solve that problem by making the call conditional:

int bar(int n)
    if (n < 100)
        return bar(n+1);

Now there are two ways to leave this function. One is by falling off the end, which returns garbage; the other is by calling itself first. So again there is no legitimate way to leave it.

Your recursivecandy function has the same problem as my bar function.

So you're saying I need two options, one that will evaluate the recursive function, then when that is completed, the function is returned by the other option?
Something Like:

int recursivecandy(int A, int B, int C) //A is money, B is coupons, C is candybars
    if (B>=7)
       C = C + A +(B/7);
       B = A + B/7, B%7;
       A = 0;
       recursivecandy(A, B, C);
       return recursivecandy (A, B, C);

Allow me to stick my nose in here for a peripheral point, but there's one surefire way to make sure that someone reading your code knows what is money, what is coupons, and what is candybars. Name your variables "money","coupons",and "candybars."

Meaningful names for your variables are important, especially when you get to page 110 of a piece of code and your comment about A,B, and C is many pages back.

Yes, but the only time 'A, B, and C' are mentioned are in the small recursive function. When the function is called, I use named variables - that's just based off how my instructor has written the examples.

Haha, you will quickly learn that just because your instructor does it doesn't mean it is right :) I'd follow jonsca's advice on this one.

I realize that, and in a real-world situation, I would do so. Just now I follow my instructor's example.
