Cash Machine

Good evening. I hope you are all doing well. I am working on an assignment where the program is required to break a monetary amount into dollars, half dollars, quarters... I thought I made a clever function to solve this problem but when tested, my program sometimes looses a penny. Any input would be greatly appreciated.

Regards,

Joe

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
54
55
56
57
58
59
60
61
62
63
64
65
/*In this program, a function recieves a floating - point number entered by the user
that represents change from a purchase.  The function will pass back the breakdown
of the change in dollar bills, half dollars, quarters, dimes, nickels, and pennies.
	Written by:  Joe 
	Date: 15 Nov 2011
*/

#include <stdio.h>
#include <stdlib.h>

float main (void)
{
	// Local Def
	float retChng;
	int doll;
	int hlf;
	int qtr;
	int dm;
	int nic;
	int pny;


	// Statement
	

	do
	{ 	
	printf("Please enter a return change value less than $100.00:  ");
	scanf("%f", &retChng);
	if (retChng > 99.99)
		printf("\nThe value you entered is not below $100.00.\n");
	}
	while (retChng > 99.99);
	
	printf("The value you entered is: %.2f\n\n", retChng);

	printf("The change you will receive is:\n\n");

	pny =		retChng * 100;
	doll =		(pny / 100);
	pny =		(pny % 100);
	hlf =		(pny / 50);
	pny =		(pny % 50);
	qtr =		(pny / 25);
	pny =		(pny % 25);
	dm =		(pny / 10);
	pny =		(pny % 10);
	nic =		(pny / 5);
	pny =		(pny % 5);

	
	printf("%d dollar bills\n", doll);
	printf("%d half dollars\n", hlf);
	printf("%d quarters\n", qtr);
	printf("%d dimes\n", dm);
	printf("%d nickels\n", nic);
	printf("%d pennies\n", pny);

	system("PAUSE");

	return 1;


}//main
Hi, Joe -

I didn't look at your problem in detail, but when representing decimal fractions in a binary number system, it's possible to "lose a penny." Someone recently posted that very problem here.

The solution is to keep *all* your money in ints (no floats). This will take a little more processing of the input and output, but will solve the problem.
Thank you very much for your help. Have a good night.
Topic archived. No new replies allowed.