Classes and structures

I'm building a sports text-based game and have a beginner level question. Say I have 100 different teams (I've set them up as classes) and each team has 10 players (I've set the players up as structures within the team class). Each of the player strucures has 10 members (these are player attributes like height, weight, etc). 2 questions:

1) How do I set up my code so I can call a specific attribute of a specific player on a specific team?


2) I want more than one team to have the same player be one of its players. How do I do this?

Below is my code. As an example to my first question, I tried totalteams[2].totalplayers[3].position but that format doesn't work because the totalplayers array is not recognized as one of the members of the totalteams array. I don't know how you call structures within a class.

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
//create team class
class team {
public:
	int teamNumber;
	string name;
	int currentPlayerCount;
	int budget;
	float powerRank;

	
	struct player {
	int rank;
	string name;
	string position;
	string state;
	string height;
	int weight;
	string school;
	float forty_time;
	float visitScore;
	int visitNumber;
	
	
};

	}; 

//create totalteams array
const int maxteams = 100;
team totalteams[maxteams];

//create totalplayers array
const int maxplayers = 100;
player totalplayers[maxplayers];

  
Hi, just wanted to rebubble this in the event someone can point me in the right direction.
So, what I would do is, not define the struct in the class, try something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct player {
	int rank;
	string name;
	string position;
	string state;
	string height;
	int weight;
	string school;
	float forty_time;
	float visitScore;
};

class team {
        public:
	     int teamNumber;
	     string name;
	     int currentPlayerCount;
	     int budget;
	     float powerRank;
             player playersInfo[]; //I would store the information of each player in this array 
};


In the long run, you might not want to use an array, but instead something that can increase in size as you add/remove/trade players like a vector or something along those lines.
Last edited on
You could look at teams and players as being in separate structures. That way, your problem of having the same player in more than one team is solved.

For example, in the teams class, you store your players as an array of player indices. When you need to render information about a team's players, just iterate through the players array using the index value of each player. Here is a very "thin" example of what I'm trying to convey. I have made the team class public for simplicity (please don't do it this way):

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

class team
    {
    public:
        int teamNumber;
        int currentPlayerCount;
        int budget;
        float powerRank;

        int players[ maxplayers ];

    };

struct player
    {
       int Id;    /*    Make this a unique number for lookups    */
       int rank;
       string name;
       string position;
       string state;
       string height;
       int weight;
       string school;
       float forty_time;
       float visitScore;
       int visitNumber;

    };

    const int maxteams = 100;
    team totalteams[maxteams];


    const int maxplayers = 100;
    player totalplayers[maxplayers];


int main( int argc, char** argv )
    {
    size_t ThisMuch = sizeof( player );

    /*    Render a player's info:    */


    player  Person;

    Person.Id = 9;

    Person.name = "Sandy Koufax";

    Person.position = "pitcher";

    Person.state = "New Jersy";

    Person.rank = 1;

    memcpy( &totalteams[ 0 ].players, &Person, ThisMuch );

    totalteams[ 0 ].players[ 0 ] = Person.Id;

    totalteams[ 0 ].currentPlayerCount++;


    /*  Find player 9:    */

    for( i = 0; i < totalteams.currentPlayerCount; i++ )
        {
        if( totalteams.players[ i ] == Person.Id )
            {
             cout << "Found!" << endl;
    
             break;

            }    /*    if( found )    */

        }    /*    for( i < totalteams.currentPlayerCount )    */
    
    }
        /*    main()    */



Topic archived. No new replies allowed.