Converting error from 'CString' to 'const char *

answerme's Avatar, Join Date: Dec 2007
Ambitious contributor
Hi I am getting a conversion error

Code:
CString Instring;
CString Automesg[2];
Automesg[0]="test 1";
Automesg[1]="Test Example2";
if(strcmp(Instring,Automesg[0])==0) //HERE IS THE ERROR FOR InString Variable
	{
		Sleep(1000);
         }

error C2664: 'strcmp' : cannot convert parameter 1 from 'CString' to 'const char *'	
Can anyone resolve it
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
You can use the "compare" function.

Say, you have 2 CStrings StringA and StringB.
You can compare them like :
Code: CPP
if ( StringA.compare(StringB) == 0 ) cout << StringA << " and " << StringB << " are not equal.";

So, your code can be modified as :
Code: CPP
CString Instring;
CString Automesg[2];
Automesg[0]="test 1";
Automesg[1]="Test Example2";
if(Instring.compare(Automesg[0])==0)
{      Sleep(1000);   }
Gene Poole's Avatar, Join Date: Nov 2009
Contributor
I think the problem is likely a UNICODE issue. With UNICODE enabled in your project, CStrings default to "wide" character strings. A "wide" character is 2 bytes whereas normal char is 1 byte. You can use the "wide" versions of the comparison functions, wcscmp() v. strcmp(), or to keep everything portable, use the TCHAR macros. _tcscmp() is a macro that will evaluate to strcmp() in MCBS builds or wcscmp() in UNICODE builds.