Getting Wrong values for money to coins program

Hey guys. I am trying to make a program that will convert a $ amount to change. Ex. $4.35 = 4 dollars, 1 quarter, 1 dime, 0 nickels, 0 pennies.

The problem is that when you put in some values, it doesn't work correctly.
So to find the problem, I removed the Penny and Nickel If statements.
So with the only the dollar, quarter, and dime decision statements remaining...:
Ex. When I put in $0.35 it gives me 0 dollars, 1 quarter, and 0 dimes. with 0.10 cents still remaining. How is this So???

[#include<iostream>
using namespace std;
#include <iomanip>
int main()
{
int dollars = 0, quarters = 0, dimes = 0, nickels = 0, pennies = 0;
double change;
cout << setprecision(2) << fixed;

cout << "Enter Amount" << endl;
cin >> change;

if (change >= 1.00)
{
dollars = change / 1.00;
change = change - dollars * 1.00;
}
if (change >= 0.25)
{
quarters = change / 0.25;
change = change - quarters * 0.25;
}
if (change >= 0.10)
{
dimes = change / 0.10;
change = change - dimes * 0.10;
}
/*if (change >= 0.05) //**************** I have omitted this part out
{
nickels = change / 0.05;
change = change - nickels * 0.05;
}
if (change >= 0.01)
{
pennies = change / 0.01;
change = change - pennies * 0.01;
}
*/ // *************** Omitted out
cout << "**************************" << endl;
cout << "Change left " << change << endl << endl;
cout << "Dollars: " << dollars << endl;
cout << "Quarters: " << quarters << endl;
cout << "Dimes : " << dimes << endl;
cout << "Pennies : " << pennies << endl;
return 0;
UPDATE****

My brother was helping me debug this and we found this:

When I enter .35 for change, it is showing up as 0.349999999999999998 on the debugger.
This only happens on certain numbers. like for .25 it is 0.250000000000000001.

Does anyone know about this??
Does anyone know about this?
Yes, but floating point arithmetic is a relatively esoteric topic. Without going into detail, many values that are exactly representable in a decimal system cannot be represented in a binary system, and floating point math usually accumulates rounding errors.

Floating point numbers should not be compared for equality if you can help it.

Because of rounding error, real financial software will not use floating point arithmetic, at least without taking special precautions. For your purposes, you can use two integer values: one for cents, the other for dollars, or maybe just one integer, keeping all your values in terms of cents.

I guess you could do something like this:

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
#include <iostream>

// Don't use the form
/*   using namespace x; */
// Because it may introduce [i]silent changes[i] to your program's behavior

int main() {
    int dollars = 0, quarters = 0, dimes = 0, nickels = 0, pennies = 0;
    int change; // change this amount of cents

    std::cout << "Enter amount in cents\n";
    std::cin >> change;	

    if (change >= 100) {
        dollars = change / 100; 
        change -= dollars * 100; 
    }
    if (change >= 25) {
        quarters = change / 25; 
        change -= quarters * 25; 
    }
    if (change >= 10) {
        dimes = change / 10; 
        change -= dimes * 10;
    }
    
    // more cases ... 

    std::cout << "**************************\n"
              << "Change left " << change   << "\n\n"
              << "Dollars: "    << dollars  << '\n'
              << "Quarters: "   << quarters << '\n'
              << "Dimes : "     << dimes    << '\n'
              << "Pennies : "   << pennies  << '\n';
    return 0;
}

Live demo:
http://coliru.stacked-crooked.com/a/005d4342e7f5d562

It's worth noting that this greedy algorithm doesn't minimize the number of coins for every coin system. The change-making problem is a textbook dynamic optimization problem.
Last edited on
Ok thanks for the replies. I did a bunch of reading on this topic last night so it is a big surprise to me that I have not had a problem with it until now.
when i had to do a similiar probably my teacher just said to use int and use the last two digits as decimals. I.E 12866 would be $128.66 and 24 would just be $.24
Topic archived. No new replies allowed.