Go4Expert

Go4Expert (http://www.go4expert.com/)
-   MFC (http://www.go4expert.com/forums/mfc/)
-   -   Converting error from 'CString' to 'const char * (http://www.go4expert.com/forums/converting-error-cstring-const-char-t20535/)

answerme 28Dec2009 12:35

Converting error from 'CString' to 'const char *
 
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 28Dec2009 17:48

Re: Converting error from 'CString' to 'const char *
 
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 28Dec2009 19:35

Re: Converting error from 'CString' to 'const char *
 
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.


All times are GMT +5.5. The time now is 15:24.