1. problem with a loop

Hi ,
I have this problem I need to code in C++:

we want to know how many candies you can have with a certain amonut of money.
Each candy costs \$1 and at each candy purchase, you receive 1 coupon.
You can redeem 1 candy with 7 coupons.

If you have \$20, you can have 23 candies.
20/7=2 more candies from the coupons=2 more coupons.
then 20%7=6 coupons remain.

6+2=8 coupons with which you can have 1 candy.

So 20+2+1=23 candies in total.

I try to use the modulo ("%") but still I cannot find the answer. Iwant to use a loop.
this what I did. I found 22 bars instead of 23.

#include<iostream>
using namespace std;

int main()
{
int money,n;
int s,finish;

cout<<" Enter the total amount of money ";
cin>>money;
do
{
s=(money/7)+(money%7);
n=money+(money/7);
}
while(s<7);

cout<< "bar "<<n;
cout<<endl;

cin>>finish;// keep the output window open

return 0;
}

Do you have an idea?
Thank you
B

2. My idea is you've been here long enough to know that code should be tagged with the [code] tag and not the [b] tag.

3. You seems to have a generally correct idea.
Code:
```do
{
s=(money/7)+(money%7);
n=money+(money/7);
}
while(s<7);```
However, I don't get this loop. It changes the value of s and n, but not the value of money (pun not intended). Therefore s should always get the same value and if money is 20 the loop should never exit.
Get a piece of paper and figure out (all) the steps you take to solve the problem.

4. Did your teacher tell you that with 20\$ you could obtain 23 candies?

From reading the problem it stands to reason that you have to PURCHASE a candy to receive a coupon. Reedeming 1 coupon to receive 1 candy+ another coupon doesn't make sense to me.

1\$ = 1 candy + 1 coupon.
Therefore: 20\$ = 20 candy + 20 coupon.

20 coupon = 2 candy + 6 remaining coupons.
Therefore 20 candy + 2 candy = 22 candy total with still only 6 coupons.. no? (not 8)

As for your code as is:
1) Might want to choose variable names more relevant/informative than s & n or your teacher will have a hissy fit.
2) Your loop has no changing condition as the above poster mentioned leading to an infinite loop.
3) There are better ways to keep your console window open than a cin >>.. been awhile since I programed a console app, but I think something like getCh() or worst case System("pause")
4) The code n=money+(money/7); by itself may give you the correct answer if you only get a coupon upon 1\$ purchase rather than your current 1\$ purchase or 1 coupon redeem. Do you want to use a loop or do you have to use a loop?

A valid loop requires a changing condition.. Example:
Code:
```int money, ;
cin >> money;

int candies = 0;
int coupons = 0;
int changingVariable = 0;   // This variable controls the loop and changes.  Of course you wouldn't call it changingVariable

while (changingVariable < money)
{
candies = candies + 1;
coupons = coupons + 1;

changingVariable = changingVariable + 1;
}```

5. Originally Posted by Salem
My idea is you've been here long enough to know that code should be tagged with the [code] tag and not the [b] tag.

6. Originally Posted by Kurisu33
Did your teacher tell you that with 20\$ you could obtain 23 candies?

From reading the problem it stands to reason that you have to PURCHASE a candy to receive a coupon. Reedeming 1 coupon to receive 1 candy+ another coupon doesn't make sense to me.

1\$ = 1 candy + 1 coupon.
Therefore: 20\$ = 20 candy + 20 coupon.

20 coupon = 2 candy + 6 remaining coupons.
Therefore 20 candy + 2 candy = 22 candy total with still only 6 coupons.. no? (not 8)

As for your code as is:
1) Might want to choose variable names more relevant/informative than s & n or your teacher will have a hissy fit.
2) Your loop has no changing condition as the above poster mentioned leading to an infinite loop.
3) There are better ways to keep your console window open than a cin >>.. been awhile since I programed a console app, but I think something like getCh() or worst case System("pause")
4) The code n=money+(money/7); by itself may give you the correct answer if you only get a coupon upon 1\$ purchase rather than your current 1\$ purchase or 1 coupon redeem. Do you want to use a loop or do you have to use a loop?

A valid loop requires a changing condition.. Example:
Code:
```int money, ;
cin >> money;

int candies = 0;
int coupons = 0;
int changingVariable = 0;   // This variable controls the loop and changes.  Of course you wouldn't call it changingVariable

while (changingVariable < money)
{
candies = candies + 1;
coupons = coupons + 1;

changingVariable = changingVariable + 1;
}```
Yes , I am sure about the number of total candies(23)
I will try to try your suggestion.
Thank you

B