How to access information on an Object the user will create.

So, I've been struggling with how to do this for a while now.

I have this function, which the user uses to let the program know how many Territories he wants to be created, for example, if the user would put "2", he would get Territory1 and Territory2:

void create(int zz, int xx){

int name;
while(zz < xx){
name = zz;
GenericEarth name(zz);
cout << "Territory " << name.gname << " created" << endl;
zz++;

}

This is the class to which the Objects are related:

GenericEarth::GenericEarth(int zzz){
truename = zzz;
gname = "Generic" + std::to_string(zzz);
gresist = 5;
gpproduct = 1;
ggold = 1;
gpoints = 1;
gtaken = "No";

So far, the program has been working, I have a casual interface which leads allows the user to start the game, take Territories and create them. Now I want to create a function that loops through all the Objects created under the class GenericEarth and print all the information related to the Territories, kinda in a way to let the user know which Territory has what, if it is taken already etc.

I've tried a couple of things such as creating a loop such as

void justanexample(xx){ //takes in XX which is the total number of Territories created up until now.

int cicle = 1;
while(cicle <= xx){
cout << "Territory name: " << cicle.gname << endl;
cout << "Territory gold per round: " << cicle.ggold << endl;
cicle++;

}

}

So in my mind, since each Object name is an Integer, is this case zz, I thought I just had to do the same loop used for creation but this time to print the information out.

What am I missing?

Extra INFO: I just began learning C++ 2 days ago. There is more to the code but it's not directly relevant to this specific problem, as it is just a set-up interface with a bunch of couts like "Would you wish to access the creation menu?" etc
Last edited on
Have you learned about scopes yet?
http://www.cplusplus.com/doc/tutorial/namespaces/

In your create function, you have two variables named name,
The first is an int and goes out of scope (no longer accessible) when create exits.
The second is a GenericEarth object and goes out of scope when the while loop exits.

BTW, it's a poor idea to name them both name as it is confusing to the reader.

What you need is a collection of some sort to hold your various objects.
The easiest collection to use is std::vector.
http://www.cplusplus.com/reference/vector/vector/

I don't ordinarily recommend the use of globals, but I've made worlds a global in the example below. When you learn to pass objects by reference, then you can make worlds a local in main and pass the collection to create.

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

class GenericEarth
{   int     truename;    
    int     gresist;
    int     gpproduct;
    int     ggold;
    int     gpoints;
    string  gtaken;

public:
    string  gname;

    GenericEarth (int zzz);
};

//  Globals 
vector<GenericEarth>    worlds;

GenericEarth::GenericEarth(int zzz) 
{   truename = zzz;
    gname = "Generic" + std::to_string(zzz);
    gresist = 5;
    gpproduct = 1;
    ggold = 1;
    gpoints = 1;
    gtaken = "No";
}

void create(int xx) 
{   int     zz = 1;
    
    while (zz <= xx) 
    {   GenericEarth name(zz);

        worlds.push_back (name);
        cout << "Territory " << name.gname << " created" << endl;
        zz++;
    }
}

int main ()
{   //  Create 10 worlds
    create (10);
    //  Now print them
    for (auto w : worlds)
        cout << "Territory " << w.gname << " exists" << endl;
    return 0;
}

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Topic archived. No new replies allowed.