1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
#include <algorithm>
using namespace std;
const int items = 5;
const string ShopInventory[items] = {"Helms", "Boots", "Swords", "Axes", "Leather Armors"};
const int ItemPrices[items]= {10, 5, 20, 30, 50};
void PrintShop(int &);
int PlayerBuy(string [], int&);
void DisplayInventory(string []);
int main()
{
string PlayerInventory[items];
int playerMoney = 100;
char Response;
cout << "\t\tWelcome traveler to my lovely shop. \n";
cout << "\nFeel free to browse many of the wonderful items within";
cout << " this store. " << endl;
while(toupper(Response) != 'N')
{
PrintShop(playerMoney);
cout << PlayerBuy(PlayerInventory, playerMoney);
cout << "\n\nIs there anything else that you would like to buy? ";
cout << "Enter n or N to quit.\nElse enter y or Y to continue. ";
cin >> Response;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
while(toupper(Response) != 'Y' && toupper(Response) != 'N')
{
cerr << "\nSorry, wrong input. Please try again. ";
cin >> Response;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
DisplayInventory(PlayerInventory);
return 0;
}
void PrintShop(int &playerMoney)
{
cout << endl;
cout << " ***** Shop Inventory ***** " << endl << endl;
cout << "Shop items " << "\t\tPrice Per Item. " << endl << endl;
for(int i=0; i<items; i++)
{
cout << i+1 << ".) " << left << setw(22) << ShopInventory[i];
cout << left << setw(3) << ItemPrices[i] << " Gold" << endl;
}
cout << "\nYou currently have " << playerMoney << " gold available. ";
}
int PlayerBuy(string PlayerInventory[], int &playerMoney)
{
string input;
while (true)
{
cout << endl;
cout << "\nEnter what you would like to buy. ";
getline(cin, input);
auto it = find(begin(ShopInventory), end(ShopInventory), input);
if (it == end(ShopInventory))
{
cout << "\nThe item that you enter isn't in my inventory. ";
continue;
}
int index = std::distance(begin(ShopInventory), it);
if (!PlayerInventory[index].empty())
{
cout << "\nThe item that you enter is already in your inventory. ";
break;
}
else if(playerMoney - ItemPrices[index] < 0)
{
cout << "\nYou don't have enough money to buy this item.";
cout << " Returning to the main menu. ";
cout << endl;
break;
}
else
{
PlayerInventory[index] = input;
playerMoney -= ItemPrices[index];
break;
}
}
cout << "\nThe amount of money that you have now is. ";
return playerMoney;
}
void DisplayInventory(string PlayerInventory[])
{
cout << "\n\nThis is what You have in your inventory at the moment. ";
cout << endl << endl;
for(int i=0; i<items; i++)
{
if(PlayerInventory[i]==ShopInventory[i])
{
cout << PlayerInventory[i] << " ";
}
}
cout << endl << endl;
}
|