string.compare() from .txt file

Hi ! I am trying to compare two string which I know have to be identical but they are never equal...'strName' is read from a text file...

1
2
3
4
5
6
if(strName.compare(strNameOne) == 0)
{
  ...;//do something here
}

fout << "result:" << endl << strName << endl <<strNameOne << endl << "end fout" << endl;



result:
jerry
jerry
end fout



i also tested their sizes like this and strName is 1 char bigger
size_t size1 = strName.length(); // size1 = 6
size_t size2 = strNameOne.length(); // size2 = 5

but, doing this before the .compare() test:
strNameOne = strNameOne + '\n';

gave this output

result
jerry
jerry

end fout



So, the strings are the same...but they're not...help ! i'm new with all of this !! Thanks :)
Last edited on
Are you sure there isn't a space character at the end of one of the strings?
Try this:

1
2
3
4
5
6
7
cout << "strName:" << endl;
for(size_t i = 0; i < strName.size(); ++i)
      cout << strName[i] << endl;
cout << "strNameOne:" << endl;
for(size_t i = 0; i < strNameOne.size(); ++i)
      cout << strNameOne[i] << endl;
cout << "End printing" << endl;


You will probably notice an extra space at the end of strName.
Not that it would change the outcome in this case, but why strName.compare(strNameOne) == 0 instead of the usual strName == strNameOne ?
Thanks Telion u're right !!

1 more question...how do I remove that extra space at the end of strName so that my strName.compare(strNameOne) will work ?

I tried to with this but it didn't work...
1
2
3
4
if (!strName.empty() && strName[strName.length()-1] == ' ') 
{ 	
   strName.erase(strName.length()-1);
}



If I do this i get an out of range error - assert:
1
2
3
4
if (!strName.empty() && strName[strName.length()] == ' ') 
{ 							
   strName.erase(strName.length());
}


I could append ' ' to strNameOne but I would rather not...Thanks again :)
Last edited on
Hi Cubbi

why strName.compare(strNameOne) == 0 instead of the usual strName == strNameOne ?


I guess it's out of habbit. I find it easier to differentiate where the string comparisons are vs integer, float etc. For 'bool' i use 'TRUE, FALSE' for the same reason...

Will it slow down the code much doing it this way ? I don't know how much efficiency I'm losing. Thanks
Things that you can do:
_Change the way you are reading the string (or writing the file) so it does not have that whitespace
_Remove the trailing whitespaces. http://cplusplus.com/reference/string/string/find_last_not_of/
_Compare a subset. http://cplusplus.com/reference/string/string/compare/

I find it easier to differentiate where the string comparisons are vs integer, float etc. For 'bool' i use 'TRUE, FALSE' for the same reason...
Dunno why you will want to differentiate those comparisons.
I find quite obfuscated to write if( var==false ) (also note the lowercase, you don't need those macros in c++)
Hi all I solved it with this:

1
2
3
4
size_t size = strName.length();
string strNameTEMP = strName;
strName.clear();
strNameTEMP.assign(strName, 0, size );	


Thanks again :)
¿? Now both strName and strNameTEMP are empty...
Gosh! I was just scratching my head about that :p
OK. nowwww it works the way it should !

1
2
3
4
size_t size = strName.length();
string strNameTEMP = strName;
strName.clear();
strName.assign(strNameTEMP, 0, size - 1 );

Topic archived. No new replies allowed.