member functions passing variables

Hello all, writing code for a game, and am trying to ask the user if they want to play vs another player or computer. If against a player, move on to p_v_p. When I do this, it skips past "Player 1 name:" and goes straight to "Player 2 name:"... can't figure out what the problem is. It works except for this snippet.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using namespace std;

void Draw::opponent(int &opp)
{
	cout << "Who do you want to play against?\nEnter the number corresponding to your choice.\n";
	cout << "1. Player\n";
	cout << "2. Computer\n";
	cin >> opp;
	cout << endl;
	if (opp != 1 && opp != 2)
	{
		cout << "Please choose a valid choice.\n";
		opponent(opp);
	}
	if (opp == 1)
		p_v_p(player1, player2);
}

void Draw::p_v_p(string &player1, string &player2)
{
	cout << "Player 1 name: ";
	getline(cin, player1);
	cout << "Player 2 name: ";
	getline(cin, player2);
	cout << endl;
	cout << "To choose a square, type in the corresponding number.\n";
}
Last edited on
Did you use >> operator before calling that function? If so, you fall into most common traps in input processing: http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input
http://stackoverflow.com/questions/9336209/mixing-ifstream-getline-and
you are amazing, thanks!!
Topic archived. No new replies allowed.