如何在C ++(UNIX)中完全阻止用户输入

问题描述 投票:0回答:1

我正在一个项目上,我想在UNIX上的C ++中延迟输出。这是我正在谈论的示例:

cout << "You walk through the darkness, no sign of life in sight" << endl;
usleep(1000000);
cout << "What would you like to do?" << endl;
cin >> userCommand;

现在,如果用户在进行usleep时输入了某些内容,它将在稍后进入cin语句,尽管我可以给出适当的错误消息,但我不想处理。此外,有时我希望屏幕上显示某些内容,因此我不希望用户仅按住Enter键并清除屏幕。

是否有任何命令可以完全阻止用户输入甚至与屏幕进行交互?我可以在cin语句之前立即停用并在cin语句之后立即重新激活吗?

谢谢!

c++ unix input cin
1个回答
-2
投票

您可以使用

#include <stdio.h>
...
cout << "You walk through the darkness, no sign of life in sight" << endl;
getch();
cout << "What would you like to do?" << endl;
cin >> userCommand;

getch()块,直到您按下一个键

© www.soinside.com 2019 - 2024. All rights reserved.