So basically I'm making a short version of Monoply. And I need to make a vector of a struct to contain all the info for multiple players that the user enters that also contains the properties owned by each player. I have a function to create x amount of players and all assign each player with the starting cash. However, when I compile this code, I get the error libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector
. What am I doing wrong? Thank you!
void createPlayers(Board b, Rules r, int players)
{
for(int i = 0; i < players; i++)
{
b.listOfPlayers.at(i).cash = r.startCash;
}
for(int i = 0; i < players; i++)
{
cout << b.listOfPlayers.at(0).cash;
}
}
typedef struct Player_Struct
{
int cash;
vector<char> propertiesOwned;
} Player;
typedef struct Board_Struct
{
vector<char>listOfProperties;
vector<Player_Struct> listOfPlayers;
} Board;
Is this even a good way to attack the problem? The number of players is up to the user and I can't initialize a certain number of player_structs in the struct initialization. My thinking is to create a vector list of all the players, then be able to draw information out of each player in the vector list. Am I on the right track, logically at least, even though my code is really bad? I'm still new to C++ just switched from C.