Thread: make change program help

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    43

    make change program help

    Hi,
    I need some help making a program that outputs change in the least amount of dollars and coins. Here is what I have so far but it doesnt work correctly and I don't know why. Thanks.
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
       double charged, given, difference;
       cout << "Enter amount charged:\n";
       cin >> charged;
       cout << "Enter amount given:\n";
       cin >> given;
    
       difference = given - charged;
       double  money[10] = {100,50,20,10,5,1,.25,.10,.05,.01};
       double  change[10] = {0,0,0,0,0,0,0,0,0,0};
    
       for(int i =0; i<10; i++)
       {
         while(money[i] < difference)
         {
           change[i]++;
           difference -=  money[i];
         }
       } 
        
      
       
       for(int  i = 0; i<10; i++)
        if(change[i] != 0) 
         cout << "$" << money[i] << ": " << change[i] << "\n";
         
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You are very close.

    But I wonder what would be the reason that I get this output:
    Enter amount charged:
    5
    Enter amount given:
    10
    $1: 4
    $0.25: 3
    $0.1: 2
    $0.01: 4
    I mean, just why it wouldn't return 5 1-dollar bills (or 1 five-dollar bill for that matter)?! What makes it ignore the five-dollar bill, stop at 4 one-dollar bills and move on to smaller values?!
    Last edited by anon; 02-19-2008 at 05:00 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    while(money[i] < difference)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Does any one know why this doesnt work? Its suppose to return the least amount of bills and coins possible.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    yes, people know and give you hints. It is now upto you to catch these hints
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    What hints are you talking about?

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What will happen in a line noted by hk_mp5kpdw
    If you pay with 10$ bill for 5$ purchase as in a sample anon suggested you to debug?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    <=, thank you!

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Good job! You were apparently missing a single byte of source code for perfection
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you make a program send data to the printer?
    By Sintu in forum C++ Programming
    Replies: 19
    Last Post: 10-20-2005, 07:22 PM
  2. need help to make a program
    By holoduke in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2002, 04:58 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. how to make a program stop indeffinitely?
    By BrianK in forum C++ Programming
    Replies: 4
    Last Post: 10-29-2002, 12:51 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM