I'm looking for help with an assignment in C++ in which change must made for an entered item cost, and the number of each monetary denomination from $20 bills down to one cent coins is to be shown (ie: Twenties - 2, tens - 0, fives - 1, ones - 0, quarters - 2, etc for the calculated change amount).
I have included code that I have already written, but can't get it to work. Everything works the way it should except the function that calculates the denominations that are to be given out. The program compiles as written and makes changes, but as I stated, doesn't properly show the denominations to be given as change. Any help would be enormously appreciated.
code is as follows:
#include <cstdlib>
#include <iostream>
using namespace std;
double chngDue(double tendered, double cost)
{
double due;
due=(tendered-cost);
return due;
}
double denomination(double chng)
{
int i =(int)(chng * 100);
if(chng >= .01)
{
int twenties = i / 2000;
i = i % 2000;
int tens = i / 1000;
i = i % 1000;
int fives = i / 500;
i = i % 500;
int ones = i / 100;
i = i % 100;
int quarters = i / 25;
i = i % 25;
int dimes = i / 10;
i = i % 10;
int nickels = i / 5;
i = i % 5;
int pennies = i / 1;
i= i % 1;
}
return i;
}
int main(int argc, char *argv[])
{
double itemCost;
double amtTend;
double change;
char keepGoing = ' ';
int getBack;
int twenties = 0;
while(keepGoing !='q')
{
cout << "Enter an item cost: \n\n";
cin >> itemCost;
cout << "\n";
cout <<"Enter an amount of money to tender in order to cover the item cost \n\n";
cin >> amtTend;
cout << "\n";
printf("You entered $%.2f", itemCost);
cout << " as an item cost.\n\n";
printf("$%.2f was tendered to cover the item cost.\n\n", amtTend);
change = chngDue(amtTend, itemCost);
printf("Your change is $%.2f \n\n", change);
getBack = (int)denomination(change);
cout <<"You will receive it in the following denominations - \n\n";
printf("Twenties - %d\n",getBack);
printf("Tens - %d\n",getBack);
printf("Fives - %d\n",getBack);
printf("Ones - %d\n",getBack);
printf("Quarters - %d\n",getBack);
printf("Dimes - %d\n",getBack);
printf("Nickels - %d\n",getBack);
printf("Pennies - %d\n",getBack);
cout << "\n\nType 'q' to quit or 'c' to continue :\n\n";
cin >> keepGoing;
}
system("PAUSE");
return EXIT_SUCCESS;
}