CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jun 2009
    Posts
    19

    Adding switches to function

    Hey again...

    This time I want to make the function paramaters operate a switch from a function variable, how is this done?

    eg

    Code:
    int 123(string 456)
    {
    switch(456)
    case 'command1': cout << timeStr << ": command1"; break;
    case 'command2': cout << timeStr << ": command2"; break;
    default: cout << "invalid input."; break;
    }
    The output need to be like this...

    I have a variable called timeStr which needs to be before the function paramater.

    [variable]: "whatever was in the function paramater".



    Thanks

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Adding switches to function

    switch only works with numeric expressions.

  3. #3
    Join Date
    Jun 2009
    Posts
    19

    Re: Adding switches to function

    oh ok thanks, is there a way to use the string and compare it to a variable?

    so it compares the "456" string and if it is command1 then number 1. If it is command2 then it would interpret it as number 2.

    Code:
    int 123(string 456)
    {
    
    if 456 = ("command1")
    operatorForSwitch = 1;
    if 456 = ("command2")
    operatorForSwitch = 2;
    
    switch(456)
    case 'command1': cout << timeStr << ": command1"; break;
    case 'command2': cout << timeStr << ": command2"; break;
    default: cout << "invalid input."; break;
    }

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Adding switches to function

    Giving your string variable a numeric name is confusing. Why don't you use real compilable syntax to show what you're trying to do, or at least unambiguous pseudocode.

    You could use the if statements to come up with a numeric value for your switch, but what's the point? As long as you're evaluating the value in your string with if, you may as well use those statements to execute the appropriate code.

  5. #5
    Join Date
    Jun 2009
    Posts
    19

    Re: Adding switches to function

    Okay, I was only using switchs becuase that's what I learnt instead of multiple if statements

    Thanks.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Adding switches to function

    Variable, function and class names in C++ must start with a letter.
    There are really a lot of basic syntax errors in your code. You should study a beginners' C++ book. See the FAQ for recommendations.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Jun 2009
    Posts
    19

    Re: Adding switches to function

    oh, The 123 and 456 where just like xyz, not actual filenames but examples :P

  8. #8
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Adding switches to function

    You want something like this

    Code:
    //This macro is easier to understand than negating a compare function
    #define AreSame(p1, p2)\
        !p1.compare(p2)
    
    if (AreSame(xyz, "SomeValue")){
       do something
    } else if (AreSame(xyz, "Some Other Value")){
       do something else
    }...

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Adding switches to function

    Quote Originally Posted by ninja9578 View Post
    You want something like this

    Code:
    //This macro is easier to understand than negating a compare function
    #define AreSame(p1, p2)\
        !p1.compare(p2)
    
    if (AreSame(xyz, "SomeValue")){
       do something
    } else if (AreSame(xyz, "Some Other Value")){
       do something else
    }...
    What's wrong with just using == to compare the string to the literal?

  10. #10
    Join Date
    Jun 2009
    Posts
    19

    Re: Adding switches to function

    ok.
    Is AreSame a real function and what does this do?

    !p1.compare(p2)?

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Adding switches to function

    Quote Originally Posted by CatFace
    Is AreSame a real function and what does this do?
    No, it is a macro. It basically replaces AreSame(x, y) for some expressions x, y with !x.compare(y).

    Quote Originally Posted by CatFace
    !p1.compare(p2)?
    That is effectively the same as p1.compare(p2) == 0, but the latter expresses the idea that this is a test of equality better, thus addressing ninja9578's objection. However, even better yet would be: p1 == p2, e.g., xyz == "SomeValue".
    Last edited by laserlight; June 18th, 2009 at 02:15 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Oct 2006
    Posts
    15

    Re: Adding switches to function

    Have a look at this, it uses strcmp the syntax is
    Code:
    int strcmp ( const char * str1, const char * str2 );
    so u can have something like
    Code:
    #include stdio.h
    #include string.h
    ...
    ...
    
    if (strcmp(string1, string2) = = 0) //both sting are equal
             do something;
    else if (strcmp(string1, string3)  < 0) //1st string < than second
              do something else;
    else do another thing;
    strcmp in my opinion is better to use for comparing strings, or array of char.

    If you must use a switch, I believe you can only switch int or a char. ie 1, 2, 40, 'a' , ' n', 'M'....
    Last edited by servalsoft; June 19th, 2009 at 01:51 AM.
    ...Don't be afraid, only believe.

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Adding switches to function

    Quote Originally Posted by servalsoft
    strcmp in my opinion is better to use for comparing strings, or array of char.
    strcmp() would be appropriate for comparing null terminated strings, but I hope and believe that CatFace is using std::string objects.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Adding switches to function

    If the string is treated as command (as in shell), you can write a function which takes string (the command), and return numeric constant for the command. Like:
    Code:
    int GetCommandId(const string& cmd)
    {
    if(cmd=="list")
    return 0;
    if(cmd=="delete")
    return 1;
    if(cmd=="find")
    return 2;
    ....
    }
    Then use the return value of 'GetCommandId' in switch:
    Code:
    switch(GetCommandId(str))
    {
    case 1:
    ...
    }
    You may also use #define or enum constants for better readability.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured