SHARE
TWEET

Weapon Shop

zCool May 6th, 2012 32 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. //Declarations
  8. void displayMenu(string options[]);
  9. void displaySaleItems(string itemNames[10], int itemPrices[10]);
  10. void displayPlayerInventory(string playerItemNames[10], int playerItemPrices[10]);
  11. void addItemToInventory(int storeItemIn, string storeItemNames[10], int storeItemPricse[10], string playerItemNamez[10], int playerItemPricez[10]);
  12. void removeItemFromInventory(int index, string inventoryItemNames[10], int inventoryItemPrices[10]);
  13. string storeItemName[10] = {"Item1", "Item2", "Item3", "Item4",
  14.                                            "Item5", "Item6", "Item7", "Item8",
  15.                                            "Item9", "Item10"};
  16. int storeItemPrice[10] = {10, 40, 50, 70, 90,
  17.                                          200, 30, 5, 11, 65};
  18. string menuOptions[3] = {"Buy", "Sell", "Quit"};
  19. string playerItemName[10];
  20. int playerItemPrice[10];
  21. int menuOptionSelected = 0;
  22. int playerMoney = 500;
  23. int storeItemIndex;
  24. int inventorySelectionIndex;
  25.  
  26. //display the menu
  27. displayMenu(menuOptions);
  28.  
  29. //setup the loop
  30. while(menuOptionSelected != 3)
  31. {
  32.         //prompt + read + validate user input
  33.         cout << "Please selct an option from the menu: ";
  34.         cin >> menuOptionSelected;
  35.         while(menuOptionSelected != 1 &&
  36.                   menuOptionSelected !=2 &&
  37.                   menuOptionSelected !=3)
  38.         {
  39.                         cout << "Please selct an option from the menu: ";
  40.                         cin.clear();
  41.                         cin.ignore(100, '\n');
  42.                         cin >> menuOptionSelected;
  43.         }
  44.  
  45.         //Buy Item
  46.         if(menuOptionSelected == 1)
  47.         {
  48.                 displaySaleItems(storeItemName, storeItemPrice);
  49.                
  50.                 //prompt + read + validate user input
  51.                 cout << "Select an item to purchase(1-10): ";
  52.                 cin >> storeItemIndex;
  53.                 while(storeItemIndex < 1 || storeItemIndex > 10)
  54.                 {
  55.                         cin.clear();
  56.                         cin.ignore(100, '\n');
  57.                         cout << "Select an item to purchase(1-10): ";
  58.                         cin >> storeItemIndex;
  59.                 }
  60.  
  61.                 //check if user has enough money
  62.                 if(storeItemPrice[storeItemIndex -1 ] <= playerMoney)
  63.                 {
  64.                         playerMoney = playerMoney - storeItemPrice[storeItemIndex -1];
  65.                         addItemToInventory(storeItemIndex - 1, storeItemName,  storeItemPrice, playerItemName, playerItemPrice);
  66.                         displayPlayerInventory(playerItemName, playerItemPrice);
  67.                         cout << "You have " << playerMoney << "gold left." <<endl;
  68.  
  69.  
  70.                 }
  71.  
  72.                 else
  73.                 {
  74.                         cout << "Not enough money.";
  75.                 }
  76.  
  77.         } // Buy
  78.  
  79.         //Sell
  80.         if(menuOptionSelected == 2)
  81.         {
  82.                 displayPlayerInventory(playerItemName, playerItemPrice);
  83.                 cout << "Sleect an item to sell: ";
  84.                 cin >> inventorySelectionIndex;
  85.  
  86.                 if(inventorySelectionIndex >= 1 && inventorySelectionIndex <= 9)
  87.                 {
  88.                         playerMoney = playerMoney + playerItemPrice[inventorySelectionIndex - 1];
  89.                         removeItemFromInventory(inventorySelectionIndex-1, playerItemName, playerItemPrice);
  90.                         cout << "You have " << playerMoney << " gold left." <<endl;
  91.                 }
  92.  
  93.         }
  94.  
  95.  
  96. }
  97.  
  98.  
  99.  
  100. }
  101.  
  102. //display the menu
  103. void displayMenu(string options[3])
  104. {
  105.         for(int i=0 ; i<3 ; i++)
  106.         {
  107.                 cout << i+1 << ". " << options[i] << endl;
  108.         }
  109. }
  110.  
  111. //display items for sale
  112. void displaySaleItems(string itemNames[10], int itemPrice[10])
  113. {
  114.         cout << "Items for sale: " << endl;
  115.         for(int i=0 ; i<10 ; i++)
  116.         {
  117.                 cout << i+1 << ". " << itemNames[i] << "\tPrice: " << itemPrice[i] << endl;
  118.         }
  119. }
  120.  
  121. //add purchased item from store to player inventory
  122. void addItemToInventory(int storeItemIndex, string storeItemName[10], int storeItemPrice[10], string playerItemName[10], int playerItemPrice[10])
  123. {
  124.         int numPlayerItems = 0;
  125.         while(!playerItemName[numPlayerItems].empty() && numPlayerItems < 10)
  126.         {
  127.                 numPlayerItems++;
  128.         }
  129.  
  130.         if(numPlayerItems < 10)
  131.         {
  132.                 playerItemName[numPlayerItems] = storeItemName[storeItemIndex];
  133.                 playerItemPrice[numPlayerItems] = storeItemPrice[storeItemIndex];
  134.         }
  135.         else if (numPlayerItems == 10)
  136.         {
  137.                 cout << "Inventory is Full";
  138.         }
  139.  
  140. }
  141.  
  142. void displayPlayerInventory(string playerItemName[10], int playerItemPrice[10])
  143. {
  144.         cout << "Player Inventory" << endl;
  145.         int i = 0;
  146.         while(!playerItemName[i].empty() && i < 10)
  147.         {
  148.                 cout << i+1 << ". " << playerItemName[i] << "\tPrice: " << playerItemPrice[i] << endl;
  149.                 i++;
  150.         }
  151. }
  152.  
  153. void removeItemFromInventory(int index, string inventoryItemNames[10], int inventoryItemPrices[10])
  154. {
  155.         for (int i = index ; i < 9 ; ++i)
  156.         {
  157.                 inventoryItemNames[i] = inventoryItemNames[i+1];
  158.                 inventoryItemPrices[i] = inventoryItemPrices[i+1];
  159.         }
  160. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top