我刚刚写了这个小程序,它应该等待用户在打印每一行之前输入一些东西,但它只适用于第一个 _kbhit(),之后它不再等待。这是为什么?
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
printf("Canada\n");
while ( _kbhit() == 0 );
printf("is\n");
while ( _kbhit() == 0 );
printf("great!");
while ( _kbhit() == 0 );
return 0;
}
function reference 中没有信息_kbhit() 在一个程序中只工作一次。
最佳答案
虽然在该文档页面中没有明确说明,但您必须使用击键(使用 getch
或 getche
),否则 _kbhit
仍然会看到它。在下一个 while 循环之后调用 _getch
:
while(_kbhit() == 0);
_getch();
// _kbhit can now be called again
关于c++ - 为什么 _kbhit() 在 C 程序中只工作一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19435255/