A switch question

This is a discussion on A switch question within the C++ Programming forums, part of the General Programming Boards category; How do I do this: Code: string test; test = "hey"; switch(test) { case "bye": do somthing(); case "hey": do ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    180

    A switch question

    How do I do this:

    Code:
    string test;
    test = "hey";
    
    switch(test)
    {
    case "bye": do somthing();
    case "hey": do somthing();
    }
    i get a [C++ Error] Unit1.cpp(25): E2383 Switch selection expression must be of integral type error. any help would be nice!
    thx

    DW

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    the controlling expression for a switch statement must always return either a bool value or an enum constant - one of integer type or character.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... and don't forget the "break" statement.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Here's how I do it:
    Code:
    #define STR_STRING1 "string1"
    #define STR_STRING2 "string2"
    
    int WhatStringIsThis(char *TheString)
    {
        if (!strcmpi(TheString,STR_STRING1))
            return STR_STRING1;
        if (!strcmpi(TheString,STR_STRING2))
            return STR_STRING2;
    
        return 0;
    }
    
    int main()
    {
        char Input[32];
    
        cin >> Input;
    
        switch (WhatStringIsThis((char *)&Input))
        {
            case STR_STRING1:
                //etc
    That seems to me like the only way to do it with a switch.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Quote Originally Posted by bennyandthejets
    Here's how I do it:
    Code:
    #define STR_STRING1 "string1"
    #define STR_STRING2 "string2"
    
    int WhatStringIsThis(char *TheString)
    {
        if (!strcmpi(TheString,STR_STRING1))
            return STR_STRING1;
        if (!strcmpi(TheString,STR_STRING2))
            return STR_STRING2;
    
        return 0;
    }
    
    int main()
    {
        char Input[32];
    
        cin >> Input;
    
        switch (WhatStringIsThis((char *)&Input))
        {
            case STR_STRING1:
                //etc
    That seems to me like the only way to do it with a switch.

    no, that won't work! that macro still reduces to a character array - in this type of situation, just use if/else statements.



    ITSA
    Socket Library!

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    how bout going this route?

    Code:
    #define "Wood" 7777;
    #define "Stone" 6666;
    #define "Me" 9999;
    
    //get variable
    
    cin << K;
    
    //then could you switch on the k?
    
    switch(k)
    {
    case 7777: break;
    case 6666: break;
    }
    is this okay?

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Nope. There is no easy way to use a switch on a string of characters.

    And besides cin has no operator <<

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Quote Originally Posted by Death_Wraith
    how bout going this route?

    Code:
    #define "Wood" 7777;
    #define "Stone" 6666;
    #define "Me" 9999;
    //get variable
    cin << K;
    //then could you switch on the k?
    switch(k)
    {
    case 7777: break;
    case 6666: break;
    }
    is this okay?

    no, it's not ok.

    you're missing the point. the switch statement is solely for testing against integral manifest constants - trying to work string handling in there is just a waste of your time...



    ITSA
    Socket Library!

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Oops sorry I mucked up that method. Here is the correct way:
    Code:
    #define STR_STRING1 101
    #define STR_STRING2 102
    
    int WhatStringIsThis(char *TheString)
    {
        if (!strcmpi(TheString,"string1"))
            return STR_STRING1;
        if (!strcmpi(TheString,"string2"))
            return STR_STRING2;
    
        return 0;
    }
    
    int main()
    {
        char Input[32];
    
        cin >> Input;
    
        switch (WhatStringIsThis((char *)&Input))
        {
            case STR_STRING1:
                //etc
    I know it's messy but if you insist on using a switch, it's alright.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Quote Originally Posted by Thantos
    Nope. There is no easy way to use a switch on a string of characters.

    And besides cin has no operator <<
    hahahah oops!


    Alright, so its not worth my time then?

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    35
    Not really. The easiest method is
    Code:
    switch (true)
    {
    	case (test == "hey"):
    		DoSomething();
    		break;
    	case (test == "bye"):
    		DoSomethingElse();
    		break;
    }
    but then you might as well use if/else if.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Did you even try and test that MortalMonkey? cases in a switch MUST be constant. Which means no evaluation may be done to determine a case's value.

  13. #13
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    w00t for BASIC's "Select Case" statement
    My Website

    "Circular logic is good because it is."

  14. #14
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    try reducing the string to an integer using a map
    .sect signature

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    How would I use a map?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Switch Case Question
    By dnysveen in forum C++ Programming
    Replies: 12
    Last Post: 06-06-2006, 05:34 AM
  3. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  4. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21