Don't like ads? PRO users don't see any ads ;-)
Guest

Daily C++ Exercises: Lesson 3

By: a guest on Aug 3rd, 2012  |  syntax: C++  |  size: 2.74 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <string>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <vector>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8. struct menuItemType {
  9.             string name;
  10.             double price;
  11. };
  12.  
  13. menuItemType* GetData();
  14. void ShowMenu(menuItemType* menuArrP);
  15. void PrintCheck( vector<int>& choices, menuItemType* menuArrP );
  16.  
  17. int main() {
  18.    
  19.         menuItemType* menuList = GetData();        
  20.         vector<int> selections = ShowMenu(menuList);           
  21.         int input;
  22.         vector<int> selections;
  23.         while ( cin >> input ) {
  24.                 cout << "Enter the number for the item you'd like, then Enter. Press '0' and Enter to end your selections: ";
  25.                 cin >> input;
  26.                 if (input >= 1 && input <= 7) {
  27.                         selections.push_back(input);
  28.                 }
  29.                 else if (input != 0) {
  30.                         cout << "Invalid seleciton. Please try again: ";
  31.                 }
  32.         }
  33.    
  34.         PrintCheck( selections, menuList);
  35.     system("PAUSE");
  36.     return 0;
  37. }
  38.  
  39. menuItemType* GetData() {
  40.            
  41.             menuItemType* mip = new menuItemType[7];
  42.            
  43.             menuItemType plainEgg;
  44.             plainEgg.name = "1. Plain Egg";
  45.             plainEgg.price = 1.45;
  46.             mip[0] = plainEgg;
  47.            
  48.             menuItemType baconEgg;
  49.             baconEgg.name = "2. Bacon and Egg";
  50.             baconEgg.price = 2.45;
  51.             mip[1] = baconEgg;
  52.            
  53.             menuItemType muffin;
  54.             muffin.name = "3. Muffin";
  55.             muffin.price = 0.99;
  56.             mip[2] = muffin;
  57.            
  58.             menuItemType frenchToast;
  59.             frenchToast.name = "4. French Toast";
  60.             frenchToast.price = 1.99;
  61.             mip[3] = frenchToast;
  62.            
  63.             menuItemType fruitBasket;
  64.             fruitBasket.name = "5. Fruit Basket";
  65.             fruitBasket.price = 2.49;
  66.             mip[4] = fruitBasket;
  67.            
  68.             menuItemType cereal;
  69.             cereal.name = "6. Cereal";
  70.             cereal.price = 0.50;
  71.             mip[5] = cereal;
  72.            
  73.             menuItemType tea;
  74.             tea.name = "7. Tea";
  75.             tea.price = 0.75;
  76.             mip[6] = tea;
  77.            
  78.                         return mip;
  79. }
  80.  
  81. void ShowMenu(menuItemType* menuArrP) {
  82.             int menuSize = 7;
  83.             for (int i = 0; i < menuSize; ++i) {
  84.                 cout.precision(2);        
  85.                                 cout.width(30);
  86.                         cout << setfill('.') << internal << fixed << left <<
  87.                                                         menuArrP[i].name << right << menuArrP[i].price << endl;
  88.             }
  89. }
  90.  
  91. void PrintCheck (vector<int>& choices, menuItemType* menuArrP) {
  92.         const double tip = 0.05;
  93.         double bill = 0;
  94.  
  95.         for (int i = 0; i < choices.size(); ++i) {
  96.                 bill += menuArrP[ choices[i]-1 ].price;
  97.         }
  98.         bill += bill * tip;
  99.         cout.precision(2);
  100.         cout << "Your total, with tax, is: $" << bill << endl;
  101. }