Coin program not calculating correct results

Trying to have an American coin calculator than asks you how many coins you have for each value and then gives out the total(sum). After tweaking and looking up different results I still haven't found a correct solution. Help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
   double quarters = 0, dimes = 0, nickels = 0, pennies = 0, total;
   cout << "Please enter change amount";
   cout << "\nPlease enter the amount of Quarters: ";
   cin >> quarters;
   cout << "Please enter the amount of Dimes: ";
   cin >> dimes;
   cout << "Please enter the amount of nickels: ";
   cin >> nickels;
   cout << "Please enter the amount of pennies: "; 
   cin >> pennies;
   cout << "Thank You!" << endl;
   total = ((pennies)+(nickels + 5) + (dimes + 10) + (quarters + 25))/100;
   cout << "Total:$" << total << endl;
   return 0;
}
Last edited on
> After tweaking and looking up different results I still haven't found a correct solution.
Do you know how to do this on paper, or are you just trying random formulae.

If I gave you three pennies and a dime, what would you expect to be printed?
Is't a Nickel worth a Half Dime? ;)

I suggest:
1
2
3
4
5
6
...
   unsigned  quarters = 0, dimes = 0, nickels = 0, pennies = 0;
   float  total;
...
   total = (pennies + nickels * 5 + dimes * 10 + quarters * 25)/100.;
...

-- or (if you like to avoid the obscure *)
total = (pennies + nickels + nickels + nickels + nickels + nickels + ... and so on
@salem c

I'm trying to configure it so it would be say $0.13.

Attempting to figure things out myself so I am trying different things. I've seen an example of the reverse ( Dollars into Coins) and other similar problems so yes, I'm testing multiple formulae.
Just started learning for myself and reading online guides(such as this website!), experimenting on questions is sort of my way to combine information and really wrapping things together for me. Hopefully I don't come out being completely idiotic.
Mikestgt I will try that thank you very much for the suggestion.
Hopefully I don't come out being completely idiotic.

You did already, well, not completely. But that is of minor importance. Most essential is that you dared to ask. Never stop asking! Or to quote Einstein: "The important thing is not to stop questioning. Curiosity has its own reason for existing."

In addition you may realize the differences:
-- why float total instead of integer,
-- why + nickels * 5 instead of + nickels + 5,
-- why converting pennies to USD by /100. instead of just /100 (without decimal point).

And, minor, why unsigned quarters ... instead of double quarters...? Well, money has no sign attached, neither + nor -, it is up to you how to account movements.
(And in case you have so many coins you'd rather go to the next bank and get them start their coin counting machine for a small fee.)
Topic archived. No new replies allowed.