Code:
#include <iostream>
using namespace std;
int makeChange(int& fifties, int& twenties, int& tens, int& fives, int& ones, int& quarters, int& dimes, int& nickels, int& pennies, double& change);
int printChange(int& fifties, int& twenties, int& tens, int& fives, int& ones, int& quarters, int& dimes, int& nickels, int& pennies, double& originalChange);
int main(void) {
int quit, fifties, twenties, tens, fives, ones, quarters, dimes, nickels, pennies;
double due, tendered, change, originalChange;
quit = 0;
cout.setf(ios::fixed | ios::showpoint);
cout.precision(2);
cout << endl << "Welcome to the change maker!" << endl
<< "This program will accepts the amount due and the amount tendered from the user \nand computes the change due." << endl;
//start the menu
while(quit != 2) {
cout << endl << "Please choose from the following options:" << endl
<< "1) Compute change." << endl
<< "2) Quit." << endl;
cin >> quit;
//if the user selects compute change
if(quit == 1) {
//init all values back to 0 each time a new transaction begins
fifties = twenties = tens = fives = ones = quarters = dimes = nickels = pennies = 0;
due = tendered = change = originalChange = 0.00;
//prompt the user for the amount due and validate to make sure its greater than 0
cout << "Please enter the amount due:" << endl;
cin >> due;
if(due <= 0.00) {
cout << "Error: The amount due must be greater than 0.00" << endl;
}
//prompt the user for the amount tendered and validate to make sure its greater than 0
cout << "Please enter the amount tendered:" << endl;
cin >> tendered;
if(tendered <= 0.00) {
cout << "Error: The amount tendered must be more than 0.00" << endl;
}
//find the change due by subtracting due from tendered. Validate the change to make sure its greater than 0
change = tendered - due;
if(change == 0.00) {
cout << "Change due is 0.00. Nothing to compute." << endl;
} else if(change < 0.00) {
cout << "Error: The amount tendered must be more than the amount due." << endl;
} else {
//if change is due
//store a copy of change due for the final print
originalChange = change;
//run the makechange function passing the denominations and change due by reference
makeChange(fifties, twenties, tens, fives, ones, quarters, dimes, nickels, pennies, change);
/*some leftover code I was using to decide if a problem was in makeChange or printChange
cout << fifties << endl
<< twenties << endl
<< tens << endl
<< fives << endl
<< ones << endl
<< quarters << endl
<< dimes << endl
<< nickels << endl
<< pennies << endl;*/
//run the printChange function passing the denominations and the originalChange by reference
printChange(fifties, twenties, tens, fives, ones, quarters, dimes, nickels, pennies, originalChange);
}
//validate the menu choice to make sure a 1 or a 2 was selected
} else if((quit != 1) && (quit != 2)) {
cout << "Error: You have entered an invalid menu option." << endl;
}
}
return 0;
}
//the makeChange function is basically just a loop that tests the ammount of change due to see what the greatest
//denomination is that can be subtracted from it. When it finds that denomination it subtracts the value of that
//denomination and incriments the denominations counter by 1. Then loops again. It continues looping until the
//change due is 0.00.
int makeChange(int& fifties, int& twenties, int& tens, int& fives, int& ones, int& quarters, int& dimes, int& nickels, int& pennies, double& change) {
while(change > 0.00) {
if(change >= 50.00) {
change -= 50.00;
fifties++;
} else if(change >= 20.00) {
change -= 20.00;
twenties++;
} else if(change >= 10.00) {
change -= 10.00;
tens++;
} else if(change >= 5.00) {
change -= 5.00;
fives++;
} else if(change >= 1.00) {
change -= 1.00;
ones++;
} else if(change >= .25) {
change -= .25;
quarters++;
} else if(change >= .10) {
change -= .10;
dimes++;
} else if(change >= .05) {
change -= .05;
nickels++;
} else if(change >= .01) {
change -= .01;
pennies++;
}
}
return 0;
}
//the printChange function takes the counters for each denomination and the original change as arguments.
//It tests each counter to see if that counters value is greater than 0. If it is greater than 0 it prints
//the name of the denomination and the value of the counter.
int printChange(int& fifties, int& twenties, int& tens, int& fives, int& ones, int& quarters, int& dimes, int& nickels, int& pennies, double& originalChange){
cout << endl << "The change due is: " << originalChange << endl
<< "Please make this change using the following: " << endl;
if(fifties != 0) {
cout << fifties << " fifties" << endl;
}
if(twenties != 0) {
cout << twenties << " twenties" << endl;
}
if(tens != 0) {
cout << tens << " tens" << endl;
}
if(fives != 0) {
cout << fives << " fives" << endl;
}
if(ones != 0) {
cout << ones << " ones" << endl;
}
if(quarters != 0) {
cout << quarters << " quarters" << endl;
}
if(dimes != 0) {
cout << dimes << " dimes" << endl;
}
if(nickels != 0) {
cout << nickels << " nickels" << endl;
}
if(pennies != 0) {
cout << pennies << " pennies" << endl;
}
return 0;
}