Solved

How to use the Switch statement with strings for cases.

Posted on 2004-10-27
228 Views
Last Modified: 2010-04-01

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
Question by:galneweinhaw
    7 Comments
     
    LVL 55

    Accepted Solution

    by:
    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
     
    LVL 9

    Assisted Solution

    by:tinchos
    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
     
    LVL 86

    Assisted Solution

    by:jkr
    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
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    >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
     
    LVL 9

    Expert Comment

    by:tinchos
    Yes jaime, I know that

    so?
    0
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    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
     
    LVL 9

    Expert Comment

    by:tinchos
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    845 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    5 Experts available now in Live!

    Get 1:1 Help Now