Solved

Convert CString to char for use in Switch

Posted on 2003-12-06
8
20,889 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:APlusComp247
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9889174
>> 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

Use

CString str ( "test");
char* pc = (LPCTSTR) str;

switch ( *pc) {

case 'a':

//...

break;

//....

case 't':

break;
};
0
 
LVL 86

Expert Comment

by:jkr
ID: 9889181
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.
0
 

Author Comment

by:APlusComp247
ID: 9889224
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
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 9889237
Ooops, make that read

const char* cLetter = (LPCTSTR) szFirstLetter;

and it will work. Since you are not going to manipulate the char*, this is the easiest solution.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:APlusComp247
ID: 9889320
That works great!!!!   Thank you so much for you quick responces!
0
 
LVL 1

Expert Comment

by:TiCpu
ID: 9896418
Better would be :
char cLetter = sTextYouWantFirstLetter.GetAt(0);
0
 

Author Comment

by:APlusComp247
ID: 10004396
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!
0
 
LVL 1

Expert Comment

by:WilimarLynwood
ID: 10484944
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;
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Separate into files by filename 12 76
Excel Use VBA to get user's Mac Address for their computer 5 114
How to determine file rename from EventLog ? 2 43
maxBlock challenge 30 112
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now