I see, that is actually a good idea. Many people have done this in their source posted previously, so I don't expect you to repeat the idea.
I wrote the code before any of you guys did, so my code didn't use that more efficient method (didn't want to cheat lol)
Here is mine, broken up into two pieces
Main.cpp
#include <stdio.h>
#include <iostream>
#include <math.h>
#include "ChangeMaker.hpp"
int main( int argc, char** argv )
{
using namespace std;
double owed = -1.0;
double tendered = -1.0;
double epsilon = 0.0050; // has to be right upto one cent (rounded)
// make sure input is within <epsilon> of 0.00 or greater than 0.00
while( true )
{
cout << "Money owed: ";
cin >> owed;
if ( !(owed > 0.0 || fabs(owed) < epsilon) )
cout << "ERROR: Illegal ammount.\n\n";
else
break;
}
// make sure tendered is within <epsilon> of owed or greater than owed
while ( true )
{
cout << "Money tendered: ";
cin >> tendered;
if ( !( tendered > owed || fabs( tendered - owed ) < epsilon) )
cout << "ERROR: Amount tendered must be >= amount owed.\n\n";
else
break;
}
ChangeHolder change;
ChangeMaker::MakeChange( tendered - owed, change );
if ( change.bill_one_hundred )
printf("100.00\t%4d\t= %9.2f\n", change.bill_one_hundred, change.bill_one_hundred * 100.0f);
if ( change.bill_fifty )
printf(" 50.00\t%4d\t= %9.2f\n", change.bill_fifty, change.bill_fifty * 50.0f);
if ( change.bill_twenty )
printf(" 20.00\t%4d\t= %9.2f\n", change.bill_twenty, change.bill_twenty * 20.0f);
if ( change.bill_ten )
printf(" 10.00\t%4d\t= %9.2f\n", change.bill_ten, change.bill_ten * 10.0f);
if ( change.bill_five )
printf(" 5.00\t%4d\t= %9.2f\n", change.bill_five, change.bill_five * 5.0f);
if ( change.bill_one )
printf(" 1.00\t%4d\t= %9.2f\n", change.bill_one, change.bill_one * 5.0f);
if ( change.coin_quarter )
printf(" 0.25\t%4d\t= %9.2f\n", change.coin_quarter, change.coin_quarter * 0.25f);
if ( change.coin_dime )
printf(" 0.10\t%4d\t= %9.2f\n", change.coin_dime, change.coin_dime * 0.10f);
if ( change.coin_nickel)
printf(" 0.05\t%4d\t= %9.2f\n", change.coin_nickel, change.coin_nickel * 0.05f);
if ( change.coin_penny )
printf(" 0.01\t%4d\t= %9.2f\n", change.coin_penny, change.coin_penny * 0.01f);
printf("change\t\t= %9.2f\n", \
change.bill_one_hundred * 100 + change.bill_fifty * 50 + change.bill_twenty * 20 + change.bill_ten * 10 + change.bill_five * 5 + change.bill_one * 1 \
+
(change.coin_quarter * 0.25) + (change.coin_dime * 0.10) + (change.coin_nickel * 0.05) + (change.coin_penny * 0.01) );
return 0;
}
ChangeMaker.hpp:
#ifndef _CHANGEMAKER_H_
#define _CHANGEMAKER_H_
#include <string.h>
#include <math.h>
struct ChangeHolder{
unsigned int bill_one_hundred;
unsigned int bill_fifty;
unsigned int bill_twenty;
unsigned int bill_ten;
unsigned int bill_five;
unsigned int bill_one;
unsigned int coin_quarter;
unsigned int coin_dime;
unsigned int coin_nickel;
unsigned int coin_penny;
};
class ChangeMaker{
public:
static const int ERROR_NEGATIVE_CHANGE;
static const int OK;
static int MakeChange( double change, ChangeHolder & changeHolder );
static void MakeChange( unsigned int dollars, unsigned int cents, ChangeHolder & changeHolder );
static void FindPartIn( unsigned int & count, unsigned int denominator, unsigned int & numerator );
};
const int ChangeMaker::ERROR_NEGATIVE_CHANGE = -1;
const int ChangeMaker::OK = 0;
int ChangeMaker::MakeChange( double change, ChangeHolder & changeHolder )
{
double epsilon = 0.0050;
unsigned int dollars;
unsigned int cents;
double dollarsDouble;
double centsDouble;
if( !( change > 0.0 || fabs( change ) < fabs ( epsilon ) ) )
return ChangeMaker::ERROR_NEGATIVE_CHANGE;
centsDouble = (100.0 * modf( change, & dollarsDouble ));
cents = (unsigned int) centsDouble;
// takes care of truncation when converting double to int
if( (centsDouble - cents) > epsilon)
cents++;
dollars = (unsigned int)(dollarsDouble);
ChangeMaker::MakeChange( dollars, cents, changeHolder );
return ChangeMaker::OK;
}
void ChangeMaker::FindPartIn( unsigned int & count, unsigned int denominator, unsigned int & numerator ){
count = numerator / denominator ;
numerator -= denominator * count;
}
void ChangeMaker::MakeChange( unsigned int dollars, unsigned int cents, ChangeHolder & changeHolder )
{
ChangeMaker::FindPartIn( changeHolder.bill_one_hundred, 100, dollars );
ChangeMaker::FindPartIn( changeHolder.bill_fifty, 50, dollars );
ChangeMaker::FindPartIn( changeHolder.bill_twenty, 20, dollars );
ChangeMaker::FindPartIn( changeHolder.bill_ten, 10, dollars );
ChangeMaker::FindPartIn( changeHolder.bill_five, 5, dollars );
ChangeMaker::FindPartIn( changeHolder.bill_one, 1, dollars );
ChangeMaker::FindPartIn( changeHolder.coin_quarter, 25, cents );
ChangeMaker::FindPartIn( changeHolder.coin_dime, 10, cents );
ChangeMaker::FindPartIn( changeHolder.coin_nickel, 5, cents );
changeHolder.coin_penny = cents;
}
#endif
output:
$ a.out
Money owed: 87.61
Money tendered: 87.604
ERROR: Amount tendered must be >= amount owed.
Money tendered: 100
10.00 1 = 10.00
1.00 2 = 10.00
0.25 1 = 0.25
0.10 1 = 0.10
0.01 4 = 0.04
change = 12.39
$