Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Getting the char from a CString

Posted on 2006-03-24
7
Medium Priority
?
349 Views
Last Modified: 2008-02-01
Hi Experts,

I am getting one char from the user.  I have a EditBox control that gets this character in
a CString variable.  I need to check the validity of the character.  I am only getting one character from the user.

CString      m_oneLetter;

I need to convert this to a character and do the validation.  How can I convert it.

0
Comment
Question by:thanesh
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 200 total points
ID: 16281055
CString m_oneLetter("a");
char c = ((LPCTSTR)m_oneLetter)[0];
c contains your one char.
0
 
LVL 1

Author Comment

by:thanesh
ID: 16281361
Thanks, but I only get the correct user entered value when I use.

m_oneLetter.GetBuffer(1).  So, how can I change this to a character?
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16281566
>> m_oneLetter.GetBuffer(1).
char c = m_oneLetter.GetBuffer(1)[0];

Use this its easier
CString m_oneLetter("a");
char c = m_oneLetter.GetAt(0);
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:thanesh
ID: 16281612
Oops.  If I use GetBuffer(1) it returns the character.  But, when I use GetAt(0) ( not 1 as GetLength() returns 0) I am getting an assertion error.
How can I do it then :-)  Thanks.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16281651
>>>> So, how can I change this to a character?

char c = m_oneLetter[0];

>>>> m_oneLetter.GetBuffer(1)

You don't need that function. The purpuse of GetBuffer is to get a writeable  char array from CString, e. g.

   char* psz = m_oneLetter.GetBuffer(255);

After that you could copy data to that buffer:

   strcpy(psz, "Hello World");

But  you need to make the CString valid after copying:

   m_oneLetter.ReleaseBuffer(-1);

After that m_oneLetter is a valid CString again cause it could set it's internal lengths members properly.

But:

   m_oneLetter = "Hello World";

would do the same as the three statements GetBuffer, strcpy, ReleaseBuffer.

Regards, Alex

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16281783
>>>> If I use GetBuffer(1) it returns the character

No. GetBuffer only returns the internal char array of CString as a writeable (non-const) char pointer.

>>>> when I use GetAt(0) ( not 1 as GetLength() returns 0) I am getting an assertion error.

GetAt(0) gets an assertion if the length of the CString is 0 or undefined. The latter is the case after you called GetBuffer cause you are now responsible for the the buffer. CString would set its internal length to 1 if you call ReleaseBuffer(-1). The -1 argument tells that the terminating zero character was checked.

If you have a edit box associated with a CString object, you should do the following:

   UpdateData(TRUE);  // That copies data from screen (editbox) to the CString member (m_oneLetter);
   if (m_oneLetter.GetLength() > 0)   // check if user entered at least one character
   {
        char c = m_oneLetter[0];    // get the character and store it to a local char
   }

Regards, Alex



0
 
LVL 1

Author Comment

by:thanesh
ID: 16282321
Thanks a lot Alex.  Sorry, I have already accepted Rajeev's answer.  I will give you the points in the next question :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

577 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