Thread: problem with a loop

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    39

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    74
    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;
    }
    Last edited by Kurisu33; 08-30-2006 at 12:59 AM.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    39
    Quote 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.
    Sorry about the no-[code] thing.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    39
    Quote 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

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM