[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

How to use the Switch statement with strings for cases.


Here is what I have:
            
char buffer[100];
fscanf(pOldFile, "%s", buffer);

switch(buffer)
{
case "[TEST]":
//do stuff
....

this doesn't work tho:
error C2051: case expression not constant

How can I make this work?
0
galneweinhaw
Asked:
galneweinhaw
  • 3
  • 3
3 Solutions
 
Jaime OlivaresSoftware ArchitectCommented:
you can't use switch / case for string.
Simplest alternative is to use strcmp with a chained if/else if:

char buffer[100];
fscanf(pOldFile, "%s", buffer);

if (!strcmp(buffer, "[TEST]")) {
        //do stuff
} else if (!strcmp(buffer, "[TEST1]")) {
        //do stuff
} else {      // default: equivalent
         //do stuff

}
0
 
tinchosCommented:
Hi

First of all, as far as I know a switch statement is only allowed on integer values.

So I guess that in order to make that with strings you should use an if - else structure

if( strcmp( buffer, "[test]" ) == 0 )
{
}
else if( )
{
}
else
{
}

and so on

From C++ primer

The value following the case keyword must be a constant expression of an integral type. The following, for example, result in compile-time errors:
// illegal case label values
case 3.14: // noninteger
case ival: // nonconstant

Hope this helps

Tincho
0
 
jkrCommented:
You cannot 'switch()' on strings. You need a different method. Either you can compare the strings like

char buffer[100];
fscanf(pOldFile, "%s", buffer);

if(!strcmp(buffer,"[TEST]")) {

//do stuff
}

or 'translate' them into numeric values using an array like

char* acTokens[] = { "[TEST]", "[TOKEN1]", "[TOKEN2]" ... NULL};

int LookupToken ( char* p)

int i = 0;

while ( NULL != acTokens[i]) {

    if (!strcmp(acTokens[i],p)) return i; // found token

    i++;
}

return -1;
}

}


My suggestion would be to use a std::map to look up the strings
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Jaime OlivaresSoftware ArchitectCommented:
>tinchos:
>So I guess that in order to make that with strings you should use an if - else structure
>if( strcmp( buffer, "[test]" ) == 0 )

It is exactly to:
    if( !strcmp( buffer, "[test]" ))

As if (a==0) is equal to if (!a)

0
 
tinchosCommented:
Yes jaime, I know that

so?
0
 
Jaime OlivaresSoftware ArchitectCommented:
Not for you. Just advicing questioner that we are talking about the same solution with a different presentation (Personally I think in this case is more legible to use ! operator, but not a functional difference).


0
 
tinchosCommented:
Ok.....

now I got it.....

yes, it all depends on how each of us find it clearer.

anyway, we agree that it is just a matter of "taste", as it does the same

Tincho
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now