class invItem {
int weaponMaxDam, weaponMinDam, itemPrice;
string weaponName;
public:
invItem (int,int,int,string);
int maxDam () {return weaponMaxDam;}
int minDam () {return weaponMinDam;}
int meanDam () {return ((weaponMaxDam+weaponMinDam)/2);}
int price () {return itemPrice;}
string itemName() {return weaponName;}
};
invItem::invItem (int a, int b, int c, string d){
weaponMaxDam = a;
weaponMinDam = b;
itemPrice = c;
weaponName = d;
}
invItem shortSword (3,9,3,"shortsword");
void shopEnter ()
{
cout << endl << "What would you like to buy?" << endl;
cin >> playerInput;
if (playerInput == "?"){
cout << endl << "The Shop contains" << endl << shortSword.itemName() << " - " << shortSword.price() << endl ;
shopEnter ();}
elseif (playerInput == shortSword.itemName())
if (goldCoin > shortSword.price()) {
goldCoin - shortSword.price() ;
cout << endl << "You have purchased a sword for " << shortSword.price() << " gold" << endl;
}
else
cout << endl << "I'm sorry, you can't afford this item" << endl;
in the shopEnter function is there a way to use (playerInput == itemName) from invItem instead of having to write a separate if statement for each item in the shop?
Yes, but you should store your items in a more generic container. For example this:
invItem shortSword (3,9,3,"shortsword");
Is "bad" because you're giving a unique variable name to each item. Instead of doing that, you should put all items in a generic container like an array or a vector:
Once you have all items in a common container... you can iterate over all of them with a loop:
1 2 3 4 5
for(int i = 0; i < num_items; ++i)
{
if( playerInput == items[i].getName() )
{
//...
EDIT:
In the future... an even better way to store fixed information like item properties is in external files. Then at startup you can load the files and build that 'items' container dynamically. This will allow you to make changes to item properties and/or add/remove items without having to recompile.