C 中的条件语句和数组(初学者)

标签 c arrays io conditional-statements

<分区>

Possible Duplicate:
How do I properly compare strings in C?

虽然我已经讨论这个主题好几个月了,但我对 C 还是比较陌生。我正在尝试编写一个简单的问题/响应程序。我知道它与 if else 条件有关(其他一切都有效),但我已经搜索过但似乎找不到问题所在。最后还有重复程序的递归。我放入其中的函数调用可能是错误的。

#include <stdio.h>

main()
{
    char line[100];
    char decision[100];
    printf("Are you gonna throw it?\n");
    printf("Type yes or no.\n");

    scanf("%s", line);
    printf("%s \n", line);

    if (line == "yes") {
        printf("Thanks.\n");
    } else if (line == "no") {
        printf("Why not?\n");
    }

    printf("Do you want to do this again?\n");
    scanf("%s", decision);
    if (decision == "yes") {
        main();
    };
}

最佳答案

line == "yes" 这样的比较不起作用。 您需要使用 strcmp 比较您的字符串,例如

if (strcmp(line, "yes") == 0) {
    printf("Thanks.\n");
} else if (strcmp(line, "no") == 0) {
    printf("Why not?\n");
}

关于C 中的条件语句和数组(初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12569765/

相关文章:

c - 在 C 中将数组存储为链表

python - 使用 2 个索引列表对 2D Numpy 数组进行索引

c# - 寻找 C# 中不同类型用户输入的数组列表声明的良好实践

java - Apache Parquet 无法读取页脚 : java. io.IOException:

javascript - 按频率过滤输入(开关去抖)

c - 查找最大数字在数组中出现的次数

c - 初始化元素不是常量

c - 从 C 程序中使用 $EDITOR 变量打开文件进行编辑

c - 有符号整数右移

java - 迭代时如何处理数组中的 null