我是 c++ 的初学者,我需要创建一个应用程序在 10 秒后或按下一个键后打印一些东西,我试过这段代码但它不起作用(按下一个键后它会打印“a”很多 a 而不是一个 "a")
int i;
while(1)
{
i=1;
while(!kbhit()||i<1000)
{
Sleep(10);
i++;
}
cout<<"a";
}//while1
你能给我一些更好的建议吗?
谢谢
最佳答案
问题是您没有从缓冲区中取出第一个按下的键...
int i;
while(1)
{
i = 0;
while(!kbhit() && ++i<1000)
{
Sleep(10);
}
if (kbhit()) getch(); // to get the key out of the buffer, otherwise kbhit will keep getting true.
cout<<"a";
}//while1
关于c++ - 10 秒后运行代码或按下某个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13861509/