Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- //Constant globals are okay, though you should avoid globals when possible
- const int numItems = 6;
- const string shopItemNames[numItems] = { "Boots", "Swords", "Helmets", "Kittens", "Poleaxes", "Leggings" };
- //Prototypes
- void printInventory(const int playerInventory[numItems]);
- void printShop();
- bool buyItems(int playerInventory[numItems]);
- int main()
- {
- //Tells us when the game is over
- bool isDone = false;
- //stores the player inventory in an array
- int playerInventory[numItems] = { 0, 0, 0, 1, 0, 0 };
- cout << "*** Welcome to the item shop! ***\n";
- //Game loop
- while (isDone == false) {
- printShop();
- printInventory(playerInventory);
- isDone = buyItems(playerInventory);
- cout << "\n\n\n";
- }
- system("PAUSE");
- return 0;
- }
- void printInventory(const int playerInventory[numItems])
- {
- cout << "Your inventory is: \n";
- //We have to loop through each item one at a time and print it out
- for (int i = 0; i < numItems; i++) {
- //Only print something if the player has at least 1
- if (playerInventory[i] > 0) {
- cout << playerInventory[i] << " x " << shopItemNames[i] << endl;
- }
- }
- cout << endl;
- }
- void printShop()
- {
- cout << "Shop Inventory: \n";
- //Print each shop item with a number in front
- for (int i = 0; i < numItems; i++) {
- cout << i + 1 << ". " << shopItemNames[i] << endl;
- }
- cout << "**********\n\n";
- }
- bool buyItems(int playerInventory[numItems])
- {
- int input;
- cout << "What would you like to buy? Enter -1 to quit. Enter (" << 1 << "-" << numItems << "):";
- cin >> input;
- if (input == -1) {
- return true; //the program is over
- }
- //Had to change this to stop a bug!
- if (input - 1 < 0 || input - 1 >= numItems) {
- cout << "That was a bad input!\n";
- return false; //keep going
- }
- //Add one to the item that we bought
- playerInventory[input - 1]++;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment