Program issue with decimal

I am tasked with creating a code that takes the input cost and tendered amount and calculates the change and breakdown. I have everything working, except the decimal that needs to be in the change print out. I am stumped.


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
#include <iostream>
#include <iomanip>

using namespace std;
int main()
{
	double tendered;
	double cost;
	int change;
	int fifties, twenties, tens, fives, dollars, quarters, dimes, nickels, pennies;
		
	cout << fixed << setprecision(2);
	cout << "Enter amount of purchase and amount tendered: ";
	cin >> cost >> tendered;

	change = (tendered - cost)* 100+.5;
	

	cout << "For your purchase of $" << cost << " you tendered $" << tendered << endl;
	cout << "Your change is $" << change << endl;
		
	fifties = change / 5000;
	change = change % 5000;
	twenties = change / 2000;
	change = change % 2000;
	tens = change / 1000;
	change = change % 1000;
	fives = change / 500;
	change = change % 500;
	dollars = change / 100;
	change = change % 100;
	quarters = change / 25;
	change = change % 25;
	dimes = change / 10;
	change = change % 10;
	nickels = change / 5;
	change = change % 5;
	pennies = change / 1;

	cout << "Fifties:	" << fifties << endl;
	cout <<	"Twenties:	" << twenties << endl;
	cout << "Tens:		" << tens << endl;
	cout << "Fives:		" << fives << endl;
	cout << "Dollars:	" << dollars << endl;
	cout << "Quarters:	" << quarters << endl;
	cout << "Dimes:		" << dimes << endl;
	cout << "Nickels:	" << nickels << endl;
	cout << "Pennies:	" << pennies << endl;

	return 0;
}


my display shows:
Enter amount of purchase and amount tendered: 2.09
100.00
For your purchase of $2.09 you tendered $100.00
Your change is $9791
Fifties: 1
Twenties: 2
Tens: 0
Fives: 1
Dollars: 2
Quarters: 3
Dimes: 1
Nickels: 1
Pennies: 1
Press any key to continue . . .

Thanks for any help
I would probably print it like this:

cout << change/100 << "." << change%100;

you can do it in floating point, but you will get occasional roundoff that makes an ugly display, and you can correct that with rounding algorithms, but the above integer solution is how I would have done it.

9791 divided by 100 in integer is 97
with a remainder (%) of 91. so it prints 97.91

also, embrace the shortcut operators.

x= x+1
is the same as
x+=1

or,
change = change %100
is the same as
change %= 100


Last edited on
Just use / 100 for the dollars and % 100 for the cents.
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
#include <iostream>
#include <iomanip>
using namespace std;

struct Denom {
    const char *name;
    int amount;
} denoms[] = {
    {"Fifties",  5000},
    {"Twenties", 2000},
    {"Tens",     1000},
    {"Fives",     500},
    {"Ones",      100},
    {"Quarters",   25},
    {"Dimes",      10},
    {"Nickels",     5},
    {"Pennies",     1},
    {nullptr,       0}
};

int main() {
    double cost = 0, tendered = 0;
    cout << fixed << setprecision(2);
    cout << "Enter amount of purchase and amount tendered: ";
    cin >> cost >> tendered;

    int change = (tendered - cost) * 100 + 0.5;

    cout << "For your purchase of $" << cost << " you tendered $" << tendered << endl;
    cout << "Your change is $" << change / 100 << '.' << change % 100 << endl;

    for (int i = 0; denoms[i].name; i++) {
        cout << setw(8) << left << denoms[i].name << ": "
             << setw(4) << change / denoms[i].amount << '\n';
        change %= denoms[i].amount;
    }

    return 0;
}

Last edited on
Thank you both, that did it! I will keep in mind all the shortcuts to make my code slimmer and faster to turn out.

Thanks again.
Topic archived. No new replies allowed.