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
using namespace std;
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);
cout << "Please enter the number of dollars you have: $";
cin >> money;
cout << "With " << money << " dollars, you can buy " << recursivecandy(money, coupons) << " candy bars.\n";
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 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.
I mean that once your recursivecandy function has been called, there is no legitimate way to get out of it. Consider:
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)
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?
int recursivecandy(int A, int B, int C) //A is money, B is coupons, C is candybars
C = C + A +(B/7);
B = A + B/7, B%7;
A = 0;
recursivecandy(A, B, C);
return recursivecandy (A, B, C);
(int A, int B, int C) //A is money, B is coupons, C is candybars
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.