Thread: Switch-case not responding to operators

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Unhappy Switch-case not responding to operators

    Code:
    switch (s)
                    {
    
                    case '+':std::cout<<"D";break;
                    case 'c':std::cout<<"D";break;
                    
                    }
    This sounds somewhat silly..!...I remember doing this correctly years ago....but but now whenever s is '+' nothing happens.
    Does stringstream or linux console input treat the operators differently (compared to characters)...There is a
    Code:
    if(in>>s ,s)
    clause before the switch..(in being an input stringstream.)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    All this means is that you think the value of s is '+' when it isn't.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    oo...Got the solution....apparently...it was bypassing the whole part and entering an if block above which had a condition of being a number to enter.......Why is + being treated as a number more preferably than a character?

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    The switch(condition) clause is based on an integral value so when you pass a char to the switch it will upcast to an int to make the actual comparison!

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    but that applies to every character that can be entered....not only '+'...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then apparently your condition of being a number to enter isn't working. You should have been using isdigit(s) rather than trying to hardwire your own.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Does isdigit() work with floating points?
    Consider the String entered:
    sin 48.8 + cos 30

    When I pass the input through the stringstream, and it is in the second iteration...would isdigit() make out 48.8 as a number?

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No.

    Individual characters in your string would be passed to isdigit(). isdigit() is not used to check floating point values, and is not used to check substrings (unless by "check substring" you mean "check each character in the substring").

    If "48.8" is being passed to isdigit() on the second iteration, you are presumably extracting substrings and using a conversion (aka cast) to pass it to isdigit(). That is not valid usage of isdigit().
    Last edited by grumpy; 02-03-2011 at 11:03 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    no..I was actually reading the substring from the stream directly into a floating point variable and assuming that if that read failed , the part is not a number....but '+' somehow manages to arrive as the same place as the numbers....

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That's because, when reading data from a stream, the + sign indicates a positive value. So the string "+42" is interpreted as the numeric value 42.

    You need to check for the presence of operators (or, more generally, anything that is not a digit or a separator) BEFORE extracting a numeric value from your string.

    Your problem is not with switch/case. It is that you are reading data from your string before doing necessary checks on what that string contains.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Consider the String entered:
    sin 48.8 + cos 30
    Why not build your own little preprocessor that will separate your tokens with spaces, and then just read from stringstream to string: ss >> token;

    After that you can check your token to see if it's an operator (not just + or -, but also lets say "sin", "and", "=", "==" - probably stored in a map? It can get a little bit messy if you wan't both + and ++, or = and == operators, but it's not hard), variable name ([a-zA-Z_]+[0-9_]*) or something else, which will probably be a numerical value. You can use stringstream to try and convert it to double (if that's your underlying type for that) - if it fails (leaves characters in the buffer) it's syntax error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. confused
    By alexbcg in forum C Programming
    Replies: 2
    Last Post: 12-07-2010, 05:42 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. function switch help
    By etbjr182 in forum C Programming
    Replies: 9
    Last Post: 02-20-2009, 03:51 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM

Tags for this Thread