First, I am learning C++, and need your help with an issue I have... I have the following piece of code:
std::string var2;
std::string MSG1="1";
char charSeq[1024];
cout << charSeq << endl;
var2 = charSeq;
cout << var2 << endl;
The above does for me what I want, which is taking the contents of the character sequence named charSeq and put them in the string variable named var2 But when I want to do comparison or any operation on the string variable var2, I cannot, for example, if charSeq has the value of 1, and I assigned this value to var2, then I made comparison between MSG and var2, the result is always false, like var2 has no value or has a wrong value...
if (var2==MSG1)
{
// the code here never gets executed, even when the values are the same in var2 and MSG...
}
else
{
// the result is always false and this condition executed no matter what var2 and MSG values were...
}
Can you help me with some tips on this?
After reading the comment from Seth above (thank you very much for the tip of the length), I managed to solve the issue: I was having 2 invisible characters in charSeq, and those were copied to var2, this was causing the false result of the comparison, what I did is:
cout << "charseq length: " << strlen(charSeq) << endl;
cout << "var2 length: " << var2.length() << endl;
var2.erase(var2.length()-2,var2.length()); //here is what i did...
cout << "var2 new length: " << var2.length() << endl; // the result was the correct length of the visible characters, and the comparison condition was executed...
Thanks again for all of you...