Thread: Hmmm... Comparison Troubles...

  1. #1

    Hmmm... Comparison Troubles...

    I have been working in the registry for awhile now, not really reading anything though. OF course, when i do i run into trouble. Below is the code i am using, with comments where i get the problems (i think). The registry code is not the problem. It is comparing the return values with predetermined values that i hit the walls.

    Code:
      HKEY hKey;
      if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Native\\SaveAll"), 0, KEY_QUERY_VALUE, &hKey)) return false;
    
      unsigned char buffer[256]; // It puts the data here...
      unsigned char yes[256] = "Yes";  // What i am going to compare with...
      unsigned long datatype;
      unsigned long bufferlength = sizeof(buffer);
    
      RegQueryValueEx(hKey, "Active", NULL, &datatype, buffer, &bufferlength);
     
      RegCloseKey(hKey);
    
      if (buffer == yes) return true;  // How do i compare these?? This is where i get the problems.
      return false;
    Even when the data appears to be the same, it still returns false.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if (buffer == yes)
    Classic mistake, I'm afraid.

    Look up strcmp() for comparing strings.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    if (std::stricmp(buffer, "Yes") == 0)

    Remember to include <string>
    Edit:: Beaten again...

  4. #4
    the problem though, is that the buffer is an unsigned char *.

    I get compiler complaints every time, with all of the strcmp functions i have tried.

    Ex.
    Code:
    42 main.cpp
     passing `unsigned char *' as argument 1 of `stricmp(const char *, const char *)' changes signedness
    
    42 main.cpp
     passing `unsigned char *' as argument 1 of `strcmp(const char *, const char *)' changes signedness
    
    
    42 main.cpp
     passing `unsigned char *' as argument 2 of `strcmp(const char *, const char *)' changes signedness
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Try

    if (std::stricmp(reinterpret_cast<const char *>(buffer), "Yes") == 0)

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    So cast it.

    if(stricmp((char*)buffer, "yes"))...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Inquirer
    the problem though, is that the buffer is an unsigned char *.

    I get compiler complaints every time, with all of the strcmp functions i have tried.

    Ex.
    Code:
    42 main.cpp
     passing `unsigned char *' as argument 1 of `stricmp(const char *, const char *)' changes signedness
    
    42 main.cpp
     passing `unsigned char *' as argument 1 of `strcmp(const char *, const char *)' changes signedness
    
    
    42 main.cpp
     passing `unsigned char *' as argument 2 of `strcmp(const char *, const char *)' changes signedness
    is there any reason the buffer needs to be unsigned char? as a general rule, you should just use char when working with strings, not signed or unsigned, because then the target system can choose whatever is fastest/easiest to work with for it
    hello, internet!

  8. #8

    ~inquirer

    *bump*

    If anyone cares, i found the source of my problem. I was thinking that strcmp returned a true value if it matches, but in actuality, it returns a zero. If it doesn't match, then it returns the character position thta doesn't. Thanks for your help and suggestions. This solved my whole problem

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  3. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  4. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 01:15 PM
  5. having troubles
    By neandrake in forum C++ Programming
    Replies: 7
    Last Post: 03-07-2002, 09:31 PM