is it possible to have different switch case values other than integers? such as characters or strings, if so how could this be done.
thanks,
is it possible to have different switch case values other than integers? such as characters or strings, if so how could this be done.
thanks,
Single characters, yes, since they're just integral constants, too.
Other than that, no.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
thanks,
Enumerations as well.
Another type of value is in windows programming. Not really sure what it is, but it may be somthing to do with destroying the open window. Or in english, close the window.
Double Helix STL
There is a way you can use lookup tables to resolve a string (or any other type for that matter) to an int - have a look at this example using weekdays..
The get_day() function resolves the string to an integer, which happens to be the position of the day in the lookup table.Code:#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cctype> enum { DAY_MONDAY, DAY_TUESDAY, DAY_WEDNESDAY, DAY_THURSDAY, DAY_FRIDAY, DAY_SATURDAY, DAY_SUNDAY, INVALID }; const std::string days_of_week[] = { "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" }; int get_day(std::string str) { typedef std::vector<std::string>::iterator iter_type; static std::vector<std::string> lookup_table( days_of_week, days_of_week + (sizeof days_of_week/sizeof *days_of_week) ); std::transform(str.begin(), str.end(), str.begin(), tolower); iter_type it ( std::find(lookup_table.begin(), lookup_table.end(), str) ); if( it != lookup_table.end() ) return ( it - lookup_table.begin() ); return INVALID; } int main() { std::string test_input("WEDNESDAY"); switch( get_day(test_input) ) { case DAY_MONDAY: std::cout << "The day is monday"; break; case DAY_TUESDAY: std::cout << "The day is tuesday"; break; case DAY_WEDNESDAY: std::cout << "The day is wednesday"; break; case DAY_THURSDAY: std::cout << "The day is thursday"; break; case DAY_FRIDAY: std::cout << "The day is friday"; break; case DAY_SATURDAY: std::cout << "The day is saturday"; break; case DAY_SUNDAY: std::cout << "The day is sunday"; break; default: std::cout << "Invalid day"; } }
The one drawback with this is that the enums must always match the lookup table, since the enums represent the position of each day in the lookup table.
the get_day() function does several things..
first it creates the days_of_week lookup table in the form of a std::vector<std::string>
secondly it converts the input into all lowercase (because the lookup table is all lowercase)
thirdly it searches the lookup table for a match to the string
and finally, it calculates the string's position (relative to the beginning of the lookup table), and returns that.
if the find() algorithm didn't get a match (it hit the end of the lookup table), then the get_day() function returns the value associated with INVALID.
Last edited by Bench82; 12-27-2006 at 08:06 PM.
Sure. I might start with something like this, though.
Code:// remove INVALID from the enum size_t get_day(std::string str) { // no changes return static_cast<size_t>(-1); } void foo(const char *text) { std::string test_input(text); size_t index = get_day(test_input); if ( index < sizeof days_of_week / sizeof *days_of_week ) { std::cout << "The day is " << days_of_week[get_day(test_input)]; } else { std::cout << "Invalid day"; } std::cout << '\n'; } int main() { foo("WEDNESDAY"); foo("BOBSDAY"); }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
There was a recent article on thedailywtf.com where someone precalculated the hashes of the set of expected strings and then did a switch statement on the hash of the user-supplied string.
Most unusual I would have to say, and you certainly wouldn't want to choose a poor hashing algorithm. It definitely qualifies as premature optimisation.
Or maybe even use a std::map to translate from std::string to some enumeration of your choice.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
thanks Salem, youv given me a few ideas there. at the moment my menu selections are done with corrosponding numbers. however using your string technique and a bit of validiation it may look/feel more proffesional albeit slower to operate.