How to Create a replacement for the switch statement in C++ that also works for strings | Sololearn: Learn to code for FREE!

+1

How to Create a replacement for the switch statement in C++ that also works for strings

I need a solution

5/30/2017 7:06:20 AM

Kibrom

5 Answers

New Answer

+7

Switch also works for characters, and strings are character arrays. There's probably a way to make things work, but it will depend on the case of your program and the strings you want to evaluate.

+5

Maybe you can try marking each string with a unique number, and then check for the number using switch. That number may be : 0) Sum of All ASCII Codes of the characters in the string. 1) Sum of First Char's ASCII Code + Length of the string. etc,etc...

+3

convert the string to a number maybe?

+3

@Hatsy Rei Yes but characters are just numbers. It wouldnt make sense to go through the trouble instead of just using a simple if statement

+2

You have to use if statements. Switches are only for numbers, not entire strings