CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    144

    trouble with strcmp

    so after trying awhile to compare a const char* to a string, I read that I'm supposed to compare using the strcmp method. But I'm still not getting a correct comparison to a string. While debugging, my text values look fine so I'm stumped. I'm trying to compare values in my std::map like so -

    Code:
    	for(std::map<Vector3DInt32,const char*>::iterator i = polys.begin(); i != polys.end(); i++) {
    		if(strcmp((*i).second,"Neighbor") == 0)  mywindow->appendText("[colour='FFFF0000']");
    		else  mywindow->appendText("[colour='FFFFFFFF']");
    		mywindow->appendText("{ ");
    		mywindow->appendText(Ogre::StringConverter::toString(Vector3((*i).first.getX(),(*i).first.getY(),(*i).first.getZ())));
    		mywindow->appendText(" }, ");
    	}
    I also tried using std::string and then comparing using

    if((*i).second.compare("Neighbor") == 0)

    but that is failing also...
    P.S. where is the code tag button?
    Last edited by Marc G; July 2nd, 2012 at 01:30 AM. Reason: Fixed code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: trouble with strcmp

    How is it failing?

    By the way: you got the code tags almost right except that you should close with [/code].
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: trouble with strcmp

    Quote Originally Posted by drwbns View Post
    so after trying awhile to compare a const char* to a string, I read that I'm supposed to compare using the strcmp method. But I'm still not getting a correct comparison to a string.
    The comparison is correct. Are you saying that strcmp() or std::string::compare has bugs? Obviously, what's behind that variable is not the string "Neighbor", as you claim it to be, since there is no chance that both of these functions have bugs comparing simple strings (if that were the case, thousands of programmers would have reported it).
    While debugging, my text values look fine so I'm stumped.
    There is a difference between how it looks in the debugger and what that value really is. If you took the time to output each character in a loop, or you did a more thorough inspection using the memory window of the debugger, you will see that the string is not "Neighbor", but possibly a string that contains control characters or spaces that are not visible under ordinary circumstances.

    Also, after 97 posts, you don't know how to properly use code tags?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 1st, 2012 at 10:23 PM.

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