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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196`` ``````#include #include 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.
 ``1234567`` `````` 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.