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;
}

There's nothing technically wrong with using printf, but there are ways to accomplish the same thing using cout.
In particular, look at the examples:
http://www.cplusplus.com/reference/iostream/manipulators/fixed/
http://www.cplusplus.com/reference/iostream/manipulators/setprecision/

It looks to me like your assignment is saying all you need to do is output the number of each of the bills and coins. I would do just that:
cout <<"Twenties - "<<i/2000<<endl; instead of line 18.

If you think your instructor really wants all of those variables coming back to main, then pass them in by reference (with the &)
void denomination(double change, int & twenties, etc.)

If you trace your function through by hand, you'll see that all you'd end up returning each time was 0 (since your leftovers after the pennies will always be 0). Also, if you want your function to return an int, don't make it double and then cast that to an int back in main, just return the int!

Watch the pennies too, there's a round off error happening when you're casting change*100 to an int.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.