Cash Register Program

Hello, I just started programming a few days ago so it would be a big help if the explanations are simple for me to understand. So for my assignment I need to make a cash register program where you input how much the cost was, then you input how much money you paid, and then the program will spit out what your change value is and what bills you'll get.

Say the cost is $3.54 and the payment is $40.
My problem is that for the quarter, dime, nickel,and penny calculation, it says "invalid operands of type 'int' and 'double'" and I'm not seeing where the problem is.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
//Making Change ($)
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double purchase;
    double payment;
    double change;

    int twenty;
    int ten;
    int five;
    int one;
    int quarter;
    int dime;
    int nickel;
    int penny;

    int num1;
    int num2;
    int num3;
    int num4;
    int num5;
    int num6;
    int num7;

       do
        {
        cout << "Please enter the value of your purchase. (0-100)" << endl;
        cin >> purchase;
        }
            while (purchase < 0 || purchase > 100);

            do
            {
                cout << "Please enter the value of how much you paid." << endl;
                cin >> payment;
            }
                while (payment <= purchase);


        cout << "Here is your change:";
        change = payment - purchase;
        cout << fixed << setprecision(2) << change << endl;

        int newchange = (double)change;

                twenty = newchange/20;
            num1 = twenty*20;
                ten = (newchange - num1)/10;
            num2 = ten*10;
                five = (newchange - (num1+num2))/5;
            num3 = five*5;
                one = (newchange - (num1+num2+num3))/1;
            num4 = one*1;
                quarter = (newchange - (num1+num2+num3+num4))%.25;
            num5 = quarter*.25;
                dime = (newchange - (num1+num2+num3+num4+num5))%.10;
            num6 = dime*.10;
                nickel = (newchange - (num1+num2+num3+num4+num5+num6))%.05;
            num7 = nickel*.05;
                penny = (newchange - (num1+num2+num3+num4+num5+num6+num7))%.01;

        cout << "Twenties: " << twenty << endl;
        cout << "Tens: " << ten << endl;
        cout << "Fives: " << five << endl;
        cout << "Ones: " << one << endl;
        cout << "Quarters: " << quarter << endl;
        cout << "Dimes: " << dime << endl;
        cout << "Nickels: " << nickel << endl;
        cout << "Pennies: " << penny << endl;

        return 0;

}
closed account (48T7M4Gy)
Your using the % function incorrectly. It only works on integers.

What this means is you should calculate the change in cents (integers)

So if you have change of 5.23 then you calculate with an integer vale of 523 cents.

The number of quarters is 523 / 25 and the left over cents are 523 % 25, and that leftover amount is then to be broken down further. You don't need to keep adding up the various numx values.
Last edited on
Hello @Lithia,

I'm probably misunderstanding your problem but, are you trying to use -> '/'
and using (by mistake) -> '%' ?

