C/C++ doesn't even support string datatype natively. What it uses are basically arrays of datatype char, terminated with a null character (basically equal to zero). In case of C++, there's a String class in the STL library, which is part of C++ standard, this is however still a library, therefore can't be mixed with the language construct themselves like that.
Keep in mind, that C/C++ are very close to machine language, they are more low level languages than other languages you may now, so they provide less level of abstraction - meaning what some language can do for you (like switch with a string), you have to do it for yourself, as it's something that can't be done directly on the most contemporary hardware.
C/C++ switch needs to be used with a number, which the processor can work with directly. So if you have for example this:
Code:
switch(a)
{
case 3:
...
break;
case 8:
...
break;
default:
...
}
It will get translated into something like this:
Code:
CMP 3, [a]
JNE case_8
...
JMP end_switch
case_8:
CMP 8, [a]
JNE case_default
...
JMP end_switch
case_default:
...
end_switch:
Notice the CMP (compare) instructions, that work with numbers. They can't work with a string, because even the most simple type of string is an array of arbitrary size in the memory and I can't remember any specific processor that could work with them natively.
So what is needed to do, is to somehow convert the string to a number. This can be done in C/C++ as following:
The Solution
You basically need to make an list of strings that you expect and make an enumeration with appropriate names for expected strings. Then define a function, that will translate the input string into the number, that corresponds with your enumeration - enumeration names correspond to a integer number, so they can be used with a switch.
This is a working C++ example, using standard C++. If you have any further questions, asks, feel free to alter it to suit your needs and use in any projects.
Code:
#include <iostream>
#include <istream>
using namespace std;
// List if strings you want to decode, in textual form
char *input_strings[] =
{
"COPY",
"MOVE",
"BURP",
"HUG",
"GLOMP",
"EXIT"
};
// List of corresponding names for strings you want to decode, MUST match intput_strings
enum decoded_string
{
strCOPY,
strMOVE,
strBURP,
strHUG,
strGLOMP,
strEXIT,
strTERMINATOR // MANDATORY! Must be always at the end of the list and must be always there
};
// decode function - goes trough input_strings and matches it to the input string
decoded_string DecodeString(char *input_string)
{
int i;
for(i = 0; i < strTERMINATOR; ++i)
if(!strcmp(input_string, input_strings[i]))
break;
return (decoded_string)i;
}
int main()
{
char input[256];
// infinite loop, not the best solution but... ah well :P
for(;;)
{
cout << "Enter command: ";
cin.getline(input, 256); // get the user input
// Tamtadadada! Using string with switch? Miracles? Nah, just Frooxius was here x3
switch( DecodeString(input) )
{
case strCOPY:
cout << "Copy what? Where?! I'm confused AAAAARGH\n";
break;
case strMOVE:
cout << "No. YOU move.\n";
break;
case strBURP:
cout << "Ewww, that's disgusting :-/\n";
break;
case strHUG:
cout << "YAAAAY! Hugs! ^^\n";
break;
case strGLOMP:
cout << "*glomps the user*\n";
break;
case strEXIT:
cout << "BYEEEEE\n";
return 0;
default:
cout << "I'm not sure what you mean by that...\n";
break;
}
}
}