【问题标题】:C Programming - comparing a const char array to a user input [duplicate]C编程-将const char数组与用户输入进行比较[重复]
【发布时间】:2015-02-23 03:15:40
【问题描述】:

我不明白为什么这段代码会直接跳到模式 5。我已经看了好几遍了,只是没有看到。任何帮助将不胜感激。我猜这与我初始化数组的方式以及我比较它们的方式有关。我曾尝试使用“strcmp”,目前正在尝试比较直接数组位置。这两个都已成功编译,但我似乎无法让它工作。

    char one[3][3];
    const char *pattern[] = {"p1","p2","p3","p4","p5"};

    printf("%s Commands are {'p1', 'p2', 'p3', 'p4', 'p5'\n", prompt);
    printf("Enter your first pattern choice: ");
    scanf("%2s",one[0]);

    printf("Enter your second pattern choice: ");
    scanf("%2s",one[1]);

    printf("Enter your third choice: ");
    scanf("%2s",one[2]);

    for (int i = 0; i < 2; i++)
    {
        do{
            if (one[i] == "p1")
            {
                printf("\n1.1");
                patternOne();}
       else if (one[i] == "p2")
            {
                printf("\n1.2");
                patternTwo();}
       else if (one[i] == "p3")
            {
                printf("\n1.3");
                patternThree();}
       else if (one[i] == "p4")
            {
                printf("\n1.4");
                patternFour();}
       else
            {
                printf("\n1.5");
                patternFive();
       }

  }
while (i < 3);

【问题讨论】:

  • 你不知道如何比较字符串。查找strcmp()(然后删除这个问题)

标签: c arrays


【解决方案1】:

对于字符串比较,请使用来自string.hstrcmp() 函数。

您不是在比较 C 风格的字符串,因此它的计算结果为 else

您预期的代码可能是:

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

int main(int argc, char *argv[])
{
    char one[3][3];
    const char *pattern[] = { "p1", "p2", "p3", "p4", "p5" };

    printf("Enter your first pattern choice: ");
    scanf("%2s", one[0]);

    printf("Enter your second pattern choice: ");
    scanf("%2s", one[1]);

    printf("Enter your third choice: ");
    scanf("%2s", one[2]);

    for (int i = 0; i < 2; i++)
    {
        if (strcmp(one[i],"p1") == 0)
        {
            printf("\n1.1");
            patternOne();
        }
        else if (strcmp(one[i], "p2") == 0)
        {
            printf("\n1.2");
            patternTwo();
        }
        else if (strcmp(one[i], "p3") == 0)
        {
            printf("\n1.3");
            patternThree();
        }
        else if (strcmp(one[i], "p4") == 0)
        {
            printf("\n1.4");
            patternFour();
        }
        else if (strcmp(one[i], "p5") == 0)
        {
            printf("\n1.5");
            patternFive();
        }
        else
        {
            printf("Unknown input.");
        }
    }
    return(0);
}

所做的更改:

  1. 删除了内部 do-while 循环,因为 i 仅由 外部for 循环。由于i 没有在do-while 内递增 可能导致无限循环。
  2. 添加了一个 else if 语句来处理 p5 输入并添加了一个单独的 else 表示遇到了意外的输出。
  3. if else 条件替换为strcmp() 相等条件 并在文件中包含string.h

编辑(回答评论):

如果您希望它显示所有 3 个结果,请更改:

    for (int i = 0; i < 2; i++)

    for (int i = 0; i <= 2; i++)

目前,对于i &lt; 2,它会循环查找i = {0, 1},并在条件失败时跳过i = 2。如果将条件更改为i &lt;= 2,它将循环i = {0, 1, 2}

【讨论】:

  • 很棒的回复谢谢!那成功了。现在虽然它只显示输入的任何 3 个模式中的 2 个。关于为什么会这样的任何线索?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 2016-05-24
  • 1970-01-01
相关资源
最近更新 更多