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?
galneweinhawAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.