Please help, this progam not calculate pennies correctly at all

he is the problem , I need to calculate a program that give change due, and show in How many Dollars, Quarters , dimes , nickles and pennsies. so far working good, but not giving pennies correctly. please check my code

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

int main(int argc, const char * argv[])
{
// Declartion of Variables
    
    //Given value of each money to the system
    /*all the given money types are double and how many money will be int*/
    
    double dollar = 1.00;
    int dollars = 0;
    double quarter = 0.25;
    int quarters = 0;
    double dime = 0.10;
    int dimes = 0;
    double nickle = 0.05;
    int nickles = 0;
    double penny = 0.01;
    int pennies = 0;
    
    float amtpaid ;
    float amtdue ;
    float changeamt;
    float change;
    
    cout<<"Sub Total:$ ";
    cin>>amtdue;
    cout<<"Amout Paid:$ ";
    cin>>amtpaid;
    
    if (amtdue>amtpaid)
    
    {
        cout<<"Amount paid less than amount owed!"<<endl;
        cout<<"you still owe $ "<<amtdue - amtpaid <<endl;
    }
    else
    {
        changeamt= (amtpaid-amtdue);
        change = changeamt;
        
        if (changeamt >= dollar)
        
        {
            dollars = changeamt/dollar;
            changeamt = changeamt - dollars;
        }
        
        if (changeamt>= quarters)
        {
            quarters = changeamt /quarter;
            changeamt = changeamt - (quarter * quarters);
        }
        if (changeamt >= dime)
        {
            dimes = changeamt /dime;
            changeamt = changeamt -(dime * dimes);
        }
        if (changeamt >= nickle)
        {
            nickles = changeamt / nickle;
            changeamt = changeamt - (nickle * nickles);
        }
        if (changeamt >= penny)
        {
            pennies = changeamt / penny;
            changeamt = changeamt -(penny * pennies);
        }
        
        //output
        
        cout<<"Change Due  :$ "<<change <<endl<<endl;
        cout<<"Dollars: "<<dollars<<endl;
        cout<<"Quarters: "<<quarters<<endl;
        cout<<"Dimes: "<<dimes<<endl;
        cout<<"Nickles: "<<nickles<<endl;
        cout<<"Pennies: "<<pennies<<endl;
        
    }
    
    cin.get();
    /*I am using Xcode on Mac OS therefore i cannot use ~ system("pause") ~ so i have to use this cin.get(); instead.*/
    
    
    
    return 0;
}



here is the latest out put came:
Sub Total:$ 25.34
Amout Paid:$ 30
Change Due :$ 4.66

Dollars: 4
Quarters: 2
Dimes: 1
Nickles: 1
Pennies: 0

This should give 1 pennies but it wont show.
I using xcode program !!
Hi @jake

The problem here is the float point number. Since the world of computer is based on digital signal, the float number isn't 100% accurate as in the math.

To see this problem you can run your program and input 9.99 as sub, 10 as amount paid. Does the due number seems confusing? It's 0.0100002 on my computer. You can google that for more information.

To solve this problem, you can add EPSILON to avoid the residual, or you can use another way to achieve this, which is also my favourite way. Use the pennies as an example, replace the if code by something like:

1
2
3
4
5
	while (changeamt>=penny)
	{
	    pennies++;
	    changeamt-=penny;
	}


You can do the same to the others. As you can see this piece of code contains no division, which is a painful operation for float number. The residual problem is sovled in the while's condition "changeamt >=penny".

I'm not sure if this will work 100% accurate, but it seems elegant, so should be working in least 90% of the situations.
Have you tried to debug this or to display intermediate values?

Keep in mind that doubles are approximations.
After the calculation at line 40, changeamt = 4.6599998, not 4.66



@Yueeng (43) , i tried what you said, and it wont work, it gives complete wrong values. how ever i did in another way and it gives me all correct values. But in the Change Due it show me so many decimals. any help for 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




#include <iostream>
using namespace std;

// Variables double and integer.
double due, paid, difference;
int change, dollars, quarter, nickle, dime, penny;
int main (void)
{
    // Input
    cout<<"Enter the amount due: $ ";
    cin>>due;
    cout<<"Enter the amount paid: $ ";
    cin>>paid;
    
    cout<<"Sub Total: $ "<<due<<endl;
    
    if (paid > due)
    {
        // The amount difference calculate new variables.
        difference = paid - due;
        difference = difference + 0.00001;
        cout<<"Change Due:$ "<<difference<<endl;
        difference = difference * 100;
        change = (int) (difference);
        
        //calculation of money due, processing
        dollars = change / 100;
        change = change % 100;
        quarter = change / 25;
        change = change % 25;
        dime = change / 10;
        change = change % 10;
        nickle = change / 5;
        change = change % 5;
        penny = change;
        
        // Output
        cout<<"Dollars; "<<dollars<<endl;
        cout<<"Quarters: "<<quarter<<endl;
        cout<<"Dimes: "<<dime<<endl;
        cout<<"Nickles: "<<nickle<<endl;
        cout<<"Pennies: "<<penny<<endl;
    }
    
        return 0;
}


Here is the out come:

Enter the amount due: $ 199.98
Enter the amount paid: $ 200
Sub Total: $ 199.98
Change Due:$ 0.02001
Dollars; 0
Quarters: 0
Dimes: 0
Nickles: 0
Pennies: 2
Topic archived. No new replies allowed.