Compare Char to String
Hi All,
I tried searching this on the web but couldnt follow anything. I am trying to compare Char with a string as following example.
char sum[2] = "1G";
string summary = "1G";
if (summary == sum)
{
if (!::CopyFile(sz1ASource, sz1ADest, false))
{
// Error file not copied
g_oLogFile.Log("Error file not copied");
return false;
}//end of if
}
Hope you could help
[445 byte] By [
nkapachi] at [2007-11-19 22:47:24]

# 1 Re: Compare Char to String
object declared with string is initialised with '1G'. type and object can not be compared
<edit> to make it sound more correct :)
# 2 Re: Compare Char to String
Try to change if(summary == sum) to if(!strcmp(summary.c_str(), sum))
# 3 Re: Compare Char to String
Well...old ANSI C character arrays do not have an overloaded comparison operator. There are several solutions to the problem. 'strcmp' is one of them. A different one would be...
char sum[2] = "1G";
string summary = "1G";
if(summary == string(sum))
Is there any need why you need the character array 'sum' in the first place? Can't you just use a string instead?
# 4 Re: Compare Char to String
BTW: You are allocation to little space for sum. You need at least sum[3] to store "1G" because of termination zero.
philkr at 2007-11-10 23:36:04 >

# 5 Re: Compare Char to String
Thanks to all.
if(!strcmp(summary.c_str(), sum)) just worked fine. thanks again