c - 双循环的 kbhit() 不能很好地工作

标签 c loops goto stdio conio

只是为了好玩,我尝试用循环打印 kbhit() ,以便在按键后程序 无限打印该行,直到再次按下键盘。它编译得很好,运行时只会出现空白屏幕。没有打印品。但按一次按键就会结束程序。但控制台并未关闭。

#include <stdio.h>
#include <conio.h>

int main()
{
  while(1)
  {
    if(kbhit())
    {
      while(1)
      {
        if(kbhit())
        {
          goto out;
        }
        printf("Print Ed Infinitum Until Key Press");
      }
    }
  }
  out:
  return 0;
}

如何解决这个问题?

最佳答案

int main(void){
    while(1){
        if(kbhit()){
            getch();
            while(1){
                if(kbhit()){
                    getch();
                    goto out;
                }
                printf("Print Ed Infinitum Until Key Press\n");
            }
        }
    }
out:
    return 0;
}

关于c - 双循环的 kbhit() 不能很好地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17263037/

相关文章:

if-statement - 这个 If 语句是如何工作的?

c - 程序未在 C 中初始化外部变量

Python循环遍历2个列表

mysql - 如何在 MySQL 存储函数中使用 goto 标签

c - 变量和多线程的奇怪问题

c# - asp.net 中图像的 Foreach 循环

C++ ,这个 goto 语句是否有保证?

python - PyErr_Set* 两次

android - 用于 Android 和 C 代码开发的 IntelliJ

c - 将图标添加到 gcc 可执行文件并在终端中打开