Cash register program - having trouble with very small values (pennies)

Hi folks,

I am having some hair-pulling trouble getting very small amounts (i.e., pennies) to work right with a fictional (for fun) cash register program I created. For some background information, I am reviewing and trying to polish my C++ skills after having taken an introductory course in the language many moons ago. I would greatly appreciate any help or advice you can offer!

My program asks for the total cost of a customer's purchase and the cash amount tendered to the cashier. It will then display how many 20s, 10s, 5s, and 1s to give back. It will also tell how many quarters, dimes, nickels, and pennies to give back. I have used successive division and modulus remainder math to calculate these totals.

The problem I am running into is with pennies. Say the total cost of the bill is $9.99, and the customer hands over a $10 bill. The program should say to give 1 penny to the customer in change, but the number calculated is 0.

Similarly, here's what happens when I test the program, giving a $10 bill to the cashier with the following bill totals:

$9.98 - outputs 1 penny, when it should say 2;
$9.97 - outputs 2 pennies, when it should say 3;
$9.96 - outputs 3 pennies, when it should say 4;

I have converted change that should be returned into a value in pennies, multiplying by 100. I have tried multiplying by larger amounts (instead of 100) and then dividing later to no avail. I have also used floating point values for modulus division using fmod, also to no avail! Any help in the right direction to get these small amounts taken care of would be appreciated (so I don't reach for the nearest sharp object!).

Below is my C++ code. Thank you for any assistance.
Last edited on
I converted the pennies to double and did not use a modulus operator for it. Then I define the variable pennies from the value of changeReturned * 100, like so:

double pennies = changeReturned * 100; // I did not use the changeReturnedCents because it is an int and would drop any decimals (i.e. 2.9 to 2)

It works to me. If you use the pennies as int, then if the answer is 2.9, it will drop the .9 and give you the answer of 2. I hope it will give you an idea.

I do not recommand approximation with currency . What if this was for M or G $ for stock exchanges ?
@chicofeo - I appreciate your help. I did try using a double value like you gave for the pennies variable, then dividing using fmod() for modulus division to get the remainder for all 20s, 10s, 5s, 1s, quarters, dimes, nickels, and pennies. Unfortunately, I still wound up with the same rounding errors for pennies.

Question: As an alternate way of solving this rounding problem, is there a C++ function I could use to extract the two digits after the decimal place (e.g., say changeReturned gives a value of 1.01 after subtracting from a 10 dollar bill, and I want to store the .01 value in pennies.

Algorithmically, I understand I would need to search for the decimal point "." and find the first two significant digits, .01. Can you help with the syntax for that, or provide any further input? Still getting those penny rounding errors. Thanks in advance!

@Ericool - Very true. Reminds me of a scene in the movie Office Space. I think one would use long doubles for stock market programs, but this is just a simple fictional program to see if I "still got it" after all this time. ;-) Thanks for your input and recommendation!
Edit/update - I solved it! I used long double and did fmod() division again. Now, the program works like a charm.

This old man has some time yet before he's playing bingo in the nearest old folks' home. ;-)

Thank you for your help!
Topic archived. No new replies allowed.