Thread: A switch question

  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
    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.
    [email protected]
    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,708
    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.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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,708
    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...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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.
    [email protected]
    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,743
    w00t for BASIC's "Select Case" statement
    My Website

    "Circular logic is good because it is."

  14. #14
    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?

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, 03:57 PM