Advertisement
Lawnknome

shopCart.cpp

Nov 23rd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6.  
  7. struct Item
  8. {
  9.     string name;
  10.     double price;
  11.     int quantity;
  12. };
  13.  
  14. void addItem (Item shoppingCart[], const int array, int count);
  15. void listCart(Item shoppingCart[], const int array, int count);
  16. void finalTotal(Item shoppingCart[], const int array, int count);
  17.  
  18. int main ()
  19. {
  20.     const int array = 100;      //size of cart array
  21.     int menu;                   //menu selection input
  22.     Item shoppingCart[array];   //array to hold all items
  23.     int count = 0;              //keeps track of shopping cart position additions
  24.  
  25.     do
  26.     {
  27.         cout << "Please view the allowable options with your cart.\n";
  28.         cout << "1. Add - Allows addition of item to cart.\n";
  29.         cout << "2. List - Lists contents of current cart; including price and quantity.\n";
  30.         cout << "3. Total - Displays current final total w/o sales tax.\n";
  31.         cout << "4. Quit Program.\n";
  32.         cout << "Please enter a menu choice 1 - 4: " << endl;
  33.         cin >> menu;
  34.  
  35.         while ((menu > 4) && (menu < 1))
  36.         {
  37.             cin.clear();
  38.             cin.ignore(1000, '\n');
  39.             cout << "ERROR" << endl;
  40.             cout << "Please be sure to enter a number between 1 and 4.";
  41.             cin >> menu;
  42.         }
  43.  
  44.         switch (menu)
  45.         {
  46.             case 1:
  47.                 addItem(shoppingCart, array, count);
  48.                 count++;
  49.                 break;
  50.  
  51.             case 2:
  52.                 listCart(shoppingCart, array, count);
  53.                 break;
  54.    
  55.             case 3:
  56.                 finalTotal(shoppingCart, array, count);
  57.                 break;
  58.    
  59.             case 4:
  60.                 return(0);
  61.    
  62.         }
  63.  
  64.     }while(1); 
  65.  
  66.     return 0;
  67. }
  68.  
  69. void addItem (Item shoppingCart[], const int array, int count)
  70. {
  71.  
  72.     cout << "\nPlease enter the details of the item to be added to your cart.\n";
  73.     cout << "Product name: ";
  74.     getline(cin, shoppingCart[count].name);
  75.     cin.ignore(256, '\n');
  76.  
  77.     cout << "Price: ";
  78.     cin >> shoppingCart[count].price;
  79.  
  80.     cout << "Quantity: ";
  81.     cin >> shoppingCart[count].quantity;
  82.  
  83.     cout << "\n\n";
  84.  
  85. }
  86.  
  87. void listCart(Item shoppingCart[], const int array, int count)
  88. {
  89.     cout << setw(15) << left << "Product" << "\t" <<" Price" << "\t" << right << " Quantity" << endl;
  90.  
  91.     for(int i = 0; i < count; i++)
  92.     {
  93.         cout << setw(15) << left << shoppingCart[i].name
  94.              << "\t" << " $" << showpoint << setprecision(2) << fixed << shoppingCart[i].price
  95.              << "\t" << " " << right << shoppingCart[i].quantity << endl;
  96.         cout << "\n";
  97.     }  
  98. }
  99.  
  100. void finalTotal(Item shoppingCart[], const int array, int count )
  101. {
  102.     double currentTotal;
  103.  
  104.     for(int i = 0; i < count; i++)
  105.     {
  106.         currentTotal += (shoppingCart[i].price * shoppingCart[i].quantity);
  107.     }  
  108.  
  109.     cout << "Your current total is " << showpoint << setprecision(2) << currentTotal << "." << endl;
  110.     cout << endl;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement