correct change

Basically, I'm trying to write a basic cash register program where it tells the user the correct amount of change as well as the most efficient way of handing out change i.e. if the purchase amount was $26.17 and the customer paid $30 then, the program should calculate that the change should be returned as $3 and 3 quarters and 0 dimes and 1 nickel and 3 pennies. I had it to where it would correctly show the dollars and quarters but not the rest of the change. now it only shows the whole change in the dollar column. How do I get it to show the correct change in each section?

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

using namespace std;

const double DOLLAR_AMOUNT = 1.00;
const double QUARTER_AMOUNT = 0.25;
const double DIME_AMOUNT = 0.10;
const double NICKEL_AMOUNT = 0.05;
const double CENT_AMOUNT = 0.01;
double price;
double amtPaid;

int main()

{
    cout<<"Welcome to Tom's Hardware \n"<<endl;

    cout<<"please enter price"<<endl;
    cin>>price;

    cout<<"please enter amount you are paying with"<<endl;
    cin>>amtPaid;

    double change = amtPaid - price; //make a double

    double dollars = change / DOLLAR_AMOUNT;
    change = change - dollars;

    double quarters  =  change / QUARTER_AMOUNT;
    change = change - quarters;

    double dimes = change / DIME_AMOUNT;
    change = change - dimes;

    double nickels = change / NICKEL_AMOUNT;
    change = change - nickels;

    double cents = change / CENT_AMOUNT;

    cout<<"Your change is "<<endl;

    cout<<"dollars = "<<dollars<<endl;

    cout<<"quarters =  "<<quarters<<endl;

    cout<<"dimes =  "<<dimes<<endl;

    cout<<"nickels =  "<<nickels<<endl;

    cout<<"cents =  "<<cents<<endl;

    return 0;
}
First of all, do use integers. Integer math and floating point math are different on CPU and this particular task is all about integer math.

Lets say you have 80 cents of change.
On line 29 you could compute 80/25 and get 3.
Three quarters sounds right, does it not?

Then you do line 30: change = 80 - 3;
Should you really have 77 cents of change left after getting three quarters?

Hint: integer math has modulo operator. The %
I am not fully understanding. when I switch to integer my program crashed
Do the whole thing in pennies.
1
2
DOLLAR_AMOUNT = 100;
QUARTER_AMOUNT = 25;

and so on.

Topic archived. No new replies allowed.