Advertisement
Need4Sleep

main.cpp (DailyC++) 7/20/12

Jul 23rd, 2012
1,713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. ///CODED BY AARGH(cplusplus.com)
  2. ///http://www.cplusplus.com/forum/beginner/75558/
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7. struct menuItemType
  8. {
  9.     string name;
  10.     double price;
  11. };
  12.  
  13. void GetData(menuItemType & menu, string name, double price)
  14. {
  15.     menu.name = name;
  16.     menu.price = price;
  17. }
  18.  
  19. void ShowMenu()
  20. {
  21.     cout << "(1) Plain Egg.............1.45\n"
  22.             << "(2) Bacon and Egg....2.45\n"
  23.             << "(3) Muffin..................0.99\n"
  24.             << "(4) French Toast.......1.99\n"
  25.             << "(5) Fruit Basket.........2.49\n"
  26.             << "(6) Cereal.................0.50\n"
  27.             << "(7) Tea......................0.75\n";
  28. }
  29.  
  30. void printCheck(menuItemType& selection, double* total)
  31. {
  32.     *total += selection.price;
  33.     cout << selection.name << " " << selection.price << endl;
  34. }
  35.  
  36. int main()
  37. {
  38.     menuItemType menuList[50];
  39.     cout << "Select up to 50 items by their menu numbers.  Press s then enter key to stop\n";
  40.     int choice;
  41.     int counter = 0; // used to ensure array does not run out of bounds
  42.     double price; // price for each menu item added to the array
  43.     double total = 0; // running total
  44.     string name; // name for each menu item added to the array
  45.     menuItemType * ptr_begin(menuList); // pointers will be used to manual figure out the size of the array (how many elements have valid values in them)
  46.     menuItemType * ptr_end(menuList); // end pointer will iterate down the array to mark where the end would be
  47.  
  48.     ShowMenu();
  49.     while (cin >> choice)
  50.     {
  51.         ShowMenu();
  52.         if (choice == 1)
  53.         {
  54.             price = 1.45;
  55.             name = "Plain Egg";
  56.         }
  57.         else if (choice == 2)
  58.         {
  59.             price = 2.45;
  60.             name = "Bacon and Egg";
  61.         }
  62.         else if (choice == 3)
  63.         {
  64.             price = 0.99;
  65.             name = "Muffin";
  66.         }
  67.         else if (choice == 4)
  68.         {
  69.             price = 1.99;
  70.             name = "French Toast";
  71.         }
  72.         else if (choice == 5)
  73.         {
  74.             price = 2.49;
  75.             name = "Fruit Basket";
  76.         }
  77.         else if (choice == 6)
  78.         {
  79.             price = 0.50;
  80.             name = "Cereal";
  81.         }
  82.         else if (choice == 7)
  83.         {
  84.             price = 0.75;
  85.             name = "Tea";
  86.         }
  87.         else // if user entered an improper selection number.
  88.         {
  89.             cout << "\n***Thats not a proper selection.  Please try again.***\n";
  90.                 continue;
  91.         }
  92.         GetData(menuList[counter], name, price);
  93.         ptr_end += 1;
  94.         ++counter;
  95.         if (counter == 50) // user entered maximum selection of menu items.
  96.             break;
  97.     }
  98.     if (ptr_begin != ptr_end) // if pointers point to different areas of array then we know user chose something to order and will display the bill.
  99.     {
  100.         cout << "\nYou selection consists of;\n";
  101.         for (menuItemType * ptr_begin(menuList); ptr_begin != ptr_end; ptr_begin += 1)
  102.         {
  103.             printCheck(*ptr_begin, &total);
  104.         }
  105.         cout << "\nYour total bill including 5% tax will be: $";
  106.         cout.precision(3);
  107.         cout << total * 1.05 << endl;
  108.     }
  109.     cout << "\nThank you.  Come again.\n";
  110.     char f; // use to prevent console from closing down.  may not work on MS visual C++ compiler for some reason.
  111.     cin >> f;
  112.     //system("pause"); //use for MS visual C++ to prevent console from closing down too fast.
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement