You could do something like this:
#include <iostream>
#include <vector>
#include "Player.hpp" // Your header file for Player struct.
int main()
{
std::cout << "Enter player quantity: ";
unsigned int quantity;
std::cin >> quantity;
std::vector<Player> game_players(quantity);
//...
return EXIT_SUCCESS;
}
See constructors for std::vector
.
Edit 1 - Dynamic allocation
If you are not allowed to use std::vector
or must use arrays, this is one method:
int main()
{
std::cout << "Enter player quantity: ";
unsigned int quantity_of_players;
std::cin >> quantity_of_players;
Player * p_players = new Player[quantity_of_players];
//...
delete [] p_players;
return 0;
}
The above code fragment allocates the container of player in dynamic memory (a.k.a. heap), since the quantity is not known a compile time. The memory is deleted prior to returning back to the operating system.
Edit 2: Passing the players
You would pass the container of Players by using references:
void Print_Player_Info(const std::vector<Player>& players)
{
for (unsigned int i = 0; i < players.size(); ++i)
{
std::cout << "Player " << i << ":\n";
std::cout << " money: " << players[i].money << "\n";
std::cout << "\n";
}
}