CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2008
    Posts
    36

    Why doesnt this memcmp work???

    Im new to c++ so bare with me.. This code compiles but it doesnt stop when they are equal.. Whats my problem?

    Code:
    static TCHAR * szUp      = TEXT ("Up") 
    TCHAR szKeyName[32];
    
    if (memcmp(szKeyName, szUp, 2)) {
      s_selected[mapSudokuBoard[hwnd]] = 0 ;
      SetFocus (s_window[mapSudokuBoard[hwnd] - 9]);
    }
    Last edited by tgreaves; July 31st, 2008 at 09:36 AM.

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

    Re: Why doesnt this memcmp work???

    Have you checekd the return values for memcmp? 0 indicates equality.

    Wouldn't strcmp be better here?

  3. #3
    Join Date
    Jul 2008
    Posts
    36

    Re: Why doesnt this memcmp work???

    I havent checked the values as of yet.. My boss is a c++ programmer and thats just what he told me to use.. Ill check out strcmp also..

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Why doesnt this memcmp work???

    Quote Originally Posted by GCDEF
    Wouldn't strcmp be better here?
    Sure, you meant _tcscmp
    Victor Nijegorodov

  5. #5
    Join Date
    Jul 2008
    Posts
    36

    Re: Why doesnt this memcmp work???

    Very strange.. This code below works..

    Code:
             iMyInt = memcmp(szKeyName, szUp, 2);
             if (iMyInt == 0) {
               s_selected[mapSudokuBoard[hwnd]] = 0 ;
               SetFocus (s_window[mapSudokuBoard[hwnd] - 9]);
    		 }
    Any ideas whats wrong with my first bit of code at the top?

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

    Re: Why doesnt this memcmp work???

    Quote Originally Posted by tgreaves
    Very strange.. This code below works..

    Code:
             iMyInt = memcmp(szKeyName, szUp, 2);
             if (iMyInt == 0) {
               s_selected[mapSudokuBoard[hwnd]] = 0 ;
               SetFocus (s_window[mapSudokuBoard[hwnd] - 9]);
    		 }
    Any ideas whats wrong with my first bit of code at the top?
    Sure. In the first you're testing for non-zero, in the second you're testing for zero.

    Change the first one to
    Code:
    if (memcmp(szKeyName, szUp, 2) == 0)
    {
      s_selected[mapSudokuBoard[hwnd]] = 0 ;
      SetFocus (s_window[mapSudokuBoard[hwnd] - 9]);
    }
    and it will work too.
    Last edited by GCDEF; July 31st, 2008 at 09:45 AM.

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

    Re: Why doesnt this memcmp work???

    Quote Originally Posted by VictorN
    Sure, you meant _tcscmp
    In all the years I've been doing this, Unicode has never been an issue I've ever had to think about. Maybe it's just me. FWIW, selecting _tcscmp documentation in MSDN brings up strcmp documentation.

  8. #8
    Join Date
    Jul 2008
    Posts
    36

    Re: Why doesnt this memcmp work???

    Quote Originally Posted by GCDEF
    Sure. In the first you're testing for non-zero, in the second you're testing for zero.

    Change the first one to
    Code:
    if (memcmp(szKeyName, szUp, 2) == 0)
    {
      s_selected[mapSudokuBoard[hwnd]] = 0 ;
      SetFocus (s_window[mapSudokuBoard[hwnd] - 9]);
    }
    and it will work too.
    I thought "if (!memcmp(szKeyName,szUp,2))" was testing for not zero???

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

    Re: Why doesnt this memcmp work???

    Sigh. It is. That's why I add == 0 to the if statement.

    A return of 0 indicates equality. You're testing to see if the different.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Why doesnt this memcmp work???

    Quote Originally Posted by GCDEF
    In all the years I've been doing this, Unicode has never been an issue I've ever had to think about.
    Consider yourself fortunate.

  11. #11
    Join Date
    May 2007
    Posts
    811

    Re: Why doesnt this memcmp work???

    Quote Originally Posted by tgreaves
    I havent checked the values as of yet.. My boss is a c++ programmer and thats just what he told me to use.. Ill check out strcmp also..
    Then why not using std::string?
    Code:
    std::string stringOne ="Blah";
    std::string stringTwo ="Nada";
    if (stringOne == stringTwo)
    {
       // string are the same
    }
    else
    {
       // strings are not the same
    }

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