-
array help
ok in this program i am making you can change the number of players that can play in the game. I would like to make it so that what ever the number of players you pick would change the size of the array that stores the names of the players.
heres some code:
Code:
int main()
{
int num_players = 2;
char player_names[5][15] = {" "}; // this holds the players names
char player1[15];
char player2[15];
char player3[15];
char player4[15];
char player5[15];
Heres The Player Selection Part:
Code:
cout << "Number Of Players: ";
cin >> num_players;
if (num_players == 2)
{
system("Cls");
cout << "Enter Player Names (15 Char. Max!)" << endl;
cout << "Player 1: ";
cin >> player1;
cout << "Player 2: ";
cin >> player2;
}
else if (num_players == 3)
{
system("Cls");
cout << "Enter Player Names (15 Char. Max!)" << endl;
cout << "Player 1: ";
cin >> player1;
cout << "Player 2: ";
cin >> player2;
cout << "Player 3: ";
cin >> player3;
}
else if (num_players == 4)
{
system("Cls");
cout << "Enter Player Names (15 Char. Max!)" << endl;
cout << "Player 1: ";
cin >> player1;
cout << "Player 2: ";
cin >> player2;
cout << "Player 3: ";
cin >> player3;
cout << "Player 4: ";
cin >> player4;
}
else if (num_players == 5)
{
system("Cls");
cout << "Enter Player Names (15 Char. Max!)" << endl;
cout << "Player 1: ";
cin >> player1;
cout << "Player 2: ";
cin >> player2;
cout << "Player 3: ";
cin >> player3;
cout << "Player 4: ";
cin >> player4;
cout << "Player 5: ";
cin >> player5;
thanks for the help.
-
Code:
cout << "How many players will be playing? ";
cin >> players;
char *playerNames = new char[players][50];
for (int i = 0; i < players; i++)
{
cout << "Player name " << i+1 << ": ";
cin.getline(playerNames[i], 49, '\n'); // Make sure no more than 50 is entered;
cout << endl;
};
Something like that would work.
-
it tells me it cant redefine it because there is differnt types of indirection.
-
...
Post your code
Post your error
What line does it point to?