繁体   English   中英

在不干扰c程序执行的情况下进行输入

[英]Taking input without disturbing the program execution in c

我想在不停止执行的情况下在循环中接受输入

`

   #include<stdio.h>
   #include<stdlib.h>
   #include<conio.h>
   int main()
   {
    char c;
      while(1)
      {

        if((c=getch())=='y')
                printf("yes\n") ;

        printf("no\n") ;
     }
      return 0;
   }

现在,我希望无论输入内容如何,​​都将无限打印“否”,如果我按y,则应该打印是。然后再次从否继续。 有可能吗,任何想法!

由于这似乎是在Windows上,并且您已经在使用旧的conio函数,因此可以使用_kbhit()来加倍使用:

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

int main()
{
  while(1)
  {
    if(_kbhit() && getch() == 'y')
      printf("yes\n");

    printf("no\n") ;
  }
  return 0;
}

根据文档_kbhit() “检查控制台是否有最近的击键”。 这意味着,如果_kbhit()为true,则getch()将能够立即获取一个字符而不会阻塞。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM