can't seem to workout pennies

I was given an assignment where I ask the user for an amount in $0000.00 then tell him that amount in coins.
for example $2500.25 would be 100001 quarters, 0 dimes, 0 nickles, 0 pennies

here is my code, but it seems I can't workout the pennies right
if i input an amount like 2500.76, it says 0 pennies except there is actually one penny.

can anyone show me what is wrong?


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
43
44
45
46
47
48
49
50
51
52
53


#include <iostream>
using namespace std;


int main ()
{

	
	double total; // amount of user input
	int quarters; // amount of quarters
	int pennies; // amount of pennies
	int dimes; // amount of dimes
	int nickles; // amount of nickles
	


	cout << "Enter an amount of US Dollars in the format: "; //Prompt User for Amount
	cin >> total; // Input Amount

	pennies = static_cast<int>(total * 100); // convert floating point amount into (pennies) integer

	quarters = pennies / 25; // how many quarters
	
	dimes = (pennies % 25)/10; // how many dimes left
	
	nickles = (pennies % 25 % 10) / 5; // how many nickles left
	
	pennies = (pennies % 25 % 10 % 5 % 1) / 1; // how many pennies left



	
	cout << "The amount of coin is: \n"<< quarters  << " quarters, \n"; // output amount into coins
	cout << dimes  << " dimes, \n";
	cout << nickles  << " nickle, \n";
	cout << pennies  << " pennies. \n";


	
}   // end main










pennies = (pennies % 25 % 10 % 5 % 1) / 1;

X % 1 is always 0. Why are you doing %1 here?

This would probably be easier if you modified pennies as you went along:

1
2
3
4
5
6
7
8
9
pennies = static_cast<int>(total * 100); // convert floating point amount into (pennies) integer

quarters = pennies / 25; // how many quarters
pennies -= quarters * 25;

dimes = pennies / 10; // how many dimes left
pennies -= dimes * 10;

//...etc 




This is just a wild guess, but it might have to do with the order in which the % operator is processed. You're implicitly assuming it is handled from left to right, but that might not be the case.
@ Disch
here is the change, now 2500.76 works, but if enter an amount like 1234.56, it gives me a pennies when it suppose to be 0, im not sure if i followed your advice correctly




1
2
3
4
5
6
7
8
9
10
11
12
pennies = static_cast<int>(total * 100); // convert floating point amount into (pennies) integer

	quarters = pennies / 25; // how many quarters
	pennies -= quarters * 25;

	dimes = (pennies % 25)/10; // how many dimes left
	pennies -= dimes * 10;

	nickles = (pennies % 25 % 10) / 5; // how many nickles left
	pennies -= nickles * 5;

	pennies = (pennies % 25 % 10 % 5) / 1;  // how many pennies left 
but if enter an amount like 1234.56, it gives me a pennies when it suppose to be 0


Err... 1234.56 should have 1 penny, not zero.

Also the whole reason to do the pennies -= quarters * 25; was so you don't have to put that mod (%) operator all over the place.

1
2
quarters = pennies / 25;
pennies -= quarters * 25; // after this, pennies will be < 25, so you don't need to % 25 any more. 


Alternatively you could do this:
1
2
quarters = pennies / 25;
pennies %= 25;


This would have the same effect.
Last edited on
@Disch yea i made a mistake, you're right there is a penny

my teacher insists we use % in our assignment

I followed your second example and it worked, thank you very much!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


	
	pennies = static_cast<int>(total * 100); // convert floating point amount into (pennies) integer

	quarters = pennies / 25; // how many quarters
	pennies %=25;

	dimes = (pennies % 25)/10; // how many dimes left
	pennies %=10;
	nickles = (pennies % 25 % 10) / 5; // how many nickles left
	pennies %=5;
	
		
	



i realize if i took out the two 1s from my old code, it works also,
what do you think? i know its less efficient this way but when i enter 1234.56 or 2500.76 it got the answers right

1
2
3
4
5
6
7
8
9
10
11
12
13
14

	
	

	pennies = static_cast<int>(total * 100); // convert floating point amount into (pennies) integer

	quarters = pennies / 25; // how many quarters
	
	dimes = (pennies % 25)/10; // how many dimes left
	
	nickles = (pennies % 25 % 10) / 5; // how many nickles left
	
	pennies = (pennies % 25 % 10 % 5); // how many pennies left







Topic archived. No new replies allowed.