Or better : you don't even seem to need a switch - you can use an array of strings together with the enum :
Main Topics
Browse All TopicsHiello All,
Can we use a string in C++ switch case.
Can I pass a string to switch,
eg:
switch ( input )
{
Case ITEM_CRISPS:
printf("Walkers Cheese & Onion\n");
break;
case ITEM_CHOCOLATE:
cout << "Cadburys Dairy Milk\n";
break;
case ITEM_LEMONADE:
printf("Schweppes Lemonade\n");
break;
case ITEM_BISCUITS:
printf("McVities Digestives\n");
break;
default:
printf("Invalid selection\n");
}
I tried ,but in vain.Can anybody help me regarding this.
Is there any easy alternative approach to handle this.
Please provide an example if possible.
Many thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The enum und string array solution, Infinity showed could be improved:
enum ItemType
{
ITEM_INVALID = -1,
ITEM_CRISPS,
ITEM_CHOCOLATE,
ITEM_LEMONADE,
ITEM_BISCUITS,
ITEM_MAX
};
const char * szItemTypes[ITEM_MAX] =
{
"Walkers Cheese & Onion",
"Cadburys Dairy Milk",
"Schweppes Lemonade",
"McVities Digestives",
};
...
string input;
showMenu();
cout << "Make your choice: ";
getline(cout, input);
int i = 0;
for (; i < MAX_ITEM; ++i)
{
if (input == szItemsTypes[i])
{
switch(i)
{
case ITEM_CRISPS :
cout << "Walkers Cheese & Onion" << endl;
break;
case ITEM_CHOCOLATE :
cout << "Cadburys Dairy Milk" << endl;
break;
case ITEM_LEMONADE :
cout << "Schweppes Lemonade" << endl;
break;
case ITEM_BISCUITS :
cout << "McVities Digestives" << endl;
break;
}
}
if (i >= MAX_ITEM)
{
cout << "Invalid selection\n";
}
}
Remarks:
the 'typedef enum' is for C compatibility only. In C++ you can omit it and define it like a struct or class.
if you don't assign a number to enum constants, the constants were numbered automatically starting with 0 for the first constant with no value. That way the MAX_ITEM automatically counts the items - if always the last constant - and can be used for array size.
For static and const char arrays, the usage of std::string has no advantages. On the contrary you may experience that the debugger complains about memory leaks as the strings were freed at end of program.
I didn't use the ItemType as a type but an integer (int i). If the number was inputted or must be read from file or database, you can't guarantee at compile time that the number is a valid enum constant and would need to make a cast or even have an invalid enum.
Btw, to be honest, I wouldn't call this :
for (; i < MAX_ITEM; ++i)
{
if (input == szItemsTypes[i])
{
switch(i)
{
an improvement. Looping over all the items to find the one you need is a bit wasteful, while you might as well have sorted the strings, or placed them in a map, or ...
Everything can be improved ;)
>> For static and const char arrays, the usage of std::string has no advantages. On the contrary you may experience that the debugger complains about memory leaks as the strings were freed at end of program.
Actually, there is a more serious problem here in so far as a std::string is not a POD (Plain Old Data) type so it can throw an exception on construction, causing your application to crash without warning or any useful reason why -- this can be very hard to isolate. Stick to using C-Style strings for file/namespace scope variables.
Apart from that I am not seeing any improvement :)
Not quite sure why we need enums for this.
>> Not quite sure why we need enums for this.
I only mentioned enums to keep the code as close as possible to the code from the question (but using the enum rather than a string). I prefer the third solution I mentioned.
About the strings : I'd like to note that my idea was to load the strings from a file (containing all the articles for sale), which would put string literals out of the question.
The static array I used as example should be replaced by some other more appropriate datatype that is filled when the application starts.
To kunchesm : the code I posted here was just to quickly illustrate different approaches to this problem. The first two are not what I recommended - the third is ...
Business Accounts
Answer for Membership
by: Infinity08Posted on 2008-08-20 at 00:18:36ID: 22266859
>> Can I pass a string to switch,
No switches work on integer values only.
However, you could use an enum :
Select allOpen in new window