Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按c中的特定键停止无限循环或进程_C_Key_Terminate - Fatal编程技术网

按c中的特定键停止无限循环或进程

按c中的特定键停止无限循环或进程,c,key,terminate,C,Key,Terminate,我刚刚想到了终端中的ctrl+c,正如您所知,它停止每个进程、更新、无限循环等。然后我开始编写一个代码,在无限循环中打印斐波那契序列,并尝试用特定的键停止它,但我不知道这是怎么可能的 有人能帮我解决这个问题吗 提前谢谢你,你可以这样做: #include <stdio.h> #include <conio.h> // include the library header, not a standard library int main(void

我刚刚想到了终端中的
ctrl+c
,正如您所知,它停止每个进程、更新、无限循环等。然后我开始编写一个代码,在无限循环中打印斐波那契序列,并尝试用特定的键停止它,但我不知道这是怎么可能的

有人能帮我解决这个问题吗


提前谢谢你,你可以这样做:

#include <stdio.h>
#include <conio.h>              // include the library header, not a standard library

int main(void)              
{
    int c = 0;             
    while(c != 'key that you want')             // until correct key is pressed
    {
        do {                
            //Fibonacci sequence
        } while(!kbhit());      // until a key press detected
        c = getch();            // fetch that key press
    }
    return 0;
}
#包括
#include//包含库标题,而不是标准库
内部主(空)
{
int c=0;
而(c!=“您想要的键”)//直到按下正确的键为止
{
做{
//斐波那契序列
}而(!kbhit());//直到检测到按键为止
c=getch();//取下按键
}
返回0;
}

Hy-man你可以这样做:

#include <stdio.h>
#include <conio.h>              // include the library header, not a standard library

int main(void)              
{
    int c = 0;             
    while(c != 'key that you want')             // until correct key is pressed
    {
        do {                
            //Fibonacci sequence
        } while(!kbhit());      // until a key press detected
        c = getch();            // fetch that key press
    }
    return 0;
}
#包括
#include//包含库标题,而不是标准库
内部主(空)
{
int c=0;
而(c!=“您想要的键”)//直到按下正确的键为止
{
做{
//斐波那契序列
}而(!kbhit());//直到检测到按键为止
c=getch();//取下按键
}
返回0;
}

使用do while可以工作,但也可以使用布尔值停止while循环

#包括
#包括
内部主(空)
{
bool running=true;
(跑步时)
{
如果(检查键按下(键))
运行=错误;
}
返回0;
}

使用do while可以工作,但也可以使用布尔值停止while循环

#包括
#包括
内部主(空)
{
bool running=true;
(跑步时)
{
如果(检查键按下(键))
运行=错误;
}
返回0;
}

请注意,函数
kbhit
getch
不是ISO C标准的一部分,而是特定于平台的扩展。例如,他们将在Microsoft Windows平台上工作。OP没有具体说明该问题适用于任何特定平台。因此,您不能依赖这些函数的可用性。@AndreasWenzel Yes that's True请注意,函数
kbhit
getch
不是ISO C标准的一部分,而是特定于平台的扩展。例如,他们将在Microsoft Windows平台上工作。OP没有具体说明该问题适用于任何特定平台。所以你不能指望这些功能可用。@AndreasWenzel是的,没错ISO C标准本身并不知道按键的概念。它只知道从流中读取输入的概念。因此,您需要特定于平台的扩展来检测是否按下了键。因此,如果您想获得问题的答案,则必须指定问题适用的平台(例如,通过设置适当的标签)。ISO C标准本身不知道按键的概念。它只知道从流中读取输入的概念。因此,您需要特定于平台的扩展来检测是否按下了键。因此,如果您想获得问题的答案,则必须指定问题适用的平台(例如,通过设置适当的标记)。