Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* p626n4
- 4. Write a program to help a local restaurant automate its breakfast billing system. The program should do the following:
- a. Show the customer the different breakfast items offered by the restaurant.
- b. Allow the customer to select more than one item from the menu.
- c. Calculate and print the bill.
- Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right of the item):
- Plain Egg $1.45
- Bacon and Egg $2.45
- Muffin $0.99
- French Toast $1.99
- Fruit Basket $2.49
- Cereal $0.69
- Coffee $0.50
- Tea $0.75
- Use an array, menuList, of the struct menuItemType, as defined in Programming Exercise 3. (Define a struct, menuItemType, with two components: menuItem of type string and menuPrice of type double.) Your program must contain at least the following functions:
- • Function getData: This function loads the data into the array menuList.
- • Function showMenu: This function shows the different items offered by the restaurant and tells the user how to select the items.
- • Function printCheck: This function calculates and prints the check. (Note that the billing amount should include a 5% tax.)
- A sample output is:
- Welcome to Johnny's Restaurant
- Bacon and Egg $2.45
- Muffin $0.99
- Coffee $0.50
- Tax $0.20
- Amount Due $4.14
- Format your output with two decimal places. The name of each item in the output must be left justified. You may assume that the user selects only one item of a particular type.
- */
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- using namespace std;
- // Function prototypes.
- void getData();
- void showMenu();
- void printCheck();
- // Let's use a struct!
- struct menuItemType
- {
- string menuItem;
- double menuPrice;
- };
- struct requestType
- {
- int menuItem;
- int quantity;
- };
- // For lack of a lazier method of sharing an array between functions, let's make these global vars.
- // Is this bad practice? Of course. I'm supposed to pass things to functions.
- menuItemType menuList[7];
- requestType request[7];
- int main()
- {
- cout << "Welcome to $RESTAURANT!" << endl;
- showMenu();
- cout << "Order by typing in the item number followed by the quantity of the item." << endl;
- cout << "Ordering multiple items? Follow the quantity with a semicolon, then input another item + quantity." << endl;
- cout << "Example input: 4 1; 3 1; 7 2" << endl;
- for (int i = 0; i < 8; i++)
- {
- cin >> request[i].menuItem;
- cin >> request[i].quantity;
- char x = '0';
- cin.get(x);
- if (x != ';')
- i = 7;
- x = '0';
- }
- printCheck();
- return 0;
- }
- void getData()
- {
- // Hardcoded values, for testing purposes.
- // (also wtf these prices are low, coffee usually costs $1.50 these days)
- menuList[0].menuItem = "Plain Egg"; menuList[0].menuPrice = 1.45;
- menuList[1].menuItem = "Bacon and Egg"; menuList[1].menuPrice = 2.45;
- menuList[2].menuItem = "Muffin"; menuList[2].menuPrice = 0.99;
- menuList[3].menuItem = "French Toast"; menuList[3].menuPrice = 1.99;
- menuList[4].menuItem = "Fruit Basket"; menuList[4].menuPrice = 2.49;
- menuList[5].menuItem = "Cereal"; menuList[5].menuPrice = 0.69;
- menuList[6].menuItem = "Coffee"; menuList[6].menuPrice = 0.50;
- menuList[7].menuItem = "Tea"; menuList[7].menuPrice = 0.75;
- // We're given a file, let's use it.
- // XXX: Why's this fucking broken?
- /*ifstream inMenu;
- inMenu.open("Ch9_Ex4Data.txt");
- for (int i = 0; i < 8; i++)
- {
- getline(inMenu, menuList[i].menuItem);
- getline(inMenu, menuList[i].menuPrice);
- }
- inMenu.close();*/
- return;
- }
- void showMenu()
- {
- getData();
- for (int i = 0; i < 8; i++)
- {
- // This is closer to the answer key code than I'd like it to be, but goddammit, it works too perfectly.
- cout << i + 1 << ". " << left << setw(20) << menuList[i].menuItem << right << " $" << menuList[i].menuPrice << endl;
- }
- return;
- }
- void printCheck()
- {
- double totalPrice = 0.0;
- for (int i = 0; i < 8; i++)
- {
- // Again, too close to the answer key. The left << setw << arrayItem << right << otherArrayItem part is the only solution I've seen that works.
- // XXX: Why's it segfaulting here?!
- cout << (i + 1) << ". " << left << setw(20) << menuList[request[i].menuItem].menuItem /* this works?! */ << right << request[i].quantity << endl;
- // Let's stuff totalPrice calculation into this loop too.
- totalPrice += request[i].quantity;
- }
- cout << endl << left << setw(20) << "Price of items: " << right << totalPrice << endl;
- double tax = totalPrice * .05;
- cout << left << setw(20) << "Tax: " << right << tax << endl;
- totalPrice += tax;
- cout << left << setw(20) << "Total price: " << right << totalPrice << endl;
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement