switch(string_variable)

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

switch(string_variable)

Post 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);
    }
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: switch(string_variable)

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: switch(string_variable)

Post 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.)
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests