A switch question

This is a discussion on A switch question within the C++ Programming forums, part of the General Programming Boards category; >>try reducing the string to an integer using a map IMO that would just complicate things even more, and I ...

  1. #16
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>try reducing the string to an integer using a map

    IMO that would just complicate things even more, and I don't know if it would be very efficient either. I think it's just as simple to do:
    Code:
    #include <iostream>
    
    #define STR1 "String1"
    #define STR2 "String2"
    int main()
    {
      std::string test;
      std::cin>>test;
      if (test==STR1)
        //do something
      else if (test==STR2)
        //do something else
    
    }
    That's not too hard to read is it?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #17
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    but what if you have a LOT OF STRINGS@#%

    huh, your not considering the full impliciations and possibliities of each situation
    huh
    .sect signature

  3. #18
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    huh, if he doesn't know that you can't switch a string do you think that he really needs to learn use a map for alot of strings
    huh
    Woop?

  4. #19
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    yes
    syntax is secondary to programming

    i don't even invoke a compiler anymore, i just imagine my creations running perfectly
    .sect signature

  5. #20
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    #define STR1 "String1"
    #define STR2 "String2"
    Might want to make that:
    const std::string str1("String1");
    const std::string str2("String2");
    Just for the sake of not mixing C and C++ strings
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #21
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I suppose so...Although I like to use defines so I can declare them in a header file to keep things a bit neater.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #22
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Couldn't you just create a singleton to encapsulate all the messy global variables, then declare the const strings in that?

    **EDIT**
    Actually, couldn't you just do this:
    Code:
    #define STRING1 std::string("String1")
    Heh, not much to look at I guess...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #23
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Couldn't you just create a singleton to encapsulate all the messy global variables, then declare the const strings in that?
    I don't know, never used singleton for anything before.

    >>Actually, couldn't you just do this:

    I suppose so, but that would create a new string everything you used it. It seems kind of like an abuse of defines though, after all, the string class does have a comparison operator for cstrings, so why not use it?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #24
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>the string class does have a comparison operator for cstrings, so why not use it?
    Why would you, when you can have so much fun with confusing #define's that construct new objects of classes every time you use them?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #25
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Okay.....I think the point is this: There is NO way to use a switch with a string without complicating the hell out the programmer! lol So i guess I have to use an If/Else!

  11. #26
    Registered User
    Join Date
    Feb 2004
    Posts
    35
    Quote Originally Posted by Thantos
    Did you even try and test that MortalMonkey?
    Only in UnrealScript. I'm starting to mix up the languages, it seems.

Page 2 of 2 FirstFirst 12
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