gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:03:45 26 4
gpt4 key购买 nike

我正在从事一个项目,我希望在 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 语句之后立即重新激活的东西吗?

谢谢!

最佳答案

你可以使用

#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() 阻塞直到你按下一个键

关于c++ - 如何在 C++ (UNIX) 中完全阻止用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58249911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com