+ 4

Can you put a string oporation into a switch statement?

I'm working on a switch statement that would require a string operation input. Is this possible, and if so, how?

12th May 2018, 9:19 PM
JDLives
7 Respostas
+ 12
you could use enum?
12th May 2018, 10:07 PM
D_Stark
D_Stark - avatar
+ 11
It's not possible to use strings as case values because of how C++ supports strings. However, any switch can be replaced with a chain of if/else if/else, which will work the same way.
12th May 2018, 9:33 PM
Tamra
Tamra - avatar
+ 3
While I definitely recommend avoiding strings due to a whole plethora of reasons (case, encoding, etc), you can technically create a constexpr hash function and use that in your switch statement. Be wary of collisions if you take that route.
15th May 2018, 9:14 PM
Nemo
+ 2
Thank you, just trying to give an old If/Then code an upgrade
12th May 2018, 9:34 PM
JDLives
0
Yes, in PHO and Javascript
20th May 2018, 5:11 PM
Stan Berger
Stan Berger - avatar
0
PHP
20th May 2018, 5:11 PM
Stan Berger
Stan Berger - avatar
23rd May 2018, 4:13 PM
Nihal Sailor
Nihal Sailor - avatar