Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Switch !!

Status
Not open for further replies.

buddydavis

Programmer
Joined
Feb 7, 2003
Messages
6
Location
IN
Hey.... new to VC++...

I need to pass a string to a variable in the Switch statement but it keep giving me errors. Says somethign about conversion. here's what I'm trying to do :

switch (selected)
{
case "This is the launcher":
...
....
.....
break;

case "This is the steamer":
...
....
.....
break;

default :
...
....
.....
break;
}

I want to pass a String into the variable "selected" in the switch function. I just can't seem to get it right.

Thanks.
 
You're probably confusing the functionality of the switch statements in C with the flexibility of the Select Case statements in VB; the C variant only takes integer numbers.
Greetings,
Rick
 
And cases can only be compile-time constants.

Use of strings and floating points in switches may be put into the language soon, though.

Until then, just use if statements.
 
You could use a string array then use the array index for the switch statement

char *strArray[] = { "String 1", "String 2"};

for (int nLoop = 0; nLoop < sizeof(strArray) / sizeof(strArray[0]); nLoop++);
{
if (strncmp(&strArray[nLoop], strCheck, strlen(strCheck)) break;
}

switch(nLoop)
{
case 0: /* First String */
break;

.....

}

strCheck - The string that you would like to check against.
 
Use _lfind() in a string array, then switch()
 
**************************************************************
you can also try to do it like this:
Code:
enum { This_is_the_launcher, This_is_the_steamer } selected;

void main()
{
	switch (selected) {
	case This_is_the_launcher:
	   .....
	   ...
	   break;
	case This_is_the_steamer:
	   .....
	   ...
	   break;
	default :
	   .....
	   ...
	   break;
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top