Coins value

Hello again,

I did some work on coins value. I got most working but last part at line 34 and 48. I realize i ran out of idea what to put in or how math work. I need value to work out as $5.30 something like that. I'm sorry, my math is my worse.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  #include "std_lib_facilities.h"

int main()

{
		cout << "How many pennies do you have?\n";
		
		cout << "\n";
	
	int cents;
	
	int pennies;
	
	int nickels;
	
	int dimes;
	
	int quarters;
	
	int dollars;
	
	double value;
	
			cin >> cents;
	
	pennies = cents;
	nickels = cents / 5;
	dimes = cents / 10;
	quarters = cents / 25;
	dollars = cents / 100;
	value = cents / 100.0;
	
		cout << "\n";
	
		cout << "Aha you have " << cents << " cents right now\n";
		cout << "Aha you have " << nickels << " nickels right now\n";
		cout << "Aha you have " << dimes << " dimes right now\n";
		cout << "Aha you have " << quarters << " quarters right now\n";
		cout << "Aha you have " << dollars << " dollars right now\n";
		cout << "Your value is $" << value << ".";
	
}


edited: fixed the mistake. thanks
Last edited on
If you want to have a variable that can handle decimals, you should declare it as float or double.

Then, the value that you put in this variable can not be an integer, but fortunately if you do a calculation with an integer and a double, the result will be a double.

So, you should declare:

double value;

and the calculation should be
value = cents / 100.0;

Kind regards, Nico
And a minor side note, because memory is limited the accuracy of decimal numbers in your program is limited. As a result if you have a double value and you perform a calculation with another double value, the result might be a bit off.

Therefor, if you are doing calculations with things like money, it is often best to convert all values to the smallest possible unit (in this case cents), calculate with that and only convert the result to a value with decimals.
You already did this here, but just in case it was not on purpose I wanted to mention it anyway.
Thanks fixed it, it worked. I gotta make new thread for new problem =/.
Topic archived. No new replies allowed.