Function for getting player names not working, suggestions?

I am trying to write a game(for my class) that can have between 1 to 4 players. I use a string for each players name. I pass the string by reference to the function to get the players names. I am attempting to use a for loop to go through each player and gather the names. When I execute the code and type in 2 players(the function is then called)

"Please enter the name of Player 1: Please Enter the name of Player 2: Please enter the name of Player 3:"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void getPlayerName(int players, string& player1, string& player2, string& player3, string& player4)
{
	for(int i = 0;i <= players; i++)
	{
	cout << "Please enter the name of Player " << i+1 << ": ";
	switch(i)
		{
		case 1:
			getline(cin,player1);
			break;
		case 2:
			getline(cin,player2);
			break;
		case 3:
			getline(cin,player3);
			break;
		case 4:
			getline(cin,player4);
			break;
		}
	}
}

What am I doing wrong?
Last edited on
The control variable on your switch statement varies from 0 to 3, but your case labels are from 1 to 4.

Try:
 
switch (i+1)

It still won't stop for each player and allow an input. It jumps straight to player 2 and will only allow player 2 to input a name.
I figured it out. A \n remained in the input stream from a previous input. I added in

"cin.ignore(1, '\n');"

Which cleared it up.
Topic archived. No new replies allowed.