Advertisement
riggnaros

9_4 Mindtap structured Array

Nov 28th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. struct menuItemType {
  10.     string menuItem;
  11.     double menuPrice;
  12.     char selected;
  13. };
  14.  
  15. void getData(menuItemType menuList[]); //This function loads the data into the array menuList
  16. void showMenu(menuItemType menuList[]); //This function shows the different items offered by the restaurant and tells the user how to select the items.
  17. void printCheck(menuItemType menuList[]); //5% sales tax - Format your output with two decimal places. The name of each item in the output must be left justified.
  18.  
  19. int main()
  20. {
  21.  
  22.     menuItemType menuList[8];
  23.     getData(menuList);
  24.     showMenu(menuList);
  25.  
  26.     return 0;
  27. }
  28.  
  29. void getData(menuItemType menuList[])
  30. {
  31.  
  32.     ifstream infile;
  33.     infile.open("9_4datafile.txt");
  34.  
  35.     for (int i = 0; i < 8; i++) {
  36.         getline(infile, menuList[i].menuItem);
  37.         infile >> menuList[i].menuPrice;
  38.         menuList[i].selected = 'n';
  39.         //if (infile.peek() == '\n')
  40.  
  41.         infile.ignore();
  42.     }
  43.     infile.close();
  44. };
  45.  
  46. void showMenu(menuItemType menuList[])
  47. {
  48.  
  49.     int count = 1;
  50.     char selection;
  51.     int itemNumber;
  52.  
  53.     cout << "\n Welcome to Charlie Tuna's Resturant"
  54.          << "\n----Today's Menu----"
  55.          << endl;
  56.  
  57.     for (int i = 0; i < 8; i++) {
  58.         cout << count++ << ". ";
  59.         cout << menuList[i].menuItem << " "
  60.              << fixed << setprecision(2) << "$" << menuList[i].menuPrice
  61.              << endl;
  62.     }
  63.  
  64.     cout << "\nYou can make up to 8 single order selections."
  65.          << "\nDo you want to make selection Y/y (Yes), N/n (No): ";
  66.     cin >> selection;
  67.  
  68.     while (selection == 'y' || selection == 'Y') {
  69.  
  70.         cout << "Enter item number: ";
  71.         cin >> itemNumber;
  72.         menuList[itemNumber - 1].selected = 'y';
  73.         cout << "Would you like to make another selection: ";
  74.         cin >> selection;
  75.     }
  76.     printCheck(menuList);
  77.     cout << "Thank you and have a nice day!";
  78. };
  79.  
  80. void printCheck(menuItemType menuList[])
  81. {
  82.  
  83.     double grandTotal = 0;
  84.     double rollingTotal = 0;
  85.  
  86.     for (int i = 0; i < 8; i++) {
  87.  
  88.         if (menuList[i].selected == 'y' || menuList[i].selected == 'Y') {
  89.             rollingTotal += menuList[i].menuPrice;
  90.             grandTotal = rollingTotal + (rollingTotal * 0.05);
  91.         }
  92.     }
  93.  
  94.     cout << "\nYour total is: $" << fixed << setprecision(2) << grandTotal << endl;
  95. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement