Here I have an array of Agents, and I want to initialize an array of Players. An agent has just his name. The user must input the player name, jersey number, and player's agent. In this case, I wanted multiple players to be able to have the same agent, so I used a pointer.
// Default constructor = Agent(std::string = "")
Agent agents[2] = {"Larry", "Joe"};
// Default constructor = Player(std::string = "", int = 0, Agent* = 0)
Player players[3];
initializePlayers(players, 3);
void initializePlayers(Player players[], int playerSize)
{
string playerName, agentName;
int playerNum;
Agent *myAgent;
for(int i = 0; i < playerSize; i++)
{
cout << "Please enter the player's name: ";
getline(cin, playerName);
cout << "Please enter the player's number: ";
cin >> playerNum;
cout << "Please enter the player's agent: ";
getline(cin, agentName);
cin.ignore(1000, '\n');
// If the agent's name matches one of the names in agents array
// assign that agent to this player
Player tempPlayer(playerName, playerNum, myAgent);
players[i] = tempPlayer;
}
}
Inside my comments, I need to assign myAgent. For example, if the user enters "Larry" for the first player, Larry should be his agent. If the user enters "Joe" for the next two players, they should both have Joe as their agents. How do I accomplish this? Even an idea to get me started would help. Thank you.