Thread: writing switch

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    14
    Rep Power
    0

    writing switch


    im writing a program using switch, that a user input a abbreviation than using switch it will out put the name of the state.
    switch!!!
    case 1: cout<<"state.\n"; break; this code work

    but i trying 2 do abbreviation
    case 'FL': cout<<"Florida.\n"; break; this code dont work
    ane1 noe y it dont work??
  2. #2
  3. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    ane1 noe y it dont work??
    First off, try typing in english; this "speed typing" or whatever it is makes you look like a drooling idiot.

    case 'FL': cout<<"Florida.\n"; break;
    'FL' is a syntax error. You're probably trying to catch a case for a particular string, in which case you should've tried using "FL" instead (note the double quotes). The switch construct though, doesn't work that way. It works only on integers. Not on strings, objects, or even floating-point values.

    If you want to handle abbreviations like FL and such, try a chain of IF-statements instead:

    Code:
    if (inputStr == "FL") {
        // process abbreviation for Florida
    } else if (inputStr == ...) {
    ...
    } else if...
    I think you'll get the idea. Good luck :)
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    14
    Rep Power
    0
    what i want to stricmp function too?
  6. #4
  7. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    what i want to stricmp function too?
    I assume you're trying to ask, "What if I want to use the stricmp() function, too?". Next time, put a little more effort into formulating your post; people will be more inclined to give you a response.

    If you want to perform case-insensitive matching, then yes, you could use the stricmp function. The syntax depends on what type of string you're using (the c-style char* or C++'s improved string class):

    Code:
    string inputStr;
    
    cin >> inputStr;
    
    if (stricmp(inputStr.c_str(), "FL") == 0) {
        cout << "Florida\n";
    }
    I'm guessing at stricmp()'s prototype here; my Linux system doesn't appear to support it. YMMV.
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    14
    Rep Power
    0

    thx


    thank you! 4 the tip man, It helped me alot hehe

IMN logo majestic logo threadwatch logo seochat tools logo