Making change homework problem

Hi everyone, I am very new to programming, and don't know much yet. I have received a homework assignment in which I need to have a user input the total cost of something they are purchasing, and then input the amount they are tendering to pay for it. The output is meant to tell them how much they will receive back as change, and tell them which bills and coins they will receive. The assumptions that can be made, according to the instructions for the assignment, is that the total cost will never exceed 50 dollars, and that the customer will pay in whole dollar amounts, never with any cents (i.e., $5.00, $8.00, etc.), and that they will never tender more than $100.

My problem is that when I was testing the code, I put in the value 25.17 as the cost, and 50 as what was tendered. The code said that the customer is owed 24.83 as change, and that they would get 1 twenty dollar bill, 4 one dollar bills, 3 quarters, 1 nickel, and 2 pennies (and 0 of everything else). It should be 3 pennies. I have an idea that the problem was caused by placing values of type double into type int variables, but I am not totally sure on that. I'm not confident I know why the problem is occurring, and I don't know how to fix it. Can someone please provide me with a push in the right direction, or some suggestion on how to go about fixing this?

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
66
67
68
  #include <iostream>
using namespace std;

// Declaring constants for dollar values
const int FIFTY = 50;
const int TWENTY = 20;
const int TEN = 10;
const int FIVE = 5;
const int ONE = 1;

// Declaring constants for cent values
const double QUARTER = 0.25;
const double DIME = 0.10;
const double NICKEL = 0.05;
const double PENNY = 0.01;

int main() {

	double amtDue, // What customer owes
		   tendered, // What customer paid with
		   chgDue; // What change is due back to customer

	cout << "What is the total cost of the item(s) you are purchasing? ";
	cin >> amtDue;

	cout << "\nWhat is the cash you are tendering to make your purchase? ";
	cin >> tendered;

	chgDue = tendered - amtDue; // Calculating total change due back to customer

	double chgDueClone = chgDue; // Creating a variable to hold what variable chgDue holds, for later use

	// Declaring int variables to truncate change due to the customer to determine the quantity of cash denominations to be returned
	// Re-calculates the change due after each step
	int fifties = chgDue / FIFTY;
	chgDue = chgDue - (fifties * FIFTY);
	int twenties = chgDue / TWENTY;
	chgDue = chgDue - (twenties * TWENTY);
	int tens = chgDue / TEN;
	chgDue = chgDue - (tens * TEN);
	int fives = chgDue / FIVE;
	chgDue = chgDue - (fives * FIVE);
	int ones = chgDue / ONE;
	chgDue = chgDue - ones;
	int quarters = chgDue / QUARTER;
	chgDue = chgDue - (quarters * QUARTER);
	int dimes = chgDue / DIME;
	chgDue = chgDue - (dimes * DIME);
	int nickels = chgDue / NICKEL;
	chgDue = chgDue - (nickels * NICKEL);
	int pennies = chgDue / PENNY;
	chgDue = chgDue - (pennies * PENNY);

	// Display the output, telling the customer the total amount they are due, and the quantities of certain bill denominations and coins they will receive
	cout << "\nYou are due a total of $" << chgDueClone << endl << endl;
	cout << "You will receive:\n"
		<< fifties << " fifty dollar bills\n"
		<< twenties << " twenty dollar bills\n"
		<< tens << " ten dollar bills\n"
		<< fives << " five dollar bills\n"
		<< ones << " one dollar bills\n"
		<< quarters << " quarters\n"
		<< dimes << " dimes\n"
		<< nickels << " nickels\n"
		<< pennies << " pennies\n";

	return 0;
} // End main 
1. use ints multiply by 100.
2. Use modulus operator %

I have an idea that the problem was caused by placing values of type double into type int variables, but I am not totally sure on that.

Yes. A value 25.17 or any of the intermediates stored in double might not be exatly that due to the way doubles actually represent values and therefore rounding errors can creep up.

If you could create integers, in pennies, from the input values and use only integer math (operators / and %), then you should stay exact.

The price is of form x.y and corresponds to 100*x+y pennies, does it not?
The tender is full dollars and 100 times that in pennies.
So if I am understanding you both correctly, only use doubles for the cash input values, convert them to type int via finding their equivalence in pennies, and go from there by using % to find the remainders?
You can start with that. However, the use of double is not necessary in the input.

The user will supply two inputs. The first may contain cents, but the second is known to be whole dollars, i.e. an int.

You could read the first input as a "word" into a string. The word can contain one number, two numbers separated by a dot, or garbage. Check the cases and convert appropriate substring(s) into integer(s). There are multiple methods for that. Have you used istringstream yet?
Last edited on
closed account (48T7M4Gy)
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
...

const int FIVE = 500;
const int ONE = 100;

// Declaring constants for cent values
const double QUARTER = 25;

...

int main() {
    
    double amtDue = 0, tendered = 0;
    cout << "What is the total cost of the item(s) you are purchasing? ";
    cin >> amtDue;
    
    int centsAmtDue = (int)(amtDue * 100); // <--
    
    cout << "\nWhat is the cash you are tendering to make your purchase? ";
    cin >> tendered;
    
    int centsTendered = (int)(tendered * 100); // <---
    
    int centschDue = centsTendered - centsAmtDue;
    
    int fifties = centschDue / FIFTY;
    centschDue -= (fifties * FIFTY); // or use %
    
    int twenties = centschDue / TWENTY;
    centschDue -= (twenties * TWENTY);

...

    int pennies = centschDue;
    cout << "\nYou are due a total of $" << tendered - amtDue << endl << endl;
    cout << "You will receive:\n"
    << fifties << " fifty dollar bills\n"
    << twenties << " twenty dollar bills\n"
...
We have not used istreamstring yet, but I think I understand how to convert the things I need to into integers. I also appreciate the example Kemort provided, that is very helpful in visualizing what the general idea is for going about this. Thank you all!
Topic archived. No new replies allowed.