LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

case selector of string

Is it possible to make a switch with a string as it is in LabVIEW.

I want tor read a string from a File and then make my selection e.g.

string = "Laden"
switch (string)
{
case "Laden":
//do anything
break;
default:
break;

}
0 Kudos
Message 1 of 4
(3,996 Views)
Ted,

According to the ANSI C/C++ standard, the switch statement can only take integers and single characters. In other words, switch will only work with int and char types, not with strings.

Azucena
0 Kudos
Message 2 of 4
(3,996 Views)

What of converting the strings to integers before doing the switch/case?

or what would be a good work around for something like the following?...

 

switch (atoi(myString)){

  case (atoi("myCase")):

    some code here;

    break;

  case (atoi("mySecondCase")):

    some code here;

    break;

}

 

0 Kudos
Message 3 of 4
(3,582 Views)

Unfortunately your code cannot work: atoi converts the string representation of a number into its numerical equivalent. That is: atoi ("123") = 123; atoi ("myString") = 0!

 

In this environment the closer equivalent of a switch is a series of if:

 

if (!strcmp (myString, "myCase")) {

    some code here;

}

else if (!strcmp (myString, "mySecondCase")) {

    some code here;

}



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 4
(3,574 Views)