Advertisement
pauldacheez

SEGFAULTS DAMMIT

Dec 12th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.65 KB | None | 0 0
  1. /* p626n4
  2. 4. Write a program to help a local restaurant automate its breakfast billing system. The program should do the following:
  3.     a. Show the customer the different breakfast items offered by the restaurant.
  4.     b. Allow the customer to select more than one item from the menu.
  5.     c. Calculate and print the bill.
  6.  
  7. Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right of the item):
  8. Plain Egg       $1.45
  9. Bacon and Egg   $2.45
  10. Muffin          $0.99
  11. French Toast    $1.99
  12. Fruit Basket    $2.49
  13. Cereal          $0.69
  14. Coffee          $0.50
  15. Tea             $0.75
  16.  
  17. 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:
  18. • Function getData: This function loads the data into the array menuList.
  19. • Function showMenu: This function shows the different items offered by the restaurant and tells the user how to select the items.
  20. • Function printCheck: This function calculates and prints the check. (Note that the billing amount should include a 5% tax.)
  21.  
  22. A sample output is:
  23. Welcome to Johnny's Restaurant
  24. Bacon and Egg   $2.45
  25. Muffin          $0.99
  26. Coffee          $0.50
  27. Tax             $0.20
  28. Amount Due      $4.14
  29.  
  30. 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.
  31. */
  32.  
  33. #include <iostream>
  34. #include <fstream>
  35. #include <iomanip>
  36. #include <string>
  37.  
  38. using namespace std;
  39.  
  40. // Function prototypes.
  41. void getData();
  42. void showMenu();
  43. void printCheck();
  44.  
  45. // Let's use a struct!
  46. struct menuItemType
  47. {
  48.     string menuItem;
  49.     double menuPrice;
  50. };
  51.  
  52. struct requestType
  53. {
  54.     int menuItem;
  55.     int quantity;
  56. };
  57.  
  58. // For lack of a lazier method of sharing an array between functions, let's make these global vars.
  59. // Is this bad practice? Of course. I'm supposed to pass things to functions.
  60. menuItemType menuList[7];
  61. requestType request[7];
  62.  
  63. int main()
  64. {
  65.     cout << "Welcome to $RESTAURANT!" << endl;
  66.     showMenu();
  67.  
  68.     cout << "Order by typing in the item number followed by the quantity of the item." << endl;
  69.     cout << "Ordering multiple items? Follow the quantity with a semicolon, then input another item + quantity." << endl;
  70.     cout << "Example input: 4 1; 3 1; 7 2" << endl;
  71.    
  72.     for (int i = 0; i < 8; i++)
  73.     {
  74.         cin >> request[i].menuItem;
  75.         cin >> request[i].quantity;
  76.         char x = '0';
  77.         cin.get(x);
  78.         if (x != ';')
  79.             i = 7;
  80.         x = '0';
  81.     }
  82.  
  83.     printCheck();
  84.  
  85.     return 0;
  86. }
  87.  
  88. void getData()
  89. {
  90.     // Hardcoded values, for testing purposes.
  91.     // (also wtf these prices are low, coffee usually costs $1.50 these days)
  92.    
  93.     menuList[0].menuItem = "Plain Egg";         menuList[0].menuPrice = 1.45;
  94.     menuList[1].menuItem = "Bacon and Egg";     menuList[1].menuPrice = 2.45;
  95.     menuList[2].menuItem = "Muffin";            menuList[2].menuPrice = 0.99;
  96.     menuList[3].menuItem = "French Toast";      menuList[3].menuPrice = 1.99;
  97.     menuList[4].menuItem = "Fruit Basket";      menuList[4].menuPrice = 2.49;
  98.     menuList[5].menuItem = "Cereal";            menuList[5].menuPrice = 0.69;
  99.     menuList[6].menuItem = "Coffee";            menuList[6].menuPrice = 0.50;
  100.     menuList[7].menuItem = "Tea";               menuList[7].menuPrice = 0.75;
  101.    
  102.  
  103.     // We're given a file, let's use it.
  104.     // XXX: Why's this fucking broken?
  105.     /*ifstream inMenu;
  106.     inMenu.open("Ch9_Ex4Data.txt");
  107.     for (int i = 0; i < 8; i++)
  108.     {
  109.         getline(inMenu, menuList[i].menuItem);
  110.         getline(inMenu, menuList[i].menuPrice);
  111.     }
  112.     inMenu.close();*/
  113.     return;
  114. }
  115.  
  116. void showMenu()
  117. {
  118.     getData();
  119.     for (int i = 0; i < 8; i++)
  120.     {
  121.         // This is closer to the answer key code than I'd like it to be, but goddammit, it works too perfectly.
  122.         cout << i + 1 << ". " << left << setw(20) << menuList[i].menuItem << right << " $" << menuList[i].menuPrice << endl;
  123.     }
  124.     return;
  125. }
  126.  
  127. void printCheck()
  128. {
  129.     double totalPrice = 0.0;
  130.     for (int i = 0; i < 8; i++)
  131.     {
  132.         // Again, too close to the answer key. The left << setw << arrayItem << right << otherArrayItem part is the only solution I've seen that works.
  133.         // XXX: Why's it segfaulting here?!
  134.         cout << (i + 1) << ". " << left << setw(20) << menuList[request[i].menuItem].menuItem /* this works?! */ << right << request[i].quantity << endl;
  135.         // Let's stuff totalPrice calculation into this loop too.
  136.         totalPrice += request[i].quantity;
  137.     }
  138.  
  139.     cout << endl << left << setw(20) << "Price of items: " << right << totalPrice << endl;
  140.     double tax = totalPrice * .05;
  141.     cout << left << setw(20) << "Tax: " << right << tax << endl;
  142.     totalPrice += tax;
  143.     cout << left << setw(20) << "Total price: " << right << totalPrice << endl;
  144.  
  145.     return;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement