Actually what I want to do is compare the contents of two char arrays but It seams that it will be easier to just convert the contents to string and just compare them using strcmp():
Actually, you don't need to declare the char array explicitly. It's more common to simply do this:
string strWord("hello");
strcmp() is a function from the standard C library (without ++) that compares char arrays, not C++ strings. To compare C++ strings, simply use the '==' operator:
# include <string>
# include <iostream>
usingnamespace std;
int main()
{
char name1[ 10 ] = "S";
char name2[ 10 ] = "Sony";
int result;
result = strcmp( name1, name2 );
if ( result == 0 )
cout << name1 << " is equal to or same as " << name2;
elseif ( result == -1 )
cout << name1 << " is less than " << name2;
elseif ( result == 1 )
cout << name1 << " is more than " << name2;
system ("pause");
}
std::string is more dynamic and useful. char[] is most useful when you need fixed-length strings. Otherwise, std::string is recommended. Of course, it is up to you to decide which one you use. I prefer std::string, but I also do C code as well as C++. For that reason, I find myself using char[] or sometimes char* with malloc()/free() when I need to use strings in C.