How do I prevent a user from inputting anything?

Hello fellow humans.

I want to be able to disable/enable the ability for the user to input anything to the console.

I'm working on a silly text adventure game and I want this so that I can prevent people from, for example, pressing enter before a prompt to press enter appears, so the game thinks you just pressed enter when in reality you pressed it a while ago.

Like this:
(The important comments are in all caps)

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
void Introduction0()
{
	cout << string(30, '\n');

	cout << " Hello!";

	Sleep(1250); //1250

	cout << endl << endl << " Welcome to a magical world... of *text*. Ohhhhhhhh fancy!";

	Sleep(2500); //2500

	cout << endl << endl;
	cout << " This is a text adventure game in which you're thrust" << endl;
	cout << " into a world full of magical creatures... also known" << endl; // USER PRESSES ENTER HERE ACCIDENTALLY
	cout << " as Chickemon.";

	Sleep(5000); //5000

	cout << endl << endl;
	cout << " Chickemon are a large diverse species of animals that" << endl;
	cout << " gallop harmoniously around the world.";

	Sleep(4500); //4500

	cout << endl << endl;
	cout << " Why don't you tell me a little about yourself?" << endl;
	cout << " Press enter to continue...";

	cin.ignore(); // GAME THINKS USER PRESSED ENTER HERE
	cout << string(30, '\n');

	cout << endl;

	return;
}


That's the kind of thing I want to prevent. While this isn't a huge problem, it is certainly something that would be useful, not just for this project

I don't know if that makes sense, but I hope it does.
Last edited on
Clear the keyboard input buffer
while(kbhit()) getch();

Requires conio.h
Excellent! Thank you so much.
I am not sure that will prevent ctrl+C or alt+tab or other such things. If you need to totally disable the keyboard, you probably need an operating system specific library and command. That is not critical to your need, but keep it in mind if the subject comes up. Some embedded software like the kiosks you see in stores do this to prevent people from abusing the systems (and some just unplug the keyboard).

Last edited on
Topic archived. No new replies allowed.