Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <windows.h>
- using namespace std;
- const int numItems = 6;
- const string shopItemNames[numItems] = { "Boots", "Swords", "Helmets", "Kittens", "Poleaxes", "Leggings" };
- void printShop();
- void printInventory(int playerInventory[numItems]);
- void stringUpper(string &s);
- bool buyItems(int playerInventory[numItems]);
- int main()
- {
- int playerInventory[numItems] = {5,3,6,1,0,0};
- bool isDone = false;
- cout << "***Welcome to the Item Shop!***" << endl<<endl;
- while (isDone == false){
- system("cls");
- printShop();
- printInventory(playerInventory);
- isDone = buyItems(playerInventory);
- cout << "\n\n\n";
- printInventory(playerInventory);
- cout << "\n\n\n";
- }
- system("PAUSE");
- return 0;
- }
- void printInventory(int playerInventory[numItems])
- {
- cout << " Your inventory is:" << endl<<endl;
- for (int i = 0; i < numItems; i++){
- if (playerInventory[i]>0){
- cout << " " << playerInventory[i] <<" x " << shopItemNames[i] << endl << endl;
- }
- }
- }
- void printShop()
- {
- cout << "Here's what we have in stock today..." << endl << endl;
- for (int i = 0; i < numItems; i++){
- cout << i+1 << ": "<< shopItemNames[i] << endl;
- }
- }
- bool buyItems(int playerInventory[numItems])
- {
- int inventoryCheck;
- string inputInitial;
- int inputBuy;
- int inputBuyMultiple;
- int inputSell;
- int inputSellMultiple;
- for (int i = 0; i < 1; i){
- cout << "\n\nWould you like to buy or sell an item? (B or S) : ";
- cin >> inputInitial;
- stringUpper(inputInitial);
- if (inputInitial == "B"){
- cout << "\n\nWhat would you like to buy? Enter a number (1 - " << numItems << ") or - 1 to quit:";
- cin >> inputBuy;
- if (inputBuy == -1){
- return true;
- }
- if (inputBuy - 1 < 0 || inputBuy - 1 >= numItems) {
- cout << "Not a valid entry!" << endl;
- cout << "\n\n\n";
- return false;
- }
- cout << "\n\nHow many would you like? Enter a number: ";
- cin >>inputBuyMultiple;
- for (int l = 0; l < inputBuyMultiple; l++){
- playerInventory[inputBuy - 1]++; //adds to inventory
- }
- return false;
- }
- if (inputInitial == "S"){
- cout << "\n\nWhat would you like to Sell? Enter a number between 1 and " << numItems << " or enter - 1 to quit:";
- cin >> inputSell;
- if (inputSell <= numItems){
- inventoryCheck = playerInventory[inputSell - 1];
- if (inventoryCheck >= 1){
- if (inputSell == -1){
- return true;
- }
- if (inputSell - 1 < 0 || inputSell - 1 >= numItems) {
- cout << "\n\nNot a valid entry!" << endl;
- cout << "\n\n\n";
- return false;
- }
- if (inventoryCheck == 1) {
- playerInventory[inputSell - 1]--; //subs from inventory
- return false;
- }
- if (inventoryCheck >= 1) {
- cout << "\n\n\n You have " << inventoryCheck << " " << shopItemNames[i] << ". \n\n\nHow many would you like to sell? Enter a number between 1 and " << inventoryCheck << ": ";
- cin >> inputSellMultiple;
- for (int k = 0; k < inputSellMultiple; k++){
- playerInventory[inputSell - 1]--; //subs from inventory
- }
- return false;
- }
- Sleep(2300);
- return false;
- }
- }
- }
- cout << "\n\n\n\n\n Not a valid entry!\n\n\n\n\n" << endl;
- Sleep(2300);
- return false;
- }
- Sleep(2300);
- return false;
- }
- void stringUpper(string &s)
- {
- for (unsigned int l = 0; l < s.length(); l++)
- {
- s[l] = toupper(s[l]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement