Problem: loop does not allow player ...

I have the following function in a program of mine. The function gives the player the option of indicating that he or she is ready to move on by typing in Yes or No. A while loop is ran (set to false by default), and while it is set to false, the player is given the chance to indicate whether he or she is ready to move on or not.

The problem is that the first time through the loop, the program does not allow the player to enter information into the string, causing the "Continue?" message to be printed twice. I can't seem to come to a fix to this problem. However, I will continue to look for one while you guys help me find the solution.

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
void Continue_Confirm() // confirm if player is ready to continue
{
	bool Choice_Confirm = false;// check if player is ready to continue





	while (!Choice_Confirm) // while player is not ready to continue, go through loop
	{
		string Continue_Confirm; // string to store player's input

		cout << endl;
		cout << "Continue? [Yes/No]: ";

		getline (cin, Continue_Confirm, '\n'); // store the input into "Continue_Confirm" string variable using getline function



		if (Continue_Confirm == "Yes" || Continue_Confirm == "yes" || Continue_Confirm == "YES" || Continue_Confirm == "yES")
		{
			Choice_Confirm = true;
		}
		else
			continue; // continue onto next iteration of loop

	}

}




Thanks.



- Sean
Last edited on
I'm thinking it has to do something with clearing up the queue in cin, however I have little knowledge of how to do that.

-- Problem is fixed by adding in cin.ignore(); before getline. However, this requires the player to press enter before the "Continue?" will show up the 2nd time the loop executes. Suggestions?
Last edited on
Bump, still trying to figure it out.
The code in the initial post works for me as is (Visual Studio 2005):

Continue? [Yes/No]: no

Continue? [Yes/No]: no

Continue? [Yes/No]: no

Continue? [Yes/No]: no

Continue? [Yes/No]: no

Continue? [Yes/No]: no

Continue? [Yes/No]: yes
Press any key to continue . . .


Maybe the problem is occuring because string Continue_Confirm; has the same name as the function?
How did you set up your compiler to have the "Press any key to continue..." ? I don't have that. Maybe it's the way my compiler is set up. I have Visual C++ 2010 Express, and my project is in a Console Application.
I changed the function name, error still occurs. However if I put cin.ignore(); at the beginning of the while loop, the 1st loop is fine. After that, the player has to press enter in order for the program to show the "Continue?" message.
However if I put cin.ignore(); at the beginning of the while loop, the 1st loop is fine.

Are you doing any console input before this function is called? I just called the function straight up in main(). From this, it would seem that cin has not been cleared before calling Continue_Confirm().

You could put cin.ignore( std::numeric_limits<std::streamsize>::max() ); before the loop (and remember to #include <limits> ). However, this would cause the user to have to press enter if cin was already clear to begin with. Again, I suspect the problem is in the code that you didn't post. My guess is a call to cin>> somewhere is leaving a newline in the stream.
Yeah, this function is a part of a much bigger project that uses a lot of user input, cout << output etc.

I put the cin.ignore(); before the loop as you said, and added #include <limits> . It works fine for the first loop iteration, but for the rest the player has to press enter in order to get the "Continue?" to pop up.
Does your function look like this?

1
2
3
4
5
6
7
8
9
10
11
void Continue_Confirm() // confirm if player is ready to continue
{
	bool Choice_Confirm = false;// check if player is ready to continue

         cin.ignore( std::numeric_limits<std::streamsize>::max() );

	while (!Choice_Confirm) // while player is not ready to continue, go through loop
	{
             ...
	}
}


The user should only have to press enter if the call to cin.ignore(); is within the loop. With the code as above, the user should only have to press enter upon entry to the function if cin is not clear.

And as I said, the above "solution" is just a band-aid. The actual issue is that cin is not cleared at some point in your other code.
Last edited on
Mmm, so you're saying that every time I get input using cin, it has to be cleared out with cin.ignore();? If that's the issue it'll take some time to go through and clear cin after each input, however I can do that.
PROBLEM FIXED! Booyah. Clearing cin after each occurrence of taking input with cin fixed the problem. I removed the cin.ignore(); from the above function, as well.

Thanks Shack



- Sean
Topic archived. No new replies allowed.