小编Alb*_*ert的帖子

检查项目是否在C中的数组中

我正在检查一个项目是否在C中的数组中,使用for循环遍历数组中的每个项目并将其与用户输入一一比较。

int main()
{
    char birds[] =
    {
        [0] = "a",
        [1] = "b",
        [2] = "c",
        [3] = "d",
        [4] = "e",
        [5] = "f",
        [6] = "g",
        [7] = "h"
    };
    int birdfound;
    int i;
    printf("Enter bird:");
    scanf("%c", &birdfound);
    //printf("%c", birdfound);
    for(i=0; i<8; i++)
    {
        //printf("Y");
        if(birdfound == birds[i]){
            printf("Bird in array, found at position %d\n", i);
        }
    }
    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道,问题出在分支逻辑之内,因为某种原因,它无法将字符输入与数组中的任何字符进行比较。因此,输出为空,程序仅结束。

c arrays string char

-2
推荐指数
1
解决办法
62
查看次数

标签 统计

arrays ×1

c ×1

char ×1

string ×1