chashier return

So i am trying to make a cashier program where the user enters the amount of money entering in a cash register and wanted to return the amount of change to the customer but can't seem to find the proper way to do it. I know I'm doing it wrong but can someone help me out, please. For example, if the customer gives the machine 220 dollars I wanted it to give 100 100 and 20 dollars back similar to a real cash register thank you in advance.


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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  


#include <iostream>

using namespace std;

double amount_due(double);

int main()
{
    
    double amount; 
    
    
    cout << "enter the dollar amount you enter in the machine " << endl;
    cin >> amount;
    
    amount_due(amount);
    

    return 0;
}

double amount(double amount){
    
    double return_amount = amount;
    
    
    int dollar[] = {1, 5, 10, 20, 50, 100};
    double change[] = {.01, .05, .10, .25};
    
    // if the amount is empty or negative its invalid
    if(amount == 0.0 || amount < 0.0){
        return 0.0;
    }
    
    // if they enter only cents 
    if(amount == .01 || amount == .05 || amount == .10 || amount == .25){
        return amount;
    }
    
    else{
        
        while (amount > 0.0){
            
            // if dollar or less return only change
            if(amount < 1.01){
                if(amount == 0.1){
                    amount_due = amount /0.1;
                    amount_due++;
                }
                
                if(amount == 0.5){
                    amount_due = amount / 0.5;
                    amount_due++;

                }
                
                if(amount == 0.10){
                    amount_due = amount / 0.10;
                    amount_due++;
                }
                
                if(amount == 0.25){
                    amount_due = amount / 0.25;
                    amount_due++;
                }
            
            }
            
            // if they enter bigger bills 
            if(amount > 1.00){
                if(amount == 1.00){
                    amount_due = amount / 1.00;
                    amount_due++;
                }
                
                if(amount == 5.00){
                    amount_due = amount / 5.00;
                    amount_due++;
                }
                
                if(amount == 10.00){
                    amount_due = amount / 10.00;
                    amount_due++;
                }
                
                if(amount == 20.00){
                    amount_due = amount / 20.00;
                    amount_due++;
                }
                
                if(amount == 50.00){
                    amount_due = amount / 50.00;
                    amount_due++;
                }
                
                if(amount == 100.00){
                    amount_due = amount / 100.00;
                    amount_due++;
                }
             
                
            }
            
            
            
        }
        
        return amount_due;
    }
    
}




Last edited on
what if owed a dollar flat, 1.00? look at line 73 again.

this seems very complex.
how about this:
1
2
3
4
5
6
7
8
9
10
11
12
13
double amounts[] = {100.0, 50.0, 20,10.0, 5.0, 1.0, 0.5, 0.25,0.1, 0.05, 0.01}; //did you leave off 50 cent pieces on purpose? 

double value = 123.45;
int index = 0;
while (value)
{
     if(value >= amounts[index];
           {
              value -= amounts[index]; 
               //dispense amounts[index] -- eg hand them a single $100 bill in this case. 
           }
        else index++;
}

this will work most of the time for small amounts. because you chose to use doubles, there may occasionally be a round off error here, breaking the code or causing various issues.
i highly recommend using long long instead and letting 1 == a penny instead. that ensures everything is exact. If required to enter and print as doubles, convert and do the work as long long and show the user floating point values.
Last edited on
i highly recommend using long long instead and letting 1 == a penny instead

Good advice.

Working with integers will making giving back change a lot easier - you'll be able to more easily use % (remainder). If someone gives you $220, then the program will take it in as 22,000. You'll then send this number down a "ramp" of all the possible change you can give, starting from largest to smallest:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    int cash = 22000;

   if(cash>10000) //if $220 is bigger than $100
  {
       int temp = cash/10000;
       cash %= 10000; //Remainder is 2000, which is $20
       std::cout << "You got: " << temp << " $100 bills\n";
  }

  //.... etc
}
Topic archived. No new replies allowed.