#include #include const double s_cost = 5.00; // dollars const double m_cost = 7.00; const double l_cost = 9.00; const double t_cost = .75; const double tax = .10; // percent void take_order(int pizzas); int get_num_pizzas(); char get_pizza_size(int count); int get_num_toppings(int count); int check_order(int small, int medium, int large, int toppings); void print_receipt(int small, int medium, int large, int toppings); double calc_tax(double total); /* * Take a series of orders. * For each order, determine number of pizzas and call take_order to handle order. * Repeat until user enters zero for number of pizzas. */ void main() { int orders, pizzas; cout << "Welcome to Pizza Yurt!" << endl; orders = 1; do { cout << "This is order " << orders << endl; pizzas = get_num_pizzas(); // check if (pizzas == 0) break; take_order(pizzas); orders++; } while (1); cout << "Thank you for working at Pizza Yurt!" << endl; } /* * Take a single order. For each order, determine number of pizzas. * For each pizza, determine size and number of toppings. * Call check_order to print a summary of the order and verify. * Repeat until order is correct, then print receipt. */ void take_order(int pizzas) { int small, medium, large; int toppings; char size; int count; // keep track of pizza number int done = 0; // flag to test for correct order while ( ! done ) // until order is correct { small = medium = large = toppings = 0; count = 1; do // for each pizza { size = get_pizza_size(count); if (size == 's') small++; else if (size == 'm') medium++; else // size must be large large++; // get toppings toppings = toppings + get_num_toppings(count); // increase number of pizzas entered for this order count = count + 1; } while (count <= pizzas); // check_order prints summary and returns "true" only if order is OK done = check_order(small, medium, large, toppings); } // print_recept calculates the total with tax print_receipt(small, medium, large, toppings); } /* * Check the user input to obtain a non-negative number of pizzas for an order. * Returns 0 if user wants to quit. * Does NOT check for non-numerical entries. */ int get_num_pizzas() { int pizzas; do { cout << "How many pizzas would you like ? "; cin >> pizzas; if (pizzas < 0) cout << "Please enter a positive number, or 0 to quit." << endl; } while (pizzas < 0); return pizzas; } /* * Check user input to obtain a pizza size of 's', 'm' or 'l' * Uses the ctype.h function tolower to take care of uppercase input * Only returns one of the 3 legal chars. */ char get_pizza_size(int number) { char size; do { cout << "What size pizza would you like (S, M, L) for pizza "; cout << number; cout << " ? "; cin >> size; size = tolower(size); if (size != 's' && size !='m' && size !='l') cout << "You may choose from S, M, or L." << endl; else return size; } while (1); } /* * Check the user input to obtain a non-negative number of toppings for a pizza. * Returns 0 if user wants no toppings. * Does NOT check for non-numerical entries. */ int get_num_toppings(int number) { int toppings; do { cout << "How many toppings would you like on pizza "; cout << number; cout << " ? "; cin >> toppings; if (toppings < 0) cout << "Please enter a positive number, or 0 for none." << endl; } while (toppings < 0); return toppings; } /* * Prints number of small, medium, and large pizzas ordered. * Prints total number of toppings ordered. */ int check_order(int small, int medium, int large, int toppings) { char ok; cout << "Your order is:" << endl; if (small > 0) cout << small << " small pizzas" << endl; if (medium > 0) cout << medium << " medium pizzas" << endl; if (large > 0) cout << large << " large pizzas" << endl; if (toppings == 0) cout << "no toppings" << endl; if (toppings > 0) cout << toppings << " toppings" << endl; do { cout << "Is this correct ? "; cin >> ok; if (ok == 'y' || ok =='Y') return 1; if (ok == 'n' || ok =='N') return 0; } while (1); } /* * Prints number of small, medium, and large pizzas ordered. * Prints total number of toppings ordered. * If quantity of a given item is 0, nothing is printed for that item. * For each item, calculates and displays subtotal and total order cost. * Uses calc_tax() to display tax and total cost. */ void print_receipt(int small, int medium, int large, int toppings) { double total = 0; double sub, tax; // format cout for "money style" floating point numbers cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); if (small > 0) { cout << small << " small pizzas $ "; sub = small * s_cost; cout << sub << endl; total = total + sub; } if (medium > 0) { cout << medium << " medium pizzas $ "; sub = medium * m_cost; cout << sub << endl; total = total + sub; } if (large > 0) { cout << large << " large pizzas $ "; sub = large * l_cost; cout << sub << endl; total = total + sub; } if (toppings > 0) { cout << toppings << " toppings $ "; sub = toppings * t_cost; cout << sub << endl; total = total + sub; } cout << endl << " subtotal $ " << total; tax = calc_tax(total); cout << endl << " tax $ " << tax << endl; total = total + tax; cout << endl << " TOTAL $ " << total << endl; // could return total here... } /* * Calculates tax using global constant tax, returns tax only. */ double calc_tax(double total) { return (total * tax); } /* SAMPLE RUN Welcome to Pizza Yurt! This is order 1 How many pizzas would you like ? 3 What size pizza would you like (S, M, L) for pizza 1 ? s How many toppings would you like on pizza 1 ? 1 What size pizza would you like (S, M, L) for pizza 2 ? m How many toppings would you like on pizza 2 ? 2 What size pizza would you like (S, M, L) for pizza 3 ? l How many toppings would you like on pizza 3 ? 3 Your order is: 1 small pizzas 1 medium pizzas 1 large pizzas 6 toppings Is this correct ? y 1 small pizzas $ 5.00 1 medium pizzas $ 7.00 1 large pizzas $ 9.00 6 toppings $ 4.50 subtotal $ 25.50 tax $ 2.55 TOTAL $ 28.05 This is order 2 How many pizzas would you like ? 1 What size pizza would you like (S, M, L) for pizza 1 ? s How many toppings would you like on pizza 1 ? 3 Your order is: 1 small pizzas 3 toppings Is this correct ? y 1 small pizzas $ 5.00 3 toppings $ 2.25 subtotal $ 7.25 tax $ 0.73 TOTAL $ 7.97 This is order 3 How many pizzas would you like ? 0 Thank you for working at Pizza Yurt! */