如果 - 其他程序

时间:2016-04-26 10:03:18

标签: c string if-statement

嗨我这里有这个代码尝试询问用户有关位置的信息。当用户输入位置时,它只是说再见。我的代码出了什么问题?

#include<stdio.h>

int main ()
{

        char location[15];
        printf("1:Greencourt\n");
        printf("2:Corianthans\n");
        printf("3:Shop\n");
        printf("Enter a location\n");
        scanf("%s",location);
        if(location=="Greencourt")
        printf("Bisleri bottle cost 25 rupees\n");
        else
        if(location=="Shop")
        printf("Bisleri bottle cost 15 rupees\n");
        else
        if(location=="Corianthans")
        printf("Bisleri bottle cost 50\n");
        else
        printf("Bye");

        return 0;
}

这是输出

1:Greencourt
2:Corianthans
3:Shop
Enter a location

Shop

Bye

2 个答案:

答案 0 :(得分:3)

您正在比较您不能使用的字符串(char数组)&#39; ==&#39;运营商。 您应该使用strcmp功能:

int strcmp(const char *str1, const char *str2)

这样:

if(strcmp(location,"Greencourt") == 0)

答案 1 :(得分:1)

最好将值与strcmp()

进行比较
相关问题