Showing posts with label CString. Show all posts
Showing posts with label CString. Show all posts

Tuesday, July 14, 2009

MFC - Find if a given string is numeric

Find if a given string is numeric





bool IsNumeric(const CString& str, bool bIsInt)
{
int nSize = str.GetLength();
int ReturnValue = 0;
int DotCounter = 0;
bool bContiue = true;



//-------Case of integer--------------------------
if ( bIsInt )
{
// Goes over the string characters
for ( int i=0; i < nSize; i++ )
{
ReturnValue = isdigit( str.GetAt(i) );
if ( !ReturnValue )
{
CString strChar = str.GetAt(i);
if ( (i == 0) && (strChar=="-") )
bContiue = true;
else
bContiue = false;
}

if ( !bContiue )
return false;
}

return true;
}

//-------Case of double----------------------
// Goes over the string characters
for ( int i=0; i < nSize; i++ )
{
ReturnValue = isdigit( str.GetAt(i) );
if ( !ReturnValue )
{
CString strChar = str.GetAt(i);
if ((i == 0) && (strChar=="-") )
bContiue = true;
else if ( (i!=0) && (strChar== ".") && (DotCounter==0) )
{
DotCounter = 1;
bContiue = true;
}
else
bContiue = false;
}

if ( !bContiue )
return false;
}

return true;
}

Wednesday, May 13, 2009

C++ - How to convert between 'CString' and 'std::string'?

Simple But Still Annoying



'CString' to 'std::string'

Code:


CString cs("Hello");
std::string s((LPCTSTR)cs);


'std::string' to 'CString':

Code:

std::string s("Hello");
CString cs(s.c_str());

 
Home | About | Link | Link
Simple Proff Blogger Template Created By Herro | Inspiring By Busy Bee Woo Themes