Guest User

ItemShop

a guest
Jun 4th, 2014
4,690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. //Constant globals are okay, though you should avoid globals when possible
  7. const int numItems = 6;
  8. const string shopItemNames[numItems] = { "Boots", "Swords", "Helmets", "Kittens", "Poleaxes", "Leggings" };
  9.  
  10. //Prototypes
  11. void printInventory(const int playerInventory[numItems]);
  12. void printShop();
  13. bool buyItems(int playerInventory[numItems]);
  14.  
  15. int main()
  16. {
  17.     //Tells us when the game is over
  18.     bool isDone = false;
  19.     //stores the player inventory in an array
  20.     int playerInventory[numItems] = { 0, 0, 0, 1, 0, 0 };
  21.  
  22.     cout << "*** Welcome to the item shop! ***\n";
  23.  
  24.     //Game loop
  25.     while (isDone == false) {
  26.  
  27.         printShop();
  28.  
  29.         printInventory(playerInventory);
  30.  
  31.         isDone = buyItems(playerInventory);
  32.  
  33.         cout << "\n\n\n";
  34.     }
  35.  
  36.  
  37.     system("PAUSE");
  38.     return 0;
  39. }
  40.  
  41. void printInventory(const int playerInventory[numItems])
  42. {
  43.     cout << "Your inventory is: \n";
  44.     //We have to loop through each item one at a time and print it out
  45.     for (int i = 0; i < numItems; i++) {
  46.         //Only print something if the player has at least 1
  47.         if (playerInventory[i] > 0) {
  48.             cout << playerInventory[i] << " x " << shopItemNames[i] << endl;
  49.         }
  50.     }
  51.     cout << endl;
  52. }
  53.  
  54. void printShop()
  55. {
  56.     cout << "Shop Inventory: \n";
  57.     //Print each shop item with a number in front
  58.     for (int i = 0; i < numItems; i++) {
  59.         cout << i + 1 << ". " << shopItemNames[i] << endl;
  60.     }
  61.  
  62.     cout << "**********\n\n";
  63. }
  64.  
  65. bool buyItems(int playerInventory[numItems])
  66. {
  67.     int input;
  68.     cout << "What would you like to buy? Enter -1 to quit. Enter (" << 1 << "-" << numItems << "):";
  69.     cin >> input;
  70.  
  71.     if (input == -1) {
  72.         return true; //the program is over
  73.     }
  74.     //Had to change this to stop a bug!
  75.     if (input - 1 < 0 || input - 1 >= numItems) {
  76.         cout << "That was a bad input!\n";
  77.         return false; //keep going
  78.     }
  79.  
  80.     //Add one to the item that we bought
  81.     playerInventory[input - 1]++;
  82.     return false;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment