gpt4 book ai didi

c++ - 10 秒后运行代码或按下某个键

转载 作者:太空宇宙 更新时间:2023-11-04 12:01:43 26 4
gpt4 key购买 nike

我是 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/

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