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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes jaime, I know that
so?
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
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
>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)