Switch statement using string on an array

前端 未结 8 1422
礼貌的吻别
礼貌的吻别 2021-01-13 06:28
#include

int main(){

    char name[20];

    printf(\"enter a name \");
    scanf(\"%s\",name);
    switch(name[20]){
        case \"kevin\" : 
             


        
8条回答
  •  伪装坚强ぢ
    2021-01-13 07:11

    Switch statements work on int values (or enum), but not on char arrays.

    You could do

    if (strcmp(name, "kevin")==0) {
        printf("hello");
    }
    else if (strcmp(name, "Laura")==0) {
        printf("Allo");
    }
    else if (strcmp(name, "Mike")==0) {
        printf("Good day");
    }
    else  {
        printf("Help!");
    }
    

提交回复
热议问题