HELP! Struct that stores coins and calculates change

I need help in writing the function that makes change for an input number. Input a bunch of quarters, dimes, nickels, pennies, then in MAKE_CHANGE the amount per say (184) is input. So for 184cents, runs through quarters first using the quarters we have in the coinbox, and then goes to the next currency for the remaining 9cents(184/25=7 184%25=9).

I may have worded it super weirdly, I apologize. I understand what needs to happen, im just having trouble writing it




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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <cstdlib>
#include <iostream>

using namespace std;

struct coinbox
{
       int quarters;
       int dimes;
       int nickels;
       int pennies;
};

void initialize (coinbox& c);
void add_quarters (coinbox& c, int quarters);
void add_dimes (coinbox& c , int dimes);
void add_nickels (coinbox& c, int nickels);
void add_pennies (coinbox& c, int pennies);
void make_change (coinbox& c, int amount);
void show (coinbox c);



int main(int argc, char *argv[])
{
   
    
    char input;
    coinbox  c,q,d,n,p,amount;
    int coin;
    coin=0;
  
    initialize(c);
    
     bool coins = true;
     while (coins==true)
     {
    
    cout << "Enter s (show coinbox), q (add quarters), d (add dimes)\n n (add nickels), p (enter pennies), c (make change), e(end): ";
    cin >> input;
    
    
    
    if (input=='s')
        {
        show (c);
        }
        
    if (input=='q')
       {
       add_quarters (c, q.quarters);
       }
    
    if (input=='d')
       {
       add_dimes (c, d.dimes);
       }
       
    if (input=='n')
       {
       add_nickels (c, n.nickels);
       }
       
    if (input=='p')
       {
       add_pennies (c, p.pennies);
       }
    
    if (input=='c')
       {
       make_change (c, coin);
       }
    
    
       
    
    if(input=='e')
    {
     coins=false;
    cout << endl;
    cout << "\nThanks for chaning things up!";
    cout << endl;
    cout << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
   
}

}

void initialize (coinbox& c)
{
     c.dimes=0;
     c.quarters=0;
     c.nickels=0;
     c.pennies=0;
     
}

void add_quarters (coinbox& c, int quarters)
{
     int newQuarters;
     cout << "Enter the number of quarters to add: ";
     cin >> newQuarters;
     c.quarters+=newQuarters;
     cout << endl;
     
     }
     
     
void add_dimes (coinbox& c , int dimes)
{
     int newDimes;
     cout << "Enter the number of dimes to add: ";
     cin >> newDimes;
     c.dimes+=newDimes;
     cout << endl;
     
     
     }
     
     
void add_nickels (coinbox& c, int nickels)
{
     int newNickels;
     cout << "Enter the number of nickels to add: ";
     cin >> newNickels;
     c.nickels+=newNickels;
     cout << endl;
     
     
     }
     
     
void add_pennies (coinbox& c, int pennies)
{
     int newPennies;
     cout << "Enter the number of pennies to add: ";
     cin >> newPennies;
     c.pennies+=newPennies;
     cout << endl;
     
     }
     
     
void make_change (coinbox& c, int amount)
{
     int change;
     change=0;
     int quarter, dime, nickel, penny;
     quarter=25;
     dime=10;
     nickel=5;
     penny=1;
     
     cout << "Enter the amount to make change for: ";
     cin >> amount;
     cout << "\n\n";
     
     
     
     
     
     
     
     
    
     
     
     
     
     
     cout << "Change made: "<< endl;
     cout << "   Quarters: "<< c.quarters << endl;
     cout << "   Dimes: "<< c.dimes << endl;
     cout << "   Nickels: " << c.nickels << endl;
     cout << "   Pennies: " << c.pennies << endl;
     cout << endl;
     cout << endl;
     
     
     }


void show (coinbox c)
{
     cout << "\nQuarters: "<< c.quarters;
     cout << "\nDimes: "<< c.dimes;
     cout << "\nNickels: " << c.nickels;
     cout << "\nPennies: " << c.pennies;
     cout << endl;
     cout << endl;
     
     }
Last edited on
I see a few problems.

1) You have a bunch of variables declared as type coinbox at line 29. Conceptually you have one coinbox. And certainly amount should not be a coinbox.

2) You're passing uninitialized variabled to your various add functions (q.quarters, d.dimes, etc), yet you never use those values in the respective add functions.

3) Line 159. You're trying to cin to an object of type coinbox. cin doesn't know how to do that. amount should be a simple int.

As for how to calculate the change required, you need a few loops. Starting with quarters. Let's declare a object of type coinbox to keep track of the change.
1
2
3
4
5
6
7
  coinbox  change;
  initialize (change);  // initialize the change struct 
  while (amount >= 25 && c.quarters > 0)
  { change.quarters++;         // Increment number of quarters to dispense
     c.quarters--;                    // Decrement number of quarters remaining
    amount -= 25;
  }

I'm sure you can extend that to deal with dimes, nickels, pennies.

Note. We can replace lines 176-183 with the following:
 
  show (change);

Topic archived. No new replies allowed.