/**** * * This program makes change, given an amount of purchase and an amount * tendered. The output is a total amount of change, followed by change in five * denominations of money: dollars, quarters, dimes, nickels, and pennies. All * inputs and outputs are in integer cents. * * The program processes multiple transactions, until the user inputs 'q' to * quit. * * Author: Gene Fisher (gfisher@calpoly.edu) * Created: 15apr11 * Modified: 19apr11 * */ #include /** * Return the correct number of dollars in change for the given amount of * cents. */ int get_dollars(int cents) { return cents / 100; } /** * Return the correct number of quarters in change for the given amount of * cents. */ int get_quarters(int cents) { return cents % 100 / 25; } /** * Return the correct number of dimes in change for the given amount of * cents. */ int get_dimes(int cents) { return cents % 100 % 25 / 10; } /** * Return the correct number of nickels in change for the given amount of * cents. */ int get_nickels(int cents) { return cents % 100 % 25 % 10 / 5; } /** * Return the correct number of pennies in change for the given amount of * cents. */ int get_pennies(int cents) { return cents % 100 % 25 % 10 % 5; } int main() { /* * Declare program variables to hold the purchase amount, amount tendered, * and total amount of change. */ int purchase; /* Amount of purchase */ int tendered; /* Amount tendered */ int change; /* Total change due */ char quit_char = ' '; /* Loop sentinel */ /* * Loop through transactions, while the user doesn't type 'q'. Compared to * make_change_with_loop.c, there is only once place in the code where the * purchase amount is entered -- at the top of the loop. Think about * what's going on here compared to make_change_with_loop.c */ while (quit_char != 'q') { /* * Prompt the user for the amount of purchase. */ printf("Input the amount of the purchase, in cents: "); /* * Input the amount of purchase. */ scanf("%d", &purchase); /* * Prompt for the amount tendered. */ printf("Input the amount tendered, in cents: "); /* * Input the amount tendered. */ scanf("%d", &tendered); /* * Output a blank line, for nice formatting. */ printf("\n"); /* * Compute the total amount of change due, in cents. Assume that this * value is non-negative. */ change = tendered - purchase; /* * Output the total change amount, followed by a blank line. */ printf("Total change due = %d\n\n", change); /* * Compute and print the correct change for each denomination. */ printf("Change in dollars through pennies is:\n"); printf(" %d dollars\n", get_dollars(change)); printf(" %d quarters\n", get_quarters(change)); printf(" %d dimes\n", get_dimes(change)); printf(" %d nickels\n", get_nickels(change)); printf(" %d pennies\n", get_pennies(change)); /* * Ask the user whether or not to quit. Note well that the message * tells the user to input a non-whitespace character to continue. * This means that the user cannot just type the Enter key on a blank * line to continue, but must type some non-blank character. It would * take some more work to write the program to allow a single press of * the Enter key to be considered an input. This is because scanf is a * rather finicky function. */ printf("Type 'q' to quit, any other non-whitespace character to continue: "); /* * Getting the following scanf to work properly is a bit tricky. The * rule for the scanf %c conversion is that it does not automatically * skip leading whitespace. The trouble is, the newline character that * the user typed to enter the most recent numeric input is still * sitting on the input stream. So, the scanf format string needs to * have the \n before the %c, in order to tell scanf to skip the * lingering newline char in the input stream. Again, scanf is quite * the finicky little bugger. */ scanf("\n%c", &quit_char); } return 0; }