Thread: Value comparison problem

  1. #1
    Lurk
    Join Date
    Dec 2005
    Posts
    3

    Value comparison problem

    Heyo. I've been bored at work and decided to brush up on my programming, but I'm hitting what appear to be a few pretty basic problems that I can't seem to fix. I'm trying to use an if statement to return different values depending on different values for passed character, ignoring case.

    Here's how the function looks right now:
    Code:
    int CheckName(int x) {
      if (x == 'Q' || x == 'q')
        return -1;
      else if (x == ('c' || 'b' || 'h' || 'k' || 'm')) 
        return 1;
      else if (x == ('C' || 'B' || 'H' || 'K' || 'M'))
        return 1;
      else
        return 0;
    }
    The first if check works fine, but everything fails the following if statements and ends up returning 0. I have also tried:
    Code:
    int CheckName(int x) {
      if (x == 'Q' || x == 'q')
        return -1;
      else if (x == 'c' || 'b' || 'h' || 'k' || 'm') 
        return 1;
      else if (x == 'C' || 'B' || 'H' || 'K' || 'M')
        return 1;
      else
        return 0;
    }
    The only way I have gotten it to work is to have multiple checks within the parentheses, like the first if check, but it looks very large and overcomplicated. Is there a better way to do this?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( x == this || x == that || x ==  theotherthing )
    You have to do it that way, if you're going to use an if.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Lurk
    Join Date
    Dec 2005
    Posts
    3
    Bother. Is there a better way to check the variable against several different values? I prefer ifs to switch/case, but am drawing a blank as to any other options.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is it really that hard to type three extra characters? (Five if you want to format it with nice little whitespaces.) But no, there isn't another way. Well, I take that back:
    Code:
    if( strchr( "cbhkmCBHKM", x ) )
    {
        ...letter found...
    }
    else
    {
        ...letter not found...
    }
    However, if you don't like function calls, use an 'if', or a 'switch'. But I'm sure there's someone else around here who taught god how to program, that will be along shortly to give you a better method. He seems to be stalking me, so he should be along shortly. (<--- sorry about that whole 'short' joke. Then again, I bet you are too.)


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Lurk
    Join Date
    Dec 2005
    Posts
    3
    It's not a huge effort, just an aesthetic thing I suppose.
    Code:
      else if (x == 'c' || x == 'b' || x == 'h' || x == 'k' || x == 'm')
        return 1;
      else if (x == 'C' || x == 'B' || x == 'H' || x == 'K' || x == 'M')
        return 0;
    Just seems like it could be cleaned up a bit.

    I don't mind function calls at all, that should work lovely. Thanks much.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    tolower() might reduce your key strokes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could use tolower or toupper and then you'll only have to test for one case.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I already said that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ah, so you did. See what happens when you add Color to your posts? Insert something about cowboys, color, and being slow on the trigger here.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    FWIW
    Code:
    int CheckName(int x)
    {
       char text[2] = {0};
       text[0] = x;
       if ( strcspn(text, "Qq") == 0 )
       {
          return -1;
       }
       if ( strcspn(text, "CcBbHhKkMm") == 0 )
       {
          return 1;
       }
       return 0;
    }
    Last edited by Dave_Sinkula; 12-06-2005 at 03:08 PM. Reason: "CdBbHhKkMm" -> "CcBbHhKkMm"
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Problem with comparison
    By black187 in forum C Programming
    Replies: 4
    Last Post: 05-11-2006, 04:14 AM