make change program help

This is a discussion on make change program help within the C++ Programming forums, part of the General Programming Boards category; Hi, I need some help making a program that outputs change in the least amount of dollars and coins. Here ...

  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 04: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,675
    Code:
    while(money[i] < difference)
    I used to be an adventurer like you... then I took an arrow to the knee.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    5,636
    yes, people know and give you hints. It is now upto you to catch these hints
    If I have eight hours for cutting wood, I spend six sharpening my axe.

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

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    5,636
    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?
    If I have eight hours for cutting wood, I spend six sharpening my axe.

  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, 03:58 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  4. how to make a program stop indeffinitely?
    By BrianK in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2002, 11:51 PM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21