的kbhit()与双回路工作不正常 [英] kbhit() with double loop not working well

查看:167
本文介绍了的kbhit()与双回路工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是为了好玩,我试着打印的kbhit()与循环,使按键preSS后程序
打印无限直到$ P $再次pssed键盘就行了。它编译良好,运行的时候,只给空白屏幕。打印编号。但在单个键preSS结束程序。控制台不关闭,虽然

 的#include<&stdio.h中GT;
#包括LT&;&CONIO.H GT;诠释的main()
{
  而(1)
  {
    如果(的kbhit())
    {
      而(1)
      {
        如果(的kbhit())
        {
          转到出来;
        }
        的printf(打印版无穷,直到按下preSS);
      }
    }
  }
  出:
  返回0;
}

我该如何解决这个问题?


解决方案

  INT主要(无效){
    而(1){
        如果(的kbhit()){
            残培();
            而(1){
                如果(的kbhit()){
                    残培();
                    转到出来;
                }
                的printf(打印版无穷,直到按下preSS \\ n);
            }
        }
    }
出:
    返回0;
}

Just for fun, I tried printing kbhit() with loops, so that the program after a key press prints the line infinitely until pressed keyboard again. It compiles well and when run, just gives blank screen. No prints. But upon single keypress ends the program. The console does not close though.

#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;
}

How do I solve this?

解决方案

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;
}

这篇关于的kbhit()与双回路工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