Link to home
Start Free TrialLog in
Avatar of galneweinhaw
galneweinhaw

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>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)

Avatar of tinchos
tinchos

Yes jaime, I know that

so?
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).


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