Jump to content
  • Advertisement
Sign in to follow this  
stenny

[SOLVED] strcmp not working?

This topic is 3722 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey GDNet, I've arrived at one of those "you've debugged a hundred times, but yet can't find the possibly stupid source of the problem"-moments. I'm going crazy! Why is strcmp() not working? What am I doing wrong? I've got two std::strings (m_pSounds[nSound].strName and strFilename), holding the same value, but when I compare them, a 0 is returned. I also tried putting the strings in buffers, and comparing those, but that doesn't work either. What's the problem?
// without buffer
int Integer;

Integer     = strcmp(m_pSounds[nSound].strName.c_str(), strFilename.c_str());

// with buffer
const char	*Buf1	= m_pSounds[nSound].strName.c_str();
const char	*Buf2	= strFilename.c_str();
Integer	= strcmp(Buf1, Buf2);




Both times Integer turns out to be 0. And yes, I've checked (with the debugger) if the strings are the same. Help me out, please! -Stijn

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Return Value
Returns an integral value indicating the relationship between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.


Are strings the same or aren't they?

Share this post


Link to post
Share on other sites
Wait. Wut...A Dammit!

..I

...You

...Ugh. I rest in peace knowing that these sort of things happen to everyone. They do. Right?

Thanks, Antheus! :p

[EDIT]
What fool made up that 0 is good, 0 means OK?

Share this post


Link to post
Share on other sites
0 means they're the same, if not 0 then one string is greater than the other.

http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html

Share this post


Link to post
Share on other sites
Quote:
Original post by stenny
What fool made up that 0 is good, 0 means OK?

You have seen this before right?:

int main()
{

return 0; // Everything went just fine
}

[wink]

Share this post


Link to post
Share on other sites
Also, why are you using strcmp? std::string has a compare method, which in addition to being more idiomatic C++ doesn't involve the risk of buffer overflows.

Share this post


Link to post
Share on other sites
Quote:
Original post by stenny
What fool made up that 0 is good, 0 means OK?


There is a good reason that equality returns 0. Because of strcmp's return value, you can use it in order to sort strings.

It returns a negative value is the first string is "lesser" than the second (I might have this backwards so don't take this 100% at face value).

It returns 0 if the strings are equal.

And it returns a positive value if the first string is "greater" than the second (again, I might have the positive and negative flipped).



Usually you'd want a more robust string sorting comparison taking into account capitals and special characters, but a simple sorting algorithm is to convert all strings to upper case and then use strcmp as the comparison.

Share this post


Link to post
Share on other sites
strcmp's return value is used for sorting as well as comparison. When you sort strings, you need to know which is "greater" than the other (i.e. comes later in lexicographic (i.e. dictionary) order).

Anyway. Here is how you compare the strings in C++:


bool areTheyTheSameDarnIt = (m_pSounds[nSound].strName == strFilename);



Instead of automatically reaching into your old bag of tricks, which is demonstrably cryptic and hard to understand, try looking for something more obvious :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!