I'm making a program where there is a function that asks how many players there are, and depending on the answer, it makes an array of classes that size. How can I access the array of classes from other functions? I thought I would just make it global, but how can I do that with it still being dependant on the answer the user gives? Here's my code: (If you run it, it will give you the error I need help solving)
/* ISU program created by Matt
in grade 12 programming. */
#include <iostream>
#include <list>
#include <iterator>
#include <vector>
using namespace std;
class humanPlayer
{
public:
humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum);
~humanPlayer(){};
void addToList(unsigned int countryToAdd) {countriesOwned.push_back(countryToAdd);}
void showList();
void removeFromList(unsigned int countryToRemove) {countriesOwned.remove(countryToRemove);}
void checkIfInList(unsigned int countryToCheck);
string getPlayerName() {return playerName;}
int getPlayerTurnNum() {return playerTurnNum;}
void setPlayerTurnNum(unsigned int newPlayerTurnNum);
private:
list<int> countriesOwned;
string playerName;
unsigned int playerTurnNum;
};
humanPlayer::humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum)
{
playerName = thePlayersName;
playerTurnNum = thePlayersTurnNum;
}
void humanPlayer::checkIfInList(unsigned int countryToCheck)
{
if(find(countriesOwned.begin(), countriesOwned.end(), countryToCheck) != countriesOwned.end())
{
cout << countryToCheck << " is in the list.\n";
}
else
{
cout << countryToCheck << " is NOT in the list.\n";
}
}
void humanPlayer::showList()
{
copy(countriesOwned.begin(),countriesOwned.end(),ostream_iterator<int>(cout,", "));
cout << endl;
}
void humanPlayer::setPlayerTurnNum(unsigned int newPlayerTurnNum)
{
playerTurnNum = newPlayerTurnNum;
}
void getName();
unsigned int numOfPlayers;
int main()
{
getName();
char nameTest;
for(unsigned int i=0 ; i<numOfPlayers ; i++)
{
nameTest = jailHouse[i].getPlayerName();
cout << nameTest << "\n";
}
system("PAUSE");
}
void getName()
{
vector<string> playerNames;
cout << "How many players will there be? ";
cin >> numOfPlayers;
cout << endl;
humanPlayer *jailHouse[numOfPlayers];
for(unsigned int i=0 ; i<numOfPlayers ; i++)
{
char name[256];
cin.ignore(255,'\n');
cout << "What is player " << (i+1) << "'s name? ";
cin.get(name,256);
jailHouse[i] = new humanPlayer(name, (i+1));
}
}
Thanks,
-Matt