Making an array of class items

So I have made a text based role playing game and pretty much when the character gets to a certain coordinate on the map they receive the option to go into the store or not. What I need to do is display a list of items in my in-game store once they enter. I thought the best way to do this would be to put all the items (weapon structs, armor structs) that are in my in-game store into an array. I would then be able to do something like:

1
2
3
4
5
6
7
8
for (int i=0; i!=arraysize; i++)
{
      //This should display both weapon and armor
      // including the price, name, and damage/armor value
      //for example:
      //weapon1.mName, weapon1.mCost, weapon1.mDamageRange 
      cout << StoreItemsArray[i] << endl;
} 


My store will contain both weapon items and armor items, which are defined as follows:

1
2
3
4
5
6
7
8
9
10
11
12
struct Weapon
{
	std::string mName;
	Range       mDamageRange;//range is another struct that is 2 values (1 low and 1 high value)
    int         mCost;
};
struct Armor
{
    std::string mName;
    int mArmorValue;
    int mCost;
};


So as you can see, I could do this the manual way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Weapon weapon1;
Weapon weapon2;
weapon1.mCost=100;
Weapon1.mDamageRange.mLow=10;
Weapon1.mDamageRange.mHigh=15;
Weapon1.mName="Weapon 1";

weapon2.mCost=100;
Weapon2.mDamageRange.mLow=10;
Weapon2.mDamageRange.mHigh=15;
Weapon2.mName="Weapon 2";

cout << "Weapon 1 \t" << weapon1.mCost << "\t" << weapon1.mDamageRange << "damage";
cout << "Weapon 2 \t" << weapon2.mCost << "\t" << weapon2.mDamageRange << "damage";




but I feel like that is not general enough and is harder to maintain (if someone feels differently please tell me). So my question is how would i do this? How would I make this more general and more manageable? I hope this is enough information to receive some help, and hopefully its not confusing!


Try using a vector data type.

You might need a seperate vector for weapons and items, but that's probably ok for now.

Then you can just use one Weapon for a buffer.

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
#include <vector>
using namespace std;

vector<Weapon> weaponStash;
Weapon weaponBuffer;

// set properties for 1
weaponBuffer.mCost=200;
weaponBuffer.mDamageRange.mLow=100;
weaponBuffer.mDamageRange.mHigh=150;
weaponBuffer.mName="Destroyer";
weaponStash.push_back(weaponBuffer);    // save in vector
// make another item
weaponBuffer.mCost=20;
weaponBuffer.mDamageRange.mLow=10;
weaponBuffer.mDamageRange.mHigh=15;
weaponBuffer.mName="Broken Dagger";
weaponStash.push_back(weaponBuffer);    // save in vector

// iterators
vector<Weapon>::iterator b,e;
b = weaponStash.begin();
e = weaponStash.end();

// Display the items
while(b!=e)
{
  weaponBuffer = *b;
  // display stats on weaponBuffer
  cout << weaponBuffer.mName << "\t" << weaponBuffer.mCost << "\t" << weaponBuffer.mDamageRange << "damage";
  ++b;
}
+1 on vectors
It would be better that you define a stream insertion operator, to simplify printing:
1
2
3
4
5
6
7
8
9
ostream& operator<<(ostream&, const Weapon& x)
{
      return cout<<x.mName<<"\nRange: "<<x.mRange.mLow<<"-"<<x.mRange.mHigh<<"\nCost: "<<mCost;
      }

ostream& operator<<(ostream&, const Armor& x)
{
      return cout<<x.mName<<"\nAP: "<<x.mArmorValue<<"\nCost: "<<x.mCost;
      }

The you can simply have 2 arrays (OK, OK, or vectors if he likes...) and just output them naturely!
Last edited on
Topic archived. No new replies allowed.