Help with structures and related objects concepts

OK.
So, I am coding monopoly,
I asked the user how many players they want, then I created new Players (STRUCT) for the same number.
Then, right now I want to say "player1.name" or anything similar in main, but I can't because they were declared when the program was running, but then when I close the console, there is no more player1, such of thing, and I can't write a code that deals or gives instructions for the program, like I cant write,
"if (player1.name == "Chad") ... "
because I don't have player 1 declared at all. I got it stored in a vector at the end of the function call.
So, how do I call player1.name from main without getting an error when I re-run the program.
To solve this problem I made a Player player1; in main but this is not what I need. What i need to have is a new player being declared and stored as a variable for each input the user input.
Here is my code.
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <iostream>
#include <string>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <vector>
 
 
using namespace std;
 
struct Position {
  string name;
  int price;
  int owner;
  string color;
  int position;
  int numberOfPosition;
};
struct Player {
  int money=1500;
  int position=1;
  int turn;
  string name;
  //Position positionn;
  //int number;  // number of player
  bool eliminated =false;
};
//*********************************************************************************
 
void startGame();
int dice1();
//int position(Player player1, int diceTotal, Position P[]);
 //int position(Player player1, int diceTotal, Position &p);
int POSITION(Player player1, int diceTotal, Position p[]);
void assignment(Position positionArray []);
void doMath(Position positionArray [], Player player);
void getOutOfJail(Position positionArray [], Player player);
 
void palyersAssignment(vector<Player> &player, int playersToAdd);//CHAD: Added playersToAdd variable for the for loop to reference in function definition
 
 
 
//********************************************************************************************************
//********************************************************************************************************
 
 
 
int main ()
{ 
  Player player1, player2;
  Position positionArray [40];

  //DECLARATIONS of the spots WITHOUT PRINTING
  assignment( positionArray);
 
  //Vector
  cout << "Enter player quantity: ";
  int quantity;
  cin >> quantity; 
  cout << endl;
  vector<Player> players;
 
  palyersAssignment(players, quantity);
 
 
  cout << "vector first component " << players.at(0).name <<endl;//CHAD: Changed from "players[1]" to "players.at(0)." We need to use the Vector functions since the data type is a Vector
 
  //cout <<"Player1 position is: " <<player1.position << endl;
  player1.position = 66; 
  getOutOfJail( positionArray ,  player1);


  int totalOfDices = dice1(); //this line prints everything
  cout << "totalOfDices" << totalOfDices << endl; // this line only print the sum(one number)
 
  POSITION( player1,  totalOfDices,  &*positionArray); // ??
  player1.position = POSITION( player1,  totalOfDices,  &*positionArray);
  cout << endl << endl;
  
  cout << players.at(1).name;
  if (player1.position == 3)
    {
      player1.money -= positionArray[3].price;
 
      cout << "Position3 price is: " << positionArray[3].price << endl;
      cout <<"You have : $ " << player1.money << endl;
    }
  else
    cout << "it was not 3" << endl;
 
   cout <<"Should c out my name: " << players.at(0).name;

  return 0;
}
 
void palyersAssignment(vector<Player> &players, int playersToAdd)
{
 // void Print_Player_Info(const std::vector<Player>& players)
 
  for (int i = 0; i < playersToAdd; i++)//CHAD: Altered for loop to start at 0 and execute code first then add 1 to i
  {
      Player newPlayer;//CHAD: Creates new player on every itteration of the for loop
      cout<< "player " << i + 1 <<" turn is #: " << i + 1 << endl;
      newPlayer.turn =i+1;
      //cin >> newPlayer.turn;//CHAD: Used cin directly on newPlayer instead of storing it in a variable first
      cout << "Enter the name for " << i + 1 << endl;
      cin >> newPlayer.name; // then how do I access this <- how do I give it a name?
      //cout << newPlayer.name;
 
      //CHAD: MOST IMPORTANT PART!!!
      //Since we passed the vector by reference with the &,This "push_back(newPlayer)" call adds
      //the "newPlayer" object we created to the vector in main. This means that the
      //"newPlayer" struct wont get destroyed after the for loop itteration is done.
      players.push_back(newPlayer);
      cout << endl;
  }
}
 
