About switch-case (C++) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

About switch-case (C++)

Can i put string with cases. For example switch(var) { case "hello" : cout << "Hello" ; break ....

16th Feb 2020, 2:41 PM
Edward
Edward - avatar
3 Answers
+ 9
Not in C/C++, at least not directly, because strings aren't considered integral values. https://en.cppreference.com/w/cpp/language/switch "... any expression of integral or enumeration type, or of a class type contextually implicitly convertible to an integral or enumeration type... "
16th Feb 2020, 2:56 PM
Hatsy Rei
Hatsy Rei - avatar
+ 5
Additional info: Is there a workaround? Yes, such as hashing. https://stackoverflow.com/questions/2111667/compile-time-string-hashing
16th Feb 2020, 2:59 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Thanks for inf
16th Feb 2020, 2:57 PM
Edward
Edward - avatar