Strange program behavior...

I just re-coded a part of my program so it validates input and now I'm having trouble with one specific part of a function of mine. It's supposed to use getline() to get a string and store it in a variable... it should work fine, except that every time that function is called, it skips the prompt for the name and goes on to the next prompt... any idea what may have gone wrong?

The Function:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
void createPlayers(vector<player>& players, int numOfPlayers)
{
	int p_ID = 0; 
	int curr_HP = 0, max_HP = 0, surge_value = 0, surges_left = 0;
	int ac = 0, fort = 0, ref = 0, will = 0;
	int init_bonus = 0;
	string name, temp;

	for(int i = 0;i < numOfPlayers;i++)
	{
		p_ID = i; // Player's ID number.

		// Get the player's name.
		cout << endl;
		cout << "Enter player #" << (i + 1) << "\'s Name: ";
		getline(cin, name);
		cout << endl;

		// Get the player's health information.
		temp = "Enter " + name + "\s Current HP: ";
		curr_HP = grab<int>(temp.c_str());
		temp = "Enter " + name + "\'s Max HP: ";
		max_HP = grab<int>(temp.c_str());
		temp = "Enter " + name + "\'s Surge Value: ";
		surge_value = grab<int>(temp.c_str());
		temp = "Enter " + name + "\'s Surges Left: ";
		surges_left = grab<int>(temp.c_str());
		cout << endl;

		// Get the player's defense information.
		temp = "Enter " + name + "\'s AC: ";
		ac = grab<int>(temp.c_str());
		temp = "Enter " + name + "\'s Fortitude: ";
		fort = grab<int>(temp.c_str());
		temp = "Enter " + name + "\'s Reflex: ";
		ref = grab<int>(temp.c_str());
		temp = "Enter " + name + "\'s Will: ";
		will = grab<int>(temp.c_str());
		cout << endl;

		// Get the player's initiative bonus.
		temp = "Enter " + name + "\'s Initiative Bonus: ";
		init_bonus = grab<int>(temp.c_str());
		cout << endl;

		players.push_back(player(p_ID, name, curr_HP, max_HP, surge_value, surges_left, ac, fort, ref, will, init_bonus));

		cin.clear();
		cout.flush();
	}
}


Leading up to the function call in main (I suspect this is causing the problem):
1
2
3
4
temp = "How many players tonight? (include # of enemies): ";
	numOfPlayers = grab<int>(temp.c_str());

	createPlayers(players, numOfPlayers); // Create each player. 


The grab<int> function in my custom namespace:
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
template< typename T >
	T grab(const char * prompt)
	{
		T thing;
		std::istream::iostate old_state = std::cin.exceptions();
		std::cin.exceptions(std::istream::failbit);

		while(true)
		{
			try
			{
				std::cout << prompt;
				std::cout.flush();
				std::cin >> thing;
				std::cin.exceptions(old_state);

				return thing;
			} catch(std::istream::failure &e) {
				std::cout << "Invalid input. Try again." << std::endl;
				std::cin.clear();
				std::cin.ignore(1024, '\n');
			}
		}
		return thing;
	}
the problem is here:
1
2
3
std::cin >> thing;
std::cin.exceptions(old_state);
return thing;


after >> the \n is not extracted. so getline(cin, name); will only read the \n.

So how do I extract it?
Nevermind... I put a cin.get call right before the prompt.
Topic archived. No new replies allowed.