Page 1 of 1

switch(string_variable)

Posted: 18 Feb 2012, 15:18
by mcsummation
The NXC guide has this statement "NXC also supports using string types in the switch expression and constant strings in case labels." However, I get "Error: Math Factor expected" when I used it that way:

Code: Select all

string sRead;
    switch(sRead)
    {
        case "ft":
            Convert = 12 * 25.4;
            break;
        case "in":
            Convert = 25.4;
            break;
        case "cm":
            Convert = 10;
            break;
        case "mm":
            Convert = 1;
            break;
        default:
            TextOut(0, LCD_LINE3, "Bad input file",DRAW_OPT_CLEAR_WHOLE_SCREEN);
            Wait(10000);
            Stop(true);
    }

Re: switch(string_variable)

Posted: 18 Feb 2012, 19:08
by afanofosc
I'm not sure what is going on in your case but your code compiles fine for me. Can you remind me what platform you are running on and what the version is of the IDE or compiler you are using?

Code: Select all

task main()
{
  int Convert;
string sRead;
    switch(sRead)
    {
        case "ft":
            Convert = 12 * 25.4;
            break;
        case "in":
            Convert = 25.4;
            break;
        case "cm":
            Convert = 10;
            break;
        case "mm":
            Convert = 1;
            break;
        default:
            TextOut(0, LCD_LINE3, "Bad input file",DRAW_OPT_CLEAR_WHOLE_SCREEN);
            Wait(10000);
            Stop(true);
    }
    
}
John Hansen

Re: switch(string_variable)

Posted: 18 Feb 2012, 19:38
by mcsummation
Platform = Windows XP SP3
BricxCC = 3.3.8.10
Compiler options: Use internal compiler; Enhanced firmware; NXT 2.0 compatible firmware; Automatic firmware version; Opt level 2

Fudge! It now compiles for me. There must have been something in the section of code above the switch statement that confused it. (I've made severe changes immediately above the switch.)