APlusComp247
asked on
Convert CString to char for use in Switch
I know that I can't use a CString in a switch statement, but I only need to do a comparison against the first letter of the string. I can not figure out how to take the letter from the CString and put it into a char so I can use it in switch statement.
This seems like a simple thing to do, but all searches on CString and char pull up char pointers and that is what I am trying to get away from.
Thanks in advance.
This seems like a simple thing to do, but all searches on CString and char pull up char pointers and that is what I am trying to get away from.
Thanks in advance.
To elaborate on the above - the docs explain 'operator LPCTSTR()' as follows:
--------------->8--------- ---------- ----------
CString::operator LPCTSTR
operator LPCTSTR ( ) const;
Return Value
A character pointer to the string’s data.
Remarks
This useful casting operator provides an efficient method to access the null-terminated C string contained in a CString object. No characters are copied; only a pointer is returned. Be careful with this operator. If you change a CString object after you have obtained the character pointer, you may cause a reallocation of memory that invalidates the pointer.
--------------->8--------- ---------- ----------
So, if you have acces to the contents through a char*, you can dereference the pointer using '*pc' to access the first character.
--------------->8---------
CString::operator LPCTSTR
operator LPCTSTR ( ) const;
Return Value
A character pointer to the string’s data.
Remarks
This useful casting operator provides an efficient method to access the null-terminated C string contained in a CString object. No characters are copied; only a pointer is returned. Be careful with this operator. If you change a CString object after you have obtained the character pointer, you may cause a reallocation of memory that invalidates the pointer.
--------------->8---------
So, if you have acces to the contents through a char*, you can dereference the pointer using '*pc' to access the first character.
ASKER
This is part of the code that I am use
szFileName = data.cFileName;
szFirstLetter.Format("%s", szFileName .Left(1));
szFirstLetter.MakeLower();
char* cLetter = (LPCTSTR) szFirstLetter;
switch (*cLetter)
{
case 'a':
m_nCount[1] ++;
default:
m_nCount[0] ++;
}
I get the following error.
error C2440: 'initializing' : cannot convert from 'const char *' to 'char *'
Conversion loses qualifiers
Thanks for the prompt reply
szFileName = data.cFileName;
szFirstLetter.Format("%s",
szFirstLetter.MakeLower();
char* cLetter = (LPCTSTR) szFirstLetter;
switch (*cLetter)
{
case 'a':
m_nCount[1] ++;
default:
m_nCount[0] ++;
}
I get the following error.
error C2440: 'initializing' : cannot convert from 'const char *' to 'char *'
Conversion loses qualifiers
Thanks for the prompt reply
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That works great!!!! Thank you so much for you quick responces!
Better would be :
char cLetter = sTextYouWantFirstLetter.Ge tAt(0);
char cLetter = sTextYouWantFirstLetter.Ge
ASKER
TiCpu
Thanks a lot for the post. I am by no means a C++/MFC pro and really appreciate all the little shortcuts that I can pick up. I really thought that there should be an easier way to do that, and sure enough you showed me.
Thanks again!
Thanks a lot for the post. I am by no means a C++/MFC pro and really appreciate all the little shortcuts that I can pick up. I really thought that there should be an easier way to do that, and sure enough you showed me.
Thanks again!
I'd use
CString str = "SomeString";
switch (str[0]) //0 so we point to the first char
{
case 'S':
//stuff
break;
default:
//other stuff
break;
}
CString str = "SomeString";
switch (str[0]) //0 so we point to the first char
{
case 'S':
//stuff
break;
default:
//other stuff
break;
}
>>and put it into a char so I can use it in switch statement
Use
CString str ( "test");
char* pc = (LPCTSTR) str;
switch ( *pc) {
case 'a':
//...
break;
//....
case 't':
break;
};