# Problem with change return problem.

• 12-02-2009
brian75
Problem with change return problem.
I am writing a program that is supposed to take an amount of change the user imputs and then the program is supposed to output the number of quarters, dimes, nickles, and pennies needed to make up the amount given prior.

I get the program to run and able to input the change. When the output of coins is given you get the right number of qurters multiplied by 100. Ex. If you put in 75 for change you will get 300 for quarters, 3000 for dimes, etc, etc. The first digit is correct but the rest isn't.

It doesn't make sense to me. Are my equations written wrong or did I go about this the wrong way.

Code:

```#include <iostream> int change; int quarter; int dime; int nickle; int pennie; int main() {         std::cout << "Amount of change: " ;         std::cin >> change ;         quarter = ( change / .25 ) ;         dime = ( change / .25 ) / .1 ;         nickle = ( ( change / .25 ) / .1 ) / .05 ;         pennie = ( ( ( change / .25 ) / .1 ) / .5 ) / .01 ;         std::cout << "Anount due:" << '\n' ;         std::cout << "  quarters: " << quarter ;         std::cout << "  dimes: " << dime ;         std::cout << "  nickles: " << nickle ;         std::cout << "  pennies: " << pennie ;         return (0); }```
Thank you in advance and helping me work through this.
• 12-02-2009
King Mir
There are two major problems.
1) The logic for quarters is correct, but then what you do for dimes and down is wrong. The number of dimes is not the number of quarters divided by 0.1 as your logic suggests.

2)Your logic seems oblivious to the types of variables. You apparently ask for a real(non-integer) number as the amount of change, but store it as an int. Then you try to divide it by a double, which represents a real number in C++. The results of this is of type double, but sometimes you store it as an int, other times you use it in a formula with doubles. This results in a number of errors.

Before you rush to change types to double, you should be aware that 0.01, 0.1, and 0.05 cannot be exactly represented, and using it may result in rounding errors, particularly if you convert the result to to an int or if you do long calculations with it and other. A solution is to immediately multiply the input by 100, cast to int, and use the int value for future calculations.
• 12-03-2009
brian75
Thank you for posting. I dont exactly follow what you are talking about but I think I have a general understanding. I will try and change my code up tonight and post the new one in and see if it is any better.
• 12-03-2009
since
Do the math of your code on paper, it doesn't work.

Code:

```quarter = ( change / .25 ) ; dime = ( change / .25 ) / .1 ; nickle = ( ( change / .25 ) / .1 ) / .05 ; pennie = ( ( ( change / .25 ) / .1 ) / .5 ) / .01 ;```
Change = 100.

No:
Quarters: Change / 25 = 4 (exception...)
Dimes: Change / 25 / 10 = 0.4
Nickels: Change / 25 / 10 / 5 = 0.08
Pennies: Change / 25 / 10 / 5 / 1 = 0.08

Yes:
Quarters: Change / 25 = 4
Dimes: Change / 10 = 10
Nickels: Change / 5 = 20
Pennies: Change / 1 = 100

The other thing is, you wouldn't give back all of the quarters, dimes, nickels and pennies that can fit in to Change. So you have to figure out what the best way to give change is, which can be a simple algorithm using MOD and what not, which is for you to figure out / attempt. >:]
• 12-03-2009
brian75
I am not sure if this is what you guys were suggeting me to do but I got this to run with no errors and ouput the correct numbers.

Here is the code I have now to date. Any inprovements for me?

Code:

```#include <iostream> int change; int quarter; int dime; int nickle; int pennie; int main() {         std::cout << "Amount of change: " ;         std::cin >> change ;         quarter = ( change / 25 ) ;         dime = ( change % 25 ) / 10 ;         nickle = ( ( change % 25 ) % 10 ) / 5 ;         pennie = ( ( ( change % 25 ) % 10 ) % 5 ) / 1 ;         std::cout << "Anount due:" << '\n' ;         std::cout << "  quarters: " << quarter ;         std::cout << "  dimes: " << dime ;         std::cout << "  nickles: " << nickle ;         std::cout << "  pennies: " << pennie << '\n' ;         return (0); }```