switch(??) - Unix

This is a discussion on switch(??) - Unix ; In my program I have the following statements void Person::setTitle(const Glib::ustring& str) { switch ( str ) { case "None" : { title = title_t::None; break; } case "Dr" : { title = title_t: r; break; } ....... case "Rabbi" ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: switch(??)

  1. switch(??)

    In my program I have the following statements

    void Person::setTitle(const Glib::ustring& str) {
    switch ( str ) {
    case "None" : {
    title = title_t::None;
    break;
    }
    case "Dr" : {
    title = title_t:r;
    break;
    }
    .......
    case "Rabbi" :{
    title = title_t::Rabbi;
    break;
    }
    case "Shaykh" :{
    title = title_t::Shaykh;
    break;
    }
    default : {
    title = title_t::None;
    break;
    }
    }// switch
    }//Method

    when compiling I get an error code that reads:
    ==== error: switch quantity not an integer ====
    Which is true, but according to

    http://newdata.box.sk/bx/c/htm/ch07.htm#Heading54

    the error is unwarranted, since the parameter received by the method
    and later on given to the switch statement for scrutiny is a valid C/C+
    + statement [setTitle(const Glib::ustring& str)]

    Can anyone shed some light to this problem?

  2. Re: switch(??)

    >
    > Can anyone shed some light to this problem?


    you would have better luck by asking in a c++ newsgroup.
    the switch statement can only process numerical values like integers
    (or chars), pointers etc.
    Passing a c++ construct, like a std::string, does not work since no
    cast to a number is possible, hence the error dumped by the compiler.
    Using switch to have a dispatch based on strings (without hashing
    them) is not feasible.
    One way to do such thing would be something like:

    #include

    // provide the mapping between input strings and their associated
    // results taken from the switch
    std:air< std::string, std::string > mapping[] =
    {
    std::make_pair( "None", title_t::None ),
    ...
    };

    or use a map, sorted vector of pairs with binary search and set your
    title accordingly:
    - build map, array of pairs, vector etc.
    - lookup for the string you are looking for
    - fetch the corresponding mapping

    And grab a good about programming in C++ like "accelerated C++" by
    Koenig and Moo or "C++ primer" - no offense here - you'd better know
    you are dealing with a very efficient language that use a lot of
    different paradigms/constructs (BTW, grab anything you find that is
    written by Koenig: he writes really good stuff).

    -- paulo

  3. Re: switch(??)

    In <900c0f30-ed01-4e3b-b4db-f70b262a2d9d@f63g2000hsf.googlegroups.com> jamiil writes:

    > void Person::setTitle(const Glib::ustring& str) {
    > switch ( str ) {
    > case "None" : {


    You can't use switch() on strings. You'll have to use a series of
    strcmp() calls (or whatever similar function Glib::ustring provides.)

    --
    John Gordon A is for Amy, who fell down the stairs
    gordon@panix.com B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"


  4. Re: switch(??)

    On Wed, 30 Apr 2008 15:36:04 +0000 (UTC) John Gordon wrote:
    | In <900c0f30-ed01-4e3b-b4db-f70b262a2d9d@f63g2000hsf.googlegroups.com> jamiil writes:
    |
    |> void Person::setTitle(const Glib::ustring& str) {
    |> switch ( str ) {
    |> case "None" : {
    |
    | You can't use switch() on strings. You'll have to use a series of
    | strcmp() calls (or whatever similar function Glib::ustring provides.)

    Well, at least not in C. Maybe some higher level language may have implemented
    such a syntax/semantic.

    --
    |WARNING: Due to extreme spam, I no longer see any articles originating from |
    | Google Groups. If you want your postings to be seen by more readers |
    | you will need to find a different place to post on Usenet. |
    | Phil Howard KA9WGN (email for humans: first name in lower case at ipal.net) |

  5. Re: switch(??)


    Thanks folks, the problem has been resolved, thanks everyone.

+ Reply to Thread