switch case problem
I'm writing a console application and using (Console::ReadLine() ) to get what user writes.
there are a lot of possible commands I'm building so I decided to use switch statement;
switch(Console::ReadLine()){
case "EXAMPLETEXT":
...
gives me the error that using System::String^ value type is illegal in a switch statement. Should I convert it to a char array or what? I'm stuck with this. It says only integral values are accepted in switch statement.
(VS2005, VC++)
[521 byte] By [
can16358p] at [2007-11-11 8:33:15]

# 1 Re: switch case problem
Believe what it says: only integral data types are accepted. This means you can use a char, an int, or even an enum, but you can't use a char array or a string or a float or double. If you want to use a switch for this, you'll need to do some interpretation of the string ... otherwise, use an "if" statement.
nspils at 2007-11-11 21:01:19 >

# 2 Re: switch case problem
case (strcmp("exampletxt",str) == 0) //booleans are integeral as well
jonnin at 2007-11-11 21:02:13 >

# 3 Re: switch case problem
Hi,
why don't you use a "waterfall-if", the functionality translates directly to
a switch statement and it's uniform:
if(!strcmp("exampletxt",str)) // case "exampletxt":
{
...
}
else if(!strcmp("example2txt",str)))) // case "example2txt":
{
}
else if(!strcmp("example3txt",str)) // case "example3txt":
{
}
else // case default:
{
}
Cheers,
D
# 4 Re: switch case problem
case (strcmp("exampletxt",str) == 0) //booleans are integeral as well
This will not work since the case label must be an integral constant expression (i.e., evaluated at compile time).
The best solution is to define an enumeration with symbolic names. Comparing strings is way inefficient, and could lead to many other bugs (what about case sensitibity for example?).
Danny at 2007-11-11 21:04:18 >

# 5 Re: switch case problem
Missed that, I do not use switch statements often because of the required break & generally just use if statements, which are easier to read (reader does not have to track the missing break fall throughs, creator does not have to "force" everything into some integer type, etc).
jonnin at 2007-11-11 21:05:22 >
