/* This program produces a customer invoice for a store it demonstrates all material in "Intro to Programming Using C" Chapter 7 Author: Evan Weaver Last Modified: 18-Nov-1996 */ #include #include #define DESCSIZE 41 /* size of item description arrays */ #define MAX 20 /* Maximum number of items on invoice */ #define TAXRATE 15.0 /* Tax Rate in percent */ /* Application specific functions */ int get_data(char desc[][DESCSIZE], int qty[], double prc[], int max); void invoice (char desc[][DESCSIZE], int qty[], double prc[], int max); /* General purpose functions */ void clear_input(void); double get_a_double(void); void get_a_line(char line[], int max); int get_an_int(void); main() { char descriptions[MAX][DESCSIZE]; int quantities[MAX], itemcount; double prices[MAX]; itemcount = get_data(descriptions, quantities, prices, MAX); if (itemcount > 0) invoice (descriptions, quantities, prices, itemcount); } /* end of main program */ int get_data(char desc[][DESCSIZE], int qty[], double prc[], int max) { int i = 0; printf("Enter item description (or quit to stop): "); get_a_line(desc[i], DESCSIZE - 1); while (i < max && strcmp(desc[i], "quit") != 0) { printf ("Enter quantity: "); qty[i] = get_an_int(); printf ("Enter unit price: "); prc[i] = get_a_double(); i++; if ( i < max) { printf ("Enter item description (or quit to stop): "); get_a_line(desc[i], DESCSIZE - 1); } } return i; } /* End of get_data() function */ /* invoice() Produces an invoice showing description, quantity, price and extended price for "max" items. Tax is added at the prevailing tax rate (TAXRATE) */ void invoice(char desc[][DESCSIZE], int qty[], double prc[], int max) { int i; double subtotal, total = 0, tax; /* display column headings */ printf ("\n\n <<< Customer Invoice>>>\n\n"); printf ("%-30s%8s%10s%10s\n", "Description", "Quan", "Cost", "Total"); printf ("%-30s%8s%10s%10s\n", "-----------", "----", "----", "-----"); /* Show details for each item, accumulating total */ for (i=0; i < max; i++) { subtotal = qty[i] * prc[i]; total += subtotal; printf ("%-30s%8d%10.2lf%10.2lf\n", desc[i], qty[i], prc[i], subtotal); } /* Display totals and taxes */ printf ("%58s\n", "--------"); printf ("%-30s%28.2lf\n", "Total", total); tax = total * TAXRATE / 100.0; printf ("Tax %5.2lf percent%41.2lf\n", TAXRATE, tax); printf ("%58s\n", "--------"); printf ("%-30s%28.2lf\n", "Total with tax", total + tax); printf ("%58s\n", "========"); } /* End of invoice() function */ /* clear_input() Clears out any input data remaining unprocessed. This function simply reads characters until it encounters the new-line that terminates the input line */ void clear_input(void) { while (getchar() != '\n') ; /* intentionally empty statement! */ } /* End of clear_input() function */ /* get_an_int() Gets an int value from the user and clears out the rest of the input line. The value entered is returned. If no int data is entered, the user is given an error message and a chance to re-enter. */ int get_an_int(void) { int n; while (0 == scanf("%d", &n)) { clear_input(); /* throw away bad data */ printf("Error! Please enter an integer: "); } clear_input(); /*throw away any extra data entered */ return n; } /* End of get_an_int() function */ /* get_a_double() Similar to get_an_int() function, except get_a_double() function gets a double rather then an int. */ double get_a_double(void) { double n; while (0 == scanf("%lf", &n)) { clear_input(); /* throw away bad data */ printf("Error! Please enter an integer: "); } clear_input(); /*throw away any extra data entered */ return n; } /* End of get_a_double() function */ /* get_a_line() Gets a line of input from the user. Up to "max" characters are placed into "line" as a string, and any remainder, including the trailing new-line, is discarded. */ void get_a_line(char line[], int max) { int n=0; char c; while ('\n' != (c = getchar())) if (n < max) line[n++] = c; line[n] = '\0'; } /* End of get_a_line() function */