Converting money into bills and change

I have a lab where I am asked to take an amount of money and convert it into the types of bills and coins. We only have an infinite amount of US $20, $5, $1, 50c, 25c, 10c, 5c, and 1c. I started writing the code: the output for the bills is always correct, but sometimes the change is the wrong amount. An example output is below. I think my amount is rounding somewhere in the code. I was informed that float and double and not great when converting money, but I was not sure any other to approach the lab.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <stdlib.h>
#include <math.h>

using namespace std;

int main ()
{
    double amount;
    int twenty, five, one, half_dollar, quarter, dime, nickel, penny; 

    cout <<"Please enter amount: ";
    cin >> amount;
    cout <<"You will need ";

    if (amount >= 20)
    {
        twenty = (amount/20);
        amount = (amount-(20*twenty));
        if (twenty > 0)
        {
            cout << twenty << " $20 bill(s). ";
        }        
    }

    if (amount >= 5 )
    {
        five = (amount/5);
        amount = (amount-(5*five));
        if (five > 0)
        {
            cout << five << " $5 bill(s). ";
        }    
    }

    if (amount >= 1 )
    {
        one = (amount/1);
        amount = (amount-(1*one));
        if (one > 0)
        {
            cout << one << " $1 bill(s). ";
        }        
    }

    if (amount >= 0.50)
    {        
        half_dollar = (amount/0.50);
        amount = (amount-(0.50*half_dollar));
        if (half_dollar > 0)
        {
            cout << half_dollar << " 50c coin(s). ";
        }        
    }

    if (amount >= 0.25 )
    {       
        quarter = (amount/0.25);
        amount = (amount-(0.25*quarter));
        if (quarter > 0)
        { 
            cout << quarter << " 25c coin(s). ";
        }
    }

    if (amount >= 0.10 )
    {                    
        dime = (amount/0.10);
        amount = (amount-(0.10*dime));
        if (dime > 0)
        {
            cout << dime << " 10c coin(s). ";
        }
    }

    if (amount >= 0.05 )
    {      
        nickel = (amount/0.05);
        amount = (amount-(0.05*nickel));
        if (nickel > 0)
        {
            cout << nickel << " 5c coin(s). ";
        }
    }

    if (amount >= 0.01)
    {                     
        penny = (amount/0.01);             
        amount = (amount-(0.01*penny));        
        if (penny > 0)
        {
            cout << penny << " 1c coin(s).\n";
        }
    }

    system("pause");
    return 0;
} 


Please enter an amount: 22.65
You will need: 1 $20 bill(s). 2 $1 bill(s). 1 50c coin(s). 1 10c coin(s). 4 1c coin(s).
Last edited on
Yeah. It's floating point rounding error. You should probably ask the user to input how many dollars they have and how much change they have, then convert it all to an integer value in pennies.
1
2
3
4
5
6
7
8
9
int amountChange, amountCash, pennies;
cout << "Please enter amout of dollars: ";
cin >> amountCash;
cout << "Please enter amount of change: "
cin >> amountChange;

pennies = amountCash * 100 + amountChange;

//figure out bill and change 
Topic archived. No new replies allowed.