I changed it but it's still not correct. It still outputs 0s 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
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
//Making Change ($)
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double purchase;
    double payment;
    double change;

    int twenty;
    int ten;
    int five;
    int one;
    int quarter;
    int dime;
    int nickel;
    int penny;


       do
        {
        cout << "Please enter the value of your purchase. (0-100)" << endl;
        cin >> purchase;
        }
            while (purchase < 0 || purchase > 100);

            do
            {
                cout << "Please enter the value of how much you paid." << endl;
                cin >> payment;
            }
                while (payment <= purchase);


        cout << "Here is your change:";
        change = payment - purchase;
        cout << fixed << setprecision(2) << change << endl;

        int newchange = (double)change;

            newchange = change*100;
            twenty = newchange / 2000;
            ten = newchange % 2000 / 1000;
            five = newchange % 2000 % 1000 / 500;
            one = newchang % 2000 % 1000 % 500 / 100;
            quarter = newchange % 2000 % 1000 % 500 % 100 / 25;
            dime = newchange % 2000 % 1000 % 500 % 100 % 25 / 10;
            nickel = newchange % 2000 % 1000 % 500 % 100 % 25 % 10 / 5;
            penny = newchange % 2000 % 1000 % 500 % 100 % 25 % 10 % 5;


        cout << "Twenties: " << twenty << endl;
        cout << "Tens: " << ten << endl;
        cout << "Fives: " << five << endl;
        cout << "Ones: " << one << endl;
        cout << "Quarters: " << quarter << endl;
        cout << "Dimes: " << dime << endl;
        cout << "Nickels: " << nickel << endl;
        cout << "Pennies: " << penny << endl;

        return 0;

}
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
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
69
//Making Change ($)
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double purchase;
    double payment;
    double change;
    
    int twenty;
    int ten;
    int five;
    int one;
    int quarter;
    int dime;
    int nickel;
    int penny;
    
    
    do
    {
        cout << "Please enter the value of your purchase. (0-100)" << endl;
        cin >> purchase;
    }
    while (purchase < 0 || purchase > 100);
    
    do
    {
        cout << "Please enter the value of how much you paid." << endl;
        cin >> payment;
    }
    while (payment <= purchase);
    
    
    cout << "Here is your change:";
    change = payment - purchase;
    cout << fixed << setprecision(2) << change << endl;
    
    int cents = (int)100*change;
    
    twenty = cents/2000;
    cents = cents % 2000;
    
    ten = cents/1000;
    cents = cents % 1000;
    
    five = cents/500;
    cents = cents % 500;
    
    
    /* and so on */
    
    cout << "Twenties: " << twenty << endl;
    cout << "Tens: " << ten << endl;
    cout << "Fives: " << five << endl;
    cout << "Ones: " << one << endl;
    cout << "Quarters: " << quarter << endl;
    cout << "Dimes: " << dime << endl;
    cout << "Nickels: " << nickel << endl;
    cout << "Pennies: " << penny << endl;
    
    return 0;
    
}
Please enter the value of your purchase. (0-100)
2.15
Please enter the value of how much you paid.
50
Here is your change:47.85
Twenties: 2
Tens: 0
Fives: 1
Ones: 0
Quarters: 0
Dimes: 0
Nickels: 0
Pennies: 0
Program ended with exit code: 0
This is odd. So I've been using Qt to run and compile things and the program output in Qt isn't the same as the browser's. In the browser, it shows that everything is correct. The bills and cents have the proper output in the browser shell. However, Qt shows the proper values for the bills but not the cents; the cents are all 0.


Qt output:
Please enter the value of your purchase. (0-100)
3.45
Please enter the value of how much you paid.
50
Here is your change:46.55
Twenties: 2
Tens: 0
Fives: 1
Ones: 1
Quarters: 0
Dimes: 0
Nickels: 0
Pennies: 0



Output from the browser shell:
Please enter the value of your purchase. (0-100)
3.45
Please enter the value of how much you paid.
50
Here is your change:46.55
Twenties: 2
Tens: 0
Fives: 1
Ones: 1
Quarters: 2
Dimes: 0
Nickels: 1
Pennies: 5



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
69
70
71
72
73
74
75
76
77
78
//Making Change ($)
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double purchase;
    double payment;
    double change;

    int twenty;
    int ten;
    int five;
    int one;
    int quarter;
    int dime;
    int nickel;
    int penny;

    do
    {
        cout << "Please enter the value of your purchase. (0-100)" << endl;
        cin >> purchase;
    }
    while (purchase < 0 || purchase > 100);

    do
    {
        cout << "Please enter the value of how much you paid." << endl;
        cin >> payment;
    }
    while (payment <= purchase);

    cout << "Here is your change:";
    change = payment - purchase;
    cout << fixed << setprecision(2) << change << endl;

    int cents = (int)100*change;

    twenty = cents/2000;
    cents = cents % 2000;

    ten = cents/1000;
    cents = cents % 1000;

    five = cents/500;
    cents = cents % 500;

    one = cents/100;
    cents = cents % 100;

    quarter = cents/25;
    cents = cents % 25;

    dime = cents/10;
    cents = cents % 25;

    nickel = cents/5;
    cents = cents % 10;

    penny = cents;

    cout << "Twenties: " << twenty << endl;
    cout << "Tens: " << ten << endl;
    cout << "Fives: " << five << endl;
    cout << "Ones: " << one << endl;
    cout << "Quarters: " << quarter << endl;
    cout << "Dimes: " << dime << endl;
    cout << "Nickels: " << nickel << endl;
    cout << "Pennies: " << penny << endl;

    return 0;

}
closed account (48T7M4Gy)
Please enter the value of your purchase. (0-100)
1.23
Please enter the value of how much you paid.
50.00
Here is your change:48.77
Twenties: 2
Tens: 0
Fives: 1
Ones: 3
Quarters: 3
Dimes: 0
Nickels: 0
Pennies: 2
 
Exit code: 0 (normal program termination)


This is what you get by running the shell here (gear wheel on the right of your code box above) and the answer looks =good to me.

Try clean and rebuild in Qt because the answer will be the same.
@Lithia

Lines 60 and 63, are incorrect, giving wrong answers. For the dime, you are still using mod for a quarter, instead of for 10, and for nickels, you are using mod for a dime. Correct those two, and you should get much better results.

I'm really surprised these got past @kemort
closed account (48T7M4Gy)
Thanks whitenite1. Two heads are better than one, especially when one of them is mine and it wasn't working at the time. I couldn't have picked a worse test example.
@kemort

Did this, done that, also. I miss simple things in my code too.
@kemort and @whitenite1 Thank you both for helping me out. @kemort I actually just created a new project in Qt and that seemed to solve the problem. I tried to clean and rebuild just as you said, but it didn't seem to do anything. I'll go and look up how to do it properly another time.

Here is my final code. I fixed the calculation part and added a few more statements. Also, if the bills showed a value of 0, I made it so it wouldn't output.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//Making Change ($)
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double cost;
    double payment;
    double change;

    int twenty;
    int ten;
    int five;
    int one;
    int quarter;
    int dime;
    int nickel;
    int penny;

    cout << "Please enter the cost of your purchase. (0-100)" << endl;
    cin >> cost;
    while (cost <= 0 || cost >= 100) {
        cout << "Please enter a cost value from 0 to 100." << endl;
        cin >> cost;
    }

    cout << "Please enter how much you paid for the purchase." << endl;
    cin >> payment;
    while(cost > payment) {
        cout << "Please enter a payment value higher than your cost value." << endl;
        cin >> payment;
    }

    cout << "Here is your total change: " << endl;
    change = payment - cost;
    cout << fixed << setprecision(2) << change << endl;

    int total = (int)100*change;

    twenty = total/2000;
    total = total % 2000;

    ten = total/1000;
    total = total % 1000;

    five = total/500;
    total = total % 500;

    one = total/100;
    total = total % 100;

    quarter = total/25;
    total = total % 25;

    dime = total/10;
    total = total % 10;

    nickel = total/5;
    total = total % 5;

    penny = total;

    cout << "Here is your change in dollar bills: " << endl;
    if (twenty != 0) {
        cout << "Twenties: " << twenty << endl;
    }

    if (ten != 0) {
        cout << "Tens: " << ten << endl;
    }

    if (five != 0) {
        cout << "Fives: " << five << endl;
    }

    if (one != 0) {
        cout << "Ones: " << one << endl;
    }

    if (quarter != 0) {
        cout << "Quarters: " << quarter << endl;
    }

    if (dime != 0) {
        cout << "Dimes: " << dime << endl;
    }

    if (nickel != 0) {
        cout << "Nickels: " << nickel << endl;
    }

    if (penny != 0) {
        cout << "Pennies: " << penny << endl;
    }

    return 0;

}
closed account (48T7M4Gy)
@Lithia,

I tried to clean and rebuild just as you said, but it didn't seem to do anything.
It wouldn't have done any good given my blooper in not looking at your code but relying on an incomplete test case which gave gave the correct result of zero for the wrong reasons.

Glad to see you're in business,
Cheers :)
@kemort

Well after I fixed the calculation and ran it, the output still showed 0s for the cents. I searched on Google what to do when in this scenario and one person said you could just make a new project. I made a new project, copy and pasted, and then the output was corrected.
Topic archived. No new replies allowed.