I want to place an object into an array so that when i call the array my stats, inventory objects will show up is it possible ?


here is my code:

void displayStats()
	{
    const int SIZE = 11;
    string stats[SIZE];
	status Status = {100,20};
	cout << "******Stats******\n";
	stats[0] = "Health\n";
    stats[1] = "Strength\n";
	stats[2] = "Empty\n";
	stats[3] = "Empty\n";
	stats[4] = "Empty\n";
	stats[5] = "Empty\n";
	stats[6] = "Empty\n";
	stats[7] = "Empty\n";
	stats[8] = "Empty\n";
	stats[9] = "Empty\n";
	stats[10] = "To continue press Enter.\n";
	cout << "\n" <<endl;
    for (int s = 0; s < SIZE; ++s) {
	
    cout << stats[s];
    }
    getchar();
	getchar();
	}

Of course it is possible! Think of an object as a piece of data. It has a type and it has a value. Exactly the same way you create an array of ints. An int is a type, and it has a value.

So, Say your class name is Foo...Therefore, its type is Foo. So, create an array of type Foo as you would an array of type ints!

I think you need to describe more exactly what you want to do. Do you want to run through an array of items and display the stats of the items in the array?

I think you need to describe more exactly what you want to do. Do you want to run through an array of items and display the stats of the items in the array?

Hey Nathan, I want to display the statistics object Health through stats[1]

i figured i could donsomething like this

stats[1] = "Health = " << Status.health;

But as you might already know this is incorrect.

What is the definition of your status class? You might be able to control all of the formatting from there depending on how you store the data.

What is the definition of your status class? You might be able to control all of the formatting from there depending on how you store the data.

class status{
public://Access Specifier.
	int health;// Attributes.
	int strength;// Attributes.
};

thats my status class im not sure if your looking for anythin else im a big noob to C++ im only doing it 2 weeks.

Here's something to start:

class Object {
public:
    // "normally" these would be private or protected, and accessed or modified through methods, but it's fine for now
    int int_val;
    std::string  str_val;
    Object2 some_other_kind_of_obj;
public:
    void print(void);
}

void Object::print(void) {
    cout << "int_val = " << int_val << endl;
    cout << "str_val = " << '"' << str_val << '"' << endl;
    cout << "obj = {" << some_other_kind_of_obj << "}" << endl;
}

Note that Object2 would have to overload the << operator for this to work, but if you stick to simple pre-defined types for your various members, then it should work just fine as-is. We can get fancier later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.