Switch-case not responding to operators

This is a discussion on Switch-case not responding to operators within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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,185
    All this means is that you think the value of s is '+' when it isn't.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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,185
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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,207
    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 10:03 AM.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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....
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    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%.

  11. #11
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    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, 04: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, 02:51 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21