+ 11
Whether we can use like this?
//This exists /*char c='a'; switch(c) { case 'a': break; case 'b':break; }*/ //But this?? char *c="hi"; switch(c) { case "hi": break; case "hey": break; }
2 ответов
+ 3
Unfortunately you cannot use switch clauses on strings nor char pointers in C++. Consider using if-else instead.
+ 2
..\Playground\: In function 'int main()':
..\Playground\:7:14: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
char *c="hi";
^
..\Playground\:8:9: error: switch quantity not an integer
switch(c)
^
..\Playground\:10:6: error: could not convert '"hi"' from 'const char [3]' to '<type error>'
case "hi": break;
^
..\Playground\:11:6: error: could not convert '"hey"' from 'const char [4]' to '<type error>'
case "hey": break;
^
..\Playground\: At global scope:
..\Playground\:14:1: error: expected declaration before '}' token
}
^