Error in conditional char in C language

1

I have a problem when trying to perform a conditional with a string, what I try is to ask the city first and depending on what the user writes, I do the conditional code, but it does not work, it takes me directly to else

#include "stdio.h"

int main (){
    char ciudad[0];

    printf("Digite el nombre de una ciudad: ");
    scanf("%s",&ciudad);

if (ciudad == "medellin"){
    printf("En este momento en %s esta lloviendo",ciudad);
}else if (ciudad == "bucaramanga"){
    printf("En este momento en %s esta haiendo un dia caluroso",ciudad);
}else{
    printf("%s no esta en nuestra base de datos",ciudad);
}

    return 0;
}
    
asked by ByGroxD 24.11.2017 в 01:35
source

1 answer

1

String comparison must be done using the strcmp ()

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

int main (){
    char ciudad[0];

    printf("Digite el nombre de una ciudad: ");
    scanf("%s",&ciudad);

//if (ciudad == "medellin"){    
if (strcmp(ciudad, "medellin") == 0){
    printf("En este momento en %s esta lloviendo",ciudad);
//}else if (ciudad == "bucaramanga"){
}else if (strcmp(ciudad, "bucaramanga") == 0) {
    printf("En este momento en %s esta haiendo un dia caluroso",ciudad);
}else{
    printf("%s no esta en nuestra base de datos",ciudad);
}

    return 0;
}
  

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

     
  • If the return value is 0, the first character that does not match has a greater value in str1 than in str2
  •   
    
answered by 24.11.2017 / 01:53
source