Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to use the Switch statement with strings for cases.

Posted on 2004-10-27
7
Medium Priority
?
229 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
Comment
Question by:galneweinhaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 800 total points
ID: 12426051
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
tinchos earned 600 total points
ID: 12426052
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
jkr earned 600 total points
ID: 12426072
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12426574
>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
ID: 12426664
Yes jaime, I know that

so?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12426792
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
ID: 12426874
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

609 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