Whether we can use like this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }

5th May 2017, 3:37 PM
GOKULNATH MADHIVANAN
GOKULNATH MADHIVANAN - avatar
2 Answers
+ 3
Unfortunately you cannot use switch clauses on strings nor char pointers in C++. Consider using if-else instead.
5th May 2017, 4:10 PM
Álvaro
+ 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 } ^
6th May 2017, 9:38 AM
mei
mei - avatar