Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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语言中使用fgets和strcmp_C_Fgets_Strcmp - Fatal编程技术网

在C语言中使用fgets和strcmp

在C语言中使用fgets和strcmp,c,fgets,strcmp,C,Fgets,Strcmp,我试图从用户那里获取字符串输入,然后根据输入的内容运行不同的函数 例如,假设我问“你最喜欢的水果是什么?”我希望程序根据他们输入的内容进行评论……我不知道该怎么做。以下是我目前掌握的情况: #include <stdio.h> #include <string.h> char fruit[100]; main() { printf("What is your favorite fruit?\n"); fgets (fruit, 100, stdin);

我试图从用户那里获取字符串输入,然后根据输入的内容运行不同的函数

例如,假设我问“你最喜欢的水果是什么?”我希望程序根据他们输入的内容进行评论……我不知道该怎么做。以下是我目前掌握的情况:

#include <stdio.h>
#include <string.h>

char fruit[100];

main() {
    printf("What is your favorite fruit?\n");
    fgets (fruit, 100, stdin);
    if (strcmp(fruit, "apple")) {
        printf("Watch out for worms!\n");
    }
    else {
        printf("You should have an apple instead.\n");
        }

}
#包括
#包括
炭果[100];
main(){
printf(“你最喜欢的水果是什么?\n”);
fgets(水果,100,标准食品);
if(strcmp(水果,苹果){
printf(“小心蠕虫!\n”);
}
否则{
printf(“您应该有一个苹果来代替。\n”);
}
}
当我运行程序时,无论我输入什么,它都不会执行else语句


谢谢你的帮助

strcmp
如果输入匹配,则返回0;如果左侧“大于”右侧,则某些值>0;如果输入匹配,则某些值
strcmp
返回0;如果左侧“大于”右侧,则某些值>0;如果
条件更改为以下条件:

if(strcmp(fruit,"apple") == 0)

strcmp
如果匹配,则返回0个字符串。应始终使用
=
运算符比较结果

if
条件更改为以下条件:

if(strcmp(fruit,"apple") == 0)

strcmp
如果匹配,则返回0个字符串。您应该始终使用
==
运算符比较结果

注意代码中的两件事:

  • fgets保留尾随“\n”。在与字符串“apple”进行比较之前,应将水果中关联的字符替换为“\0”
  • 当两个字符串相同时,strcmp返回0,因此应该根据您的意思更改if子句。(if子句中的水果和“apple”是等效的)
  • C main函数的标准用法是
    int main(){return 0;}
  • 修订守则:

    #include <stdio.h>
    #include <string.h>
    
    char fruit[100];
    
    int main() {
        printf("What is your favorite fruit?\n");
        fgets (fruit, 100, stdin);
        fruit[strlen(fruit)-1] = '\0';
        if (strcmp(fruit, "apple") == 0) {
            printf("Watch out for worms!\n");
        }
        else {
            printf("You should have an apple instead.\n");
        }
        return 0;
    }
    
    #包括
    #包括
    炭果[100];
    int main(){
    printf(“你最喜欢的水果是什么?\n”);
    fgets(水果,100,标准食品);
    水果[strlen(水果)-1]='\0';
    if(strcmp(水果,苹果)=0){
    printf(“小心蠕虫!\n”);
    }
    否则{
    printf(“您应该有一个苹果来代替。\n”);
    }
    返回0;
    }
    
    注意代码中的两件事:

  • fgets保留尾随“\n”。在与字符串“apple”进行比较之前,应将水果中关联的字符替换为“\0”
  • 当两个字符串相同时,strcmp返回0,因此应该根据您的意思更改if子句。(if子句中的水果和“apple”是等效的)
  • C main函数的标准用法是
    int main(){return 0;}
  • 修订守则:

    #include <stdio.h>
    #include <string.h>
    
    char fruit[100];
    
    int main() {
        printf("What is your favorite fruit?\n");
        fgets (fruit, 100, stdin);
        fruit[strlen(fruit)-1] = '\0';
        if (strcmp(fruit, "apple") == 0) {
            printf("Watch out for worms!\n");
        }
        else {
            printf("You should have an apple instead.\n");
        }
        return 0;
    }
    
    #包括
    #包括
    炭果[100];
    int main(){
    printf(“你最喜欢的水果是什么?\n”);
    fgets(水果,100,标准食品);
    水果[strlen(水果)-1]='\0';
    if(strcmp(水果,苹果)=0){
    printf(“小心蠕虫!\n”);
    }
    否则{
    printf(“您应该有一个苹果来代替。\n”);
    }
    返回0;
    }
    
    strcmp
    返回
    0
    (其计算结果为false)以指示两个字符串相等。阅读您试图使用的函数的文档是否真的有伤害?另外,不要使用
    main()
    使用
    int main(void)
    strcmp
    返回
    0
    (计算结果为false)表示两个字符串相等。阅读您试图使用的函数的文档是否真的会有伤害?另外,不要使用
    main()
    use
    int main(void)
    取而代之。如果字符串长度为
    arraysize-1
    ,它将被删除。即使如此,我也不会说换行符被删除,但可能会被保留或挂起。我应该在removes周围使用引号,它显然不是正确的词……实际上它保留了
    “\n”
    ,如果字符串足够小,可以容纳它,否则它会被删除没有。请注意,您对strcmp()的返回值的描述是错误的;如果相等,则返回值将为0(您也说过),但当第一个参数“小于”第二个参数时,返回值将仅为负值,不一定为-1;类似地,对于“更大”,返回值将为正值,不一定为+1。如果字符串长度为
    arraysize-1
    ,请查看它是否删除。即使如此,我也不会说新行已删除,但可能会保留或挂起ing.我应该在removes周围使用引号,它显然不是正确的词…..实际上,如果字符串足够小,可以容纳它,它会保留
    '\n'
    ,否则它不会。请注意,您对
    strcmp()
    的返回值的描述是错误的;相等时,值将为0(您也说过),但当第一个参数“小于”第二个参数时,返回值将仅为负值,不一定为-1;类似地,对于“更大”,返回值将为正值,不一定为+1。请参见上面的内容,因为fgets将“\n”作为字符串的结尾,因此将其替换为“\0”是正确的。上面的内容是正确的one,因为fgets将“\n”作为字符串的结尾,所以将其替换为“\0”是正确的。