array help

This is a discussion on array help within the C++ Programming forums, part of the General Programming Boards category; ok in this program i am making you can change the number of players that can play in the game. ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    77

    Question 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.
    Hooked On Phonics Didn't Work For Me!

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    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.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    it tells me it cant redefine it because there is differnt types of indirection.
    Hooked On Phonics Didn't Work For Me!

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    ...
    Post your code
    Post your error
    What line does it point to?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21