# Thread: need some quick help

1. ## need some quick help

I'm at yet another exercise in my book:
Code:
```Create a program
that prompts the user to enter an amount of money between \$0
and \$10(decimal places aloud). Determine how many quarters(25c)
dimes(10c) nickels(5c) and pennies(1c) are needed to make up that
amount. Output this information to the screen and ensure the
output makes gramatical sense.```
I'm not that far into the program - but I've hit a bit of a snag already. Here is my code:
Code:
```#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
double cash = 0;
const double quarter = .25;
const double dime = .10;
const double nickel = .05;
const double penny = .01;
double quarters = 0;
double dimes = 0;
double nickels = 0;
double pennies = 0;
double change = 0;

cout <<"Please enter an amount of money from \$0 dollars, to \$10 dollars: ";
cin >> cash;
cout << endl;

if((cash >= 0) && (cash <= 10)){
quarters = cash / quarter;
change = quarters * quarter;
cash = cash - change;

cout << quarters;
cout << endl;
cout << cash;
cout << endl;

}
else{
cout <<"You entered an invalid amount of money: ";
cout << cash;
cout << endl;
}

system("PAUSE");
return EXIT_SUCCESS;
}```
The problem lies somewhere with the variable 'cash' of type double. When I output it to the screen after the program's done calculating how much money is left to work with after the number of quarters has been found - the value of change is printed to the screen as '0'. However, the number of quarters comes out fine. I'm very confused!

Here is the program's logic - just so we all know what's supposed to happen - the user enters an amount of money... then the program finds out how many quarters there are in the amount - the value of all the quarters is added up and stored as 'change'.. which is then subracted from cash to come up with the amount of money minus the quarters... which is 'cash'.

I'll take that an apply it with dimes, nickels, and pennies as well - but that's after we all figure out why cash is displayed as 0! My guess is that I made a stupid mistake with the arithmatic that I fail to see.

2. Code:
```quarters = cash / quarter;
change = quarters * quarter;
cash = cash - change;```
Let's examine this if cash is, say, \$1.20.
Code:
```quarters = cash / quarter;    quarters = 1.2 / .25 = 4.8
change = quarters * quarter;    change = 4.8 * .25 = 1.2
cash = cash - change;    cash = 1.2 - 1.2 = 0```
You do have a slight problem in your algorithm. Try this code:
Code:
`quarters = static_cast<int>(cash / quarter);`
Which would be
Code:
```quarters = cash / quarter;    quarters = 1.2 / .25 = 4
change = quarters * quarter;    change = 4 * .25 = 1
cash = cash - change;    cash = 1.2 - 1 = .2```
That should fix it.

3. Modulos are another nice way to do it:
Code:
```quarters = (cash - (cash % 0.25) / 0.25)
change = cash % 0.25```
Wait, you only want whole numbers of quarters right?

4. Modulo operator only works for ints, so it won't work since floats
have to be used:

Create a program
that prompts the user to enter an amount of money between \$0
and \$10(decimal places aloud).

5. Modulo operator only works for ints, so it won't work since floats
You can use fmod() (in <cmath>), which is like a modulus for doubles. But my suggestion should fix the problem.

6. Hmm, thats a strange flaw.

7. Originally Posted by dwks
Code:
```quarters = cash / quarter;
change = quarters * quarter;
cash = cash - change;```
Let's examine this if cash is, say, \$1.20.
Code:
```quarters = cash / quarter;    quarters = 1.2 / .25 = 4.8
change = quarters * quarter;    change = 4.8 * .25 = 1.2
cash = cash - change;    cash = 1.2 - 1.2 = 0```
You do have a slight problem in your algorithm. Try this code:
Code:
`quarters = static_cast<int>(cash / quarter);`
Which would be
Code:
```quarters = cash / quarter;    quarters = 1.2 / .25 = 4
change = quarters * quarter;    change = 4 * .25 = 1
cash = cash - change;    cash = 1.2 - 1 = .2```
That should fix it.
I see the problem now! thatnks for pointing it out! I never thought once about a remainder.. but if I did - I know for sure that I would have known to use a static cast!

8. hmm, now having updated the code to handle dimes, nickels and pennies -- I am getting some funky output in the penny category.. have a look:

Code:
```#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
double cash = 0;
const double quarter = .25;
const double dime = .10;
const double nickel = .05;
const double penny = .01;
double quarters = 0;
double dimes = 0;
double nickels = 0;
double pennies = 0;
double change = 0;

cout <<"Please enter an amount of money from \$0 dollars, to \$10 dollars: ";
cin >> cash;
cout << endl;

if((cash >= 0) && (cash <= 10)){
quarters = static_cast<int>(cash / quarter); //kills a remainder
change = quarters * quarter;
cash = cash - change;

dimes = static_cast<int>(cash / dime);
change = dimes * dime;
cash = cash - change;

nickels = static_cast<int>(cash / nickel);
change = nickels * dime;
cash = cash - change;

pennies = static_cast<int>(cash / penny);
change = pennies * penny;
cash = cash - change;

cout << quarters <<" quarters\n";
cout << dimes <<" dimes\n";
cout << nickels <<" nickels\n";
cout << pennies <<" pennies\n";

}
else{
cout <<"You entered an invalid amount of money: ";
cout << cash;
cout << endl;
}

system("PAUSE");
return EXIT_SUCCESS;
}```
I sometimes can end up with a negative amount of pennies, and sometimes when there should be a penny - there is no penny... For example: in the program if I enter 1.20 I get 4 quarters(1.00) one dime (.10) and 1 nickel(.05) for a combined total of \$1.15. If I enter .05, I get one nickel and -5 pennies... ect.

Obvously I'm not accounting for some change in the values in the aglorithm -- could someone spot the problem? My brain is in the process of turning to mush!

9. well first since you can't have negitive money you might as well use a unsigned. Second, when ever you are getting odd ammounts when you are using #s or even strings, just start cout'ing them before and after they should change, then you can see where you program is messing them up

10. Is there another algorithm I should be using to solve this problem? One that is a bit more manageable?

11. ## more loop trouble

nothing -- ignore