1 Attachment(s)
[RESOLVED] Help with cash register program
Hello all, I am new here and pretty new to programming as well. I have never posted code before and so I included my source file as an attachment. Basically I have been trying for days now to create a program that asks for the amount of money owed by a customer, and the amount paid, and then prints the total change to give back, as well as the number of dollars and coins of each denomination (i.e $4.66, 4 dollars, 2 quarters, 1 dime, 1 nickel, 1 penny).
I have reached the point where everything works but the pennies, and I believe it is because of using int values, however if I use doubles or floats I can't use the % division operator.
Sorry for the long post and thanks in advance for any help, if you need anymore information, or can explain how to post the code, I'll try to respond quickly.
p.s. I'm coding on a Linux machine, if that makes a difference.
Re: Help with cash register program
Sorry, I forgot to specify that I have written most of the program, but I need help finding what I did wrong or how I could do things differently. I am not asking anyone to code the program for me, but to perhaps provide insight into the problem, helpful suggestions, or links to resources that might help me understand where I've gone wrong.
thanks again.
Re: Help with cash register program
Floating point numbers can't represent all numbers exact down to the very last decimal so when you do this
Code:
noDecimal = change * 100;
truncation you will suffer from that (didn't your compiler warn you?). To overcome this you will have to round the value before truncating it and the usual way to do that is to add 0.5 LSB i.e.
Code:
noDecimal = 0.5 + change * 100;
Re: Help with cash register program
Quote:
Originally Posted by
~$ ./project_2501
Sorry, I forgot to specify that I have written most of the program, but I need help finding what I did wrong or how I could do things differently. I am not asking anyone to code the program for me, but to perhaps provide insight into the problem, helpful suggestions, or links to resources that might help me understand where I've gone wrong.
thanks again.
The first thing you should learn to use is your compiler's debugger, so that you can single step through your code yourself to discover your own errors without guessing what the error may be.
Second, you should have multiplied both inputs by 100 to turn them into the integer equivalents right away. Instead, you subtracted two floating point numbers from each other, and subtracting floating point numbers always leaves the risk of round-off errors.
Code:
cout << "Enter the amount owed: ";
cin >> owed;
cout << "Enter the amount paid: ";
cin >> paid;
owed *= 100;
paid *= 100;
Then the routine is very simple:
Pseudo-code example:
Code:
Owed: 4.38
Paid: 10.00
Owed = 438
Paid = 1000
Change = Paid - Owed = 1000 - 438 = 562.
NumDollars = 562 / 100 = 5.
Change = Change - 100 * NumDollars = 62
NumQuarters = Change / 25 = 2
Change = Change - 25 * NumQuarters = 12
NumDimes = Change / 10 = 1
....
Repeat same logic for number of dimes, nickels. Then pennies will be just this after completing nickels:
NumPennies = Change;
Therefore there is no need for multiple "remnant" variables. Just keep subtracting the sub-answers from change, and then use "change" in the next calculation.
There is also no need for floating point after you multiply the initial input by 100.
Regards,
Paul McKenzie
Re: Help with cash register program
Awesome, Thank you both so much for explaining this so thoroughly. I am taking a course in C++ but so far there has been VERY little programming so I am only able to learn by exploring on my own (which is what I like to do anyway). Unfortunately the instructor is very vague and the textbook seems to be on a totally different path than the one he's on. Anyway, Thanks again to both of you.