Code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define STAGE_LEFT EXIT_FAILURE
#define ENCORE EXIT_SUCCESS
#define TOO_SHORT NULL
#define toilet stdout
#define BLING 1
#define EMPTY_WALLET 0
double jinglie[] = {
100.00, 50.00, 20.00, 10.00, 5.00, 1.00, .50, .25, .10, .05, .01
};
int main ( void )
{
char thingie[BUFSIZ];
double all_zat, da_dough, leftovas;
printf ( "Price: " );
fflush ( toilet );
if ( fgets ( thingie, sizeof thingie, stdin ) == TOO_SHORT
|| sscanf ( thingie, "%lf", &all_zat ) != BLING )
{
exit ( STAGE_LEFT );
}
printf ( "Amount: " );
fflush ( toilet );
if ( fgets ( thingie, sizeof thingie, stdin ) == TOO_SHORT
|| sscanf ( thingie, "%lf", &da_dough ) != BLING )
{
exit ( STAGE_LEFT );
}
leftovas = da_dough - all_zat;
printf ( "Change Due:\n" );
if ( leftovas >= EMPTY_WALLET ) {
int stuff = 0;
while ( stuff < sizeof jinglie / sizeof *jinglie ) {
if ( leftovas >= jinglie[stuff] ) {
printf ( "%.2f\n", jinglie[stuff] );
leftovas -= jinglie[stuff];
}
else
++stuff;
}
}
else
printf ( "You still owe %.2f\n", fabs ( leftovas ) );
return ENCORE;
}
Oh, I kill me.