Yet another "Press ENTER to continue"

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
void gamePlayBattle(user &x)
{
	cout << x.avatar.name << " has entered gamePlayBattle(you)\n";
	x.avatar.hp = x.avatar.level;
	cout << "Creating enemy..." << endl;
	enemy e;
	e.level = x.avatar.level + rand() % 3 - 1; //Enemy -1, same or +1 level.
	e.hp = e.level;
	cout << "ENEMY\n"
		<< "e.level: " << e.level << "\n"
		<< "e.hp: " << e.hp << "\n" << endl;
	
	cout << "Press ENTER to attack!\n";
	cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
	//cin.ignore();
	/*cin.clear();
	while ((cout << "Press ENTER to attack!") && (cin != ""))
	{
		cout << "That's not ENTER!\n";
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}*/
	/*int ch;
	do { printf("Press ENTER to attack!"); }
	while (( ch = getchar()) != '\n');*/

	int damage, edamage;
	int strikeCount = 0;
	while (e.hp > 0)
	{
		while ( x.avatar.hp > 0 && e.hp > 0 )
		{

1
2
3
		}
	}
}


As you can see, I have commented out all of the attempts that didn't work, and I've left only that which I've found as a suggestion here in the cplusplus forums, but that didn't work either.

1
2
	cout << "Press ENTER to attack!\n";
	cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


Please help!
How about:

1
2
cout << "Press ENTER to attack!" << endl;
while (cin.get() != '\n') {}
That won't help either.

THe problem is that somewhere else in your program, before you called the gamePlayBattle() function, you used a formatted input function, like:
1
2
3
  int age;
  cout << "How old are you? ";
  cin >> age;

Don't do that.

There is nothing wrong with using formatted input functions, but you must get all input from the user as everything he types up to and including the ENTER key. Remember,

The user always expects to press ENTER after every input.

There are several ways to fix this. The simplest might just to be:
1
2
3
4
5
  int age;
  cout << "How old are you? ";
  cin >> age;  // Get the input
  cin.clear(); // Ignore any errors
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );  // Get the EOL 

Here's a much more complicated example of how to get and verify input:
http://www.cplusplus.com/forum/beginner/13044/page1.html#msg62827

Here are some threads related to yours:
http://www.cplusplus.com/forum/unices/12772/
http://www.cplusplus.com/forum/beginner/397/
http://www.cplusplus.com/forum/general/6554/

And Zaitas article on Using cin to get user input
http://www.cplusplus.com/forum/articles/6046/

Hope this helps.
1
2
3
4
	cin.clear(); // Ignore any errors
	cin.ignore( numeric_limits <streamsize> ::max(), '\n' );  // Get the EOL 
	cout << "Press ENTER to attack!\n";
	cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


Not pretty, but this is working, for now.

Thanks!
Topic archived. No new replies allowed.