void startGame()
{
  int numOfPlayers;
 
  cout <<"Enter the number of players:";
  cin >> numOfPlayers;
  //between 2-8 ? how
  for (int i=0; i<numOfPlayers; i++)
  {
    //Player player[i];
  }
//3 players
 
}
 
int dice1()
{
  int t,t1;
  srand(time(0)); // this the generator, the seed
  t = rand()%6+1; // assigning the number ;
  t1= rand()%6+1;
  cout << "1st dice: " << t<<endl;
  cout << "2nd dice: " <<t1 << endl;
 
  int sum = t+t1;
  return sum;
}
 
 int POSITION(Player player1, int diceTotal, Position p[]) // try to pass in the function as int
{
  //cout <<endl<<endl<< "Insdie the Position function" << endl;
  //cout <<"The sum of the dices is: "<<endl <<dice1() << endl; // just ot make sure everything is in place;
  player1.position =player1.position+ diceTotal;  // the position is added to the sum of the dices .
  //cout <<"player1.position is: " <<player1.position << endl;
  //player1 position = 1 ******
  //int num = player1.position;
 
  p[player1.position].position = player1.position;
  //cout << endl;
  //cout << "P: " << p << "P. Position: " <<p[player1.position].position << endl; // the P gives me the address
  //cout << "Player 1 position" << player1.position << endl;
 // cout << "end of the Position function" << endl << endl;
  return player1.position;
}
 
void doMath(Position positionArray [], Player player)
{
  if(player.position == 11)
  {
    player.position = 66; // In jail
  }
 
  // calling the getOutOfJail function
 
  getOutOfJail( *&positionArray ,  player); // idk if this function call works?
 
}
 
void assignment(Position positionArray [])
{
  positionArray[1].name = "Go";
  positionArray[1].price = 0;
  //***********************************
  positionArray[2].name = "Vine Street";
  positionArray[2].price = 150;
  positionArray[2].owner = -1;
  positionArray[2].color = "brown";
  positionArray[2].position =2;
  positionArray[3].name = "Community Chest";
  positionArray[3].price = 300;
  positionArray[3].owner = -1;
  positionArray[3].color = "PLAIN";
  positionArray[3].position =3;
  positionArray[5].name = "Income Tax";
  positionArray[5].price = 100;
  positionArray[5].owner = -1;
  positionArray[5].color = "PLAIN";
  positionArray[5].position =5;
  //In Jail
  positionArray[6].name ="Just Visiting";
  positionArray[6].position =6;
  positionArray[6].price =0;
 
 
 
  positionArray[11].name = "Go TO Jail";
 
  positionArray[23].name = "Chance";
 
}
 
void getOutOfJail(Position positionArray [], Player player)
{
  bool did_U_RoleDobls = true; // I did role doubles
  int sumOfDices=dice1();
 
   if(player.position ==66)
  {
    cout << "Now you are in Jail" << endl;
    if (did_U_RoleDobls&&sumOfDices ==12)
    {
      cout << "You are getting out of Jail and heading to ""Chance"  << endl;
      player.position = 23;
    }
    else
    cout << "Your new balance is: " << player.money-50 << endl; //subtract 50 from balance. 
  }
}
Last edited on
Why do you have player1, player2; when you have a vector of Player?

Then, right now I want to say "player1.name"
Why not use players[0].name ?

I also would get rid of all the arrays and use only vectors.
Also consider to create a class Monopoly that wraps the player, positions and the game logic.
Finally an option for saving and loading the game might be nice.EDIT not relevant here
Last edited on
Topic archived. No new replies allowed.