0

In CPP, is the 'switch' condition only limited to inputted integer values OR are strings also acceptable?

7th Oct 2020, 5:37 AM
Not_in_Use
1 Resposta
+ 3
You cannot use a string literal with a switch in C++. It requires an integral type which a string is not. There are ways to work around this such as creating a hash from a string. A newer addition to the standard, constexpr, allows a more elegant way of doing this. Please refer to this for an example implementation: https://stackoverflow.com/questions/16388510/evaluate-a-string-with-a-switch-in-c I just threw this together so you can see it in action: https://code.sololearn.com/cJ4WIR52PLQf
7th Oct 2020, 5:55 AM
MaddieL
MaddieL - avatar