满意答案
路见不平badao
推荐于 2017.09.14
采纳率:52% 等级:6
已帮助:908人
摘自MSDN:
--------------------------------------------------------------------------------
kbhit() Does Not Take Character out of Buffer
Article Last Modified on 03-26-2002
The information in this article applies to:
The C Run-Time (CRT)
Microsoft C for MS-DOS
Microsoft C for OS/2
Microsoft C/C++ for MS-DOS
Microsoft Visual C++ for Windows, 16-bit edition 1.0, 1.5
Microsoft Visual C++, 32-bit Editions 1.0, 2.0, 4.0, 5.0, 6.0
Microsoft Visual C++ .NET (2002)
--------------------------------------------------------------------------------
NOTE: Microsoft Visual C++ NET (2002) supported both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.
Summary
When an application tests the value returned by the kbhit() function, the application must explicitly remove the character from the keyboard buffer after entering the keystroke. Otherwise, kbhit() continues to return TRUE.
The following code example demonstrates the situation:
Sample Code
/*
* Compiler options needed: None
*/
#include
void main(void)
{
while (!kbhit())
; // Waits for keystroke
while (kbhit())
getch(); // Empties buffer
while (!kbhit())
; // Waits for keystroke
}
kbhit()不会清空缓冲区内容,一旦有键盘事件,必须使用getch()清空缓存,至于为什么cin.sync();会不能清空缓存我也不太清楚,可能与编译器有关
20分享举报