gpt4 book ai didi

c - C 编程 : Exit endless loop if written inside

转载 作者:行者123 更新时间:2023-11-30 18:50:02 25 4
gpt4 key购买 nike

如果添加新关系,我需要退出此循环!只要它是空的或者按下“Enter”键,它就需要继续下去。

CODE:
static char input[100];

int main()
{
printf("Press Ctrl+c to Exit!\n");
while(1)
{
//Input (stdin) and output (stdout) declared in <stdio.h> library
fputs("esp>", stdout);
fgets(input,100,stdin);

if(input != 0) //here I can not change it !!!
return 1;
}
return 0;
}

输出:

Press Ctrl+c to Exit!
esp> (If "enter" is pressed) keep going
esp>
esp> s ( if something wrong is written, exit) else, keep going

最佳答案

如果您只想检查是否有空输入,请尝试此操作?

while(1)
{
fputs("esp> ", stdout );
fgets( input, 100, stdin );

if( input[0] != '\n' && strlen(input) )
{
// Do something with input
}
}

关于c - C 编程 : Exit endless loop if written inside,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41066496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com