【问题标题】:Endles loop (while loop) runs until I press a key. Programming C无限循环(while 循环)一直运行,直到我按下一个键。编程 C
【发布时间】:2020-05-17 15:24:00
【问题描述】:

我想在 C 中创建一个代码,它运行一个循环并在我点击“1”但不按 ENTER 时中断循环。

我使用 GCC 编译器。

示例代码:

While-loop 必须始终不间断地运行,直到我按下 '1' 但没有按 ENTER ...(bin/stty raw)


int c=0;
int leave=0;
system ("/bin/stty raw"); 
while ((leave=getchar()) != '1'){
        c++;
        printf("\n- - while loop nr. %d\n",c);
        sleep(1);
}
system ("/bin/stty cooked");
printf("\n-- end of loop...\n");*
...

【问题讨论】:

  • Bitte, sprechen si English.
  • 嗨 KingOrgie,请用英语替换您的语言文本。
  • 你需要为此使用 ncurses
  • @P__J_ 很好,谢谢你的第一个......你能告诉我如何实现它吗?
  • 不——网上的例子太多了

标签: c while-loop getchar


【解决方案1】:

这是一个 C 常见问题解答,在此回答:http://c-faq.com/osdep/cbreak.html

问:如何在不等待 RETURN 键的情况下从键盘读取单个字符?如何阻止字符在输入时在屏幕上回显?

基本上,它依赖于系统和库。对于列出的各种系统,有几种可能的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    • 2021-05-26
    • 2022-01-12
    • 2011-06-10
    • 1970-01-01
    相关资源
    最近更新 更多