Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.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_User Input - Fatal编程技术网

在C语言中用空字符串终止程序

在C语言中用空字符串终止程序,c,user-input,C,User Input,所以,我对C非常陌生,我试图在一个空字符串上终止我的程序(当用户按enter键时,没有输入任何内容),但出于某种原因,它给了我一个无限循环 这是我的密码: while(input[0] != '\0') { if(slot < 27 && slot >= 0) { struct LinkedList curr = files[slot]; if(strcmpci(input, curr.val) == 0) {

所以,我对C非常陌生,我试图在一个空字符串上终止我的程序(当用户按enter键时,没有输入任何内容),但出于某种原因,它给了我一个无限循环

这是我的密码:

while(input[0] != '\0') {
    if(slot < 27 && slot >= 0) {
        struct LinkedList curr = files[slot];
        if(strcmpci(input, curr.val) == 0) {
            printf("%s, \n", curr.val);
        }

        while(curr.next != NULL) {
            curr = (*(curr.next));
            if(strcmpci(input, curr.val) == 0) {
                printf("%s\n", curr.val);
            }
        }
    }
}

所以我很困惑。

当用户按enter键时,他们不是在输入空字符,而是在输入换行符。尝试检查
\n

input
在您的循环中实际上从未更改,因此它
input[0]!='\0'
第一次通过循环,那么第二次、第三次和第710143次将是相同的。我将
\0
更改为
\n
,它仍然给我一个无限循环。对于发生这种情况的原因仍然非常困惑。scanf语句是什么样子的
scanf(“%s”,&input)好的,我现在就试试。感谢您的帮助和时间。上述代码将起作用,但是如果用户不按enter键,您将无法访问他们输入的内容,请确保您已记录
while(____ != '\0') {
}