cin.get needs to be pressed twice in loop

Hello!

Beginner here. I am working on my first real program. It is a reaction game and the user is told to press enter when an indication is displayed in the console.
Then the milliseconds it took to press enter shows up.

The problem is when the for loop runs again after the first level, the user needs to press enter twice after "Draw!".
Why does this happen? And any tips for how to fix it?
I have marked the line with an arrow.

(Sorry for the messy code. The lines got wierd with ctrl-c/ctrl-v)

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
#include <iostream>
#include <cstring>
#include <Windows.h>
#include <cstdlib>
#include <ctime>
#include "Game.h"
using namespace std;



void Game()
{
	int level = 1;
	
	for (int i = 0; i < 10; i++)
		{
	system("cls");

	cout << "Level " << level+i << "!" << endl << endl;

	cout << "When you see ''Draw!'' press enter do fire your gun. " << endl;
	Sleep(5000);
	cout << "Are you ready?" << endl << endl;
	srand(time(0));
	int tal = rand() % 10 + 3;
	Sleep(tal * 1000);

	cout << "Draw!" << endl << endl;
	long StartTime = GetTickCount();
			
	cin.get();         

	if (cin.get() != '\n')
	{
	cout << "I told you to press the enter key to shoot! Well, your dead." << endl << endl << "GAME OVER!";
	Sleep(3000);
	system("cls");
	return;
        }
	else
	cout << "-BAM!-" << endl;
	long EndTime = GetTickCount();
	long ReactionTime = (EndTime - StartTime);
	Sleep(2000);
	if (ReactionTime == 0)
	{
	cout << "You pulled you your trigger too soon. Cheating is never the answer." << endl << endl << "GAME OVER!";
	Sleep(3000);
	system("cls");
	return;

	}
	else

        cout << endl << "Your reaction time was " << ReactionTime << " milliseconds." << endl;
        long Survival = ((10-i) / 2)*1000 - ReactionTime;
	Sleep(2000);
	if (ReactionTime > Survival)
	{
	cout << endl << "You got hit first." << endl << endl << "GAME OVER!";
	Sleep(3000);
	system("cls");
	return;
	}
	else

	cout << endl << "You killed your enemy!";
	cout << endl << endl << "Press Enter for next level.";
	cin.get();

							
		}
	cout << "You completed the game";
	
	Sleep(3000);

	system("cls");
	
	return;

}
Where's int main()? Maybe you have another cin.get() there?
This is a function that is being called in a menu (Main).
And my arrow to show where the problem was disappeared. Sorry.
This is a function that is being called in a menu (Main).

So you get input in main, and leave a newline in the input stream which needs to be extracted before you get new input. That's a common mistake when mixing formatted and unformatted input extraction.
Thanks for the replies guys! So I have now been searching for an answer to fix this but I am not sure I understand completly. Do you maybe have any advice on how to make this work with only one press on the Enter-key?
Well at line 31 you have
 
    cin.get();

and then what I assume is probably the next line, you have
 
    if (cin.get() != '\n')

That looks like two consecutive uses of cin.get, requiring at least two keystrokes.

By the way your code didn't display properly because you put some hyphens in there somewhere like this:


// some code
// some code
// some code
---
 three dashes on previous line mark the end of your code

Last edited on
Topic archived. No new replies allowed.