c++基于键盘输入的退出循环

标签 c++ loops

是否可以根据键盘输入退出 C++ 循环,而无需每次迭代都实际输入一些内容?

例如

while(checkkeyboardinput != 'q')
   {
     do work
   }

我觉得这很容易,但谷歌没有帮助我,我不记得如何做到这一点。感谢您的帮助。

编辑:我正在使用 VS2008

最佳答案

试试 _kbhit()。据我所知,它会检查缓冲区中是否有任何键盘输入在等待。

http://msdn.microsoft.com/en-us/library/58w7c94c%28VS.80%29.aspx

_kbhit

Checks the console for keyboard input.

int _kbhit( void );

Return Value

_kbhit returns a nonzero value if a key has been pressed. Otherwise, it returns 0.

Remarks

The _kbhit function checks the console for a recent keystroke. If the function returns a nonzero value, a keystroke is waiting in the buffer. The program can then call _getch or _getche to get the keystroke.

关于c++基于键盘输入的退出循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/695960/

相关文章:

c++ - 即使文件描述符不可用,也能有效地监听多个套接字

c++ - Visual Studio : Unresolved Token Error while linking CreateILockBytesOnHGlobal()

c++ - 在应用程序中禁用 Microsoft IME

javascript - 打印图案的功能

python为scipy中的约束动态创建字典

c++ - 带有用户和密码的文本文件

c++ - 文件记录的有效方法

iphone - iPhone AVAudioPlayer循环卡死了吗?

amazon-web-services - 是否可以在 Cloudformation 模板中使用循环和映射来使用不同的 CostFilter 服务制定多个 AWS 预算?

c - 使用 'for' 和 'while' 循环时出现问题