CString
-
See
isdigit();
How to read your string?
_**
**_
WhiteSky
WhiteSky wrote:
atoi(m_Str);//return value is 0
If the string is as 12a3 then Index would be 12 and not 0 inspite of the fact that there exists a character in the third place.
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
See
isdigit();
How to read your string?
_**
**_
WhiteSky
-
Hi, is there an easy way (a function) to test if a CString contains only numbers? Thanks
Here's a small function I use for validating user input:
bool IsNumeric(const CString& str)
{
if(str.IsEmpty())
return false;for(int i = 0; i < str.GetLength(); i++) if(str\[i\] < '0' || str\[i\] > '9') return false; return true;
}
It only takes decimal non-negative integers into account. It's easy to extend for other types of numbers, should you want to.
-- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!
-
Hi, is there an easy way (a function) to test if a CString contains only numbers? Thanks
speedy4711 wrote:
test if a CString contains only numbers?
here's one more solution. You can give this a try...
CString s="253453"; CComVariant v; v=s; if(v.ChangeType(VT_INT)==S_OK) { CString str;str.Format ("%d",v.intVal ); AfxMessageBox (str); }
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
Hi, is there an easy way (a function) to test if a CString contains only numbers? Thanks
I looked over the responses, and I want to point to two possible problems. The first may be a leading Zero ( "0123" ) and the other is a value with a comma or a sign ( "-1,5" or "+ 2.7" ) I would resolve this with checking every char of being a digit or allowed other char.
Greetings from Germany
-
speedy4711 wrote:
CString contains only numbers?
CString str="1142654"; int i=atoi (str); CString str1;str1.Format("%d",i); if(str.GetLength ()==str1.GetLength ()) AfxMessageBox("string is pure number"); else AfxMessageBox ("Characters exist");
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
_AnShUmAn_ wrote:
if(str.GetLength ()==str1.GetLength ())
Doesn't work with eg. CString str="0x1142654";
-
Here's a small function I use for validating user input:
bool IsNumeric(const CString& str)
{
if(str.IsEmpty())
return false;for(int i = 0; i < str.GetLength(); i++) if(str\[i\] < '0' || str\[i\] > '9') return false; return true;
}
It only takes decimal non-negative integers into account. It's easy to extend for other types of numbers, should you want to.
-- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!
Jörgen Sigvardsson wrote:
for(int i = 0; i < str.GetLength(); i++) if(str[i] < '0' || str[i] > '9') return false;
Doesn't work with eg. CString str="0x1142654";
-
Jörgen Sigvardsson wrote:
for(int i = 0; i < str.GetLength(); i++) if(str[i] < '0' || str[i] > '9') return false;
Doesn't work with eg. CString str="0x1142654";
How often do you accept hexadecimal user input? And how hard would it be to extend it? I never claimed it would recognize all possible numeric syntaxes...
-- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!
-
_AnShUmAn_ wrote:
if(str.GetLength ()==str1.GetLength ())
Doesn't work with eg. CString str="0x1142654";
SilentSilent wrote:
Doesn't work with eg.
Since this value you are entering is a string x is treated as a character only, it doesn't represent a hexadecimal number nor did I check for this.So I would try and provide a solution for this... Thanks for pointing this out...
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
Hi, is there an easy way (a function) to test if a CString contains only numbers? Thanks
Why don't you just use
strtol()
?
"Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.
"Judge not by the eye but by the heart." - Native American Proverb
-
Hi, is there an easy way (a function) to test if a CString contains only numbers? Thanks
Here is a base-10 checker that allows for ,'s and -'s (but doesn't check for placement). You can easily extend it to check for proper placement of special characters (,'s, and -'s) as well as other base numbers and floating point numbers.
// function to check for base-10 digits only bool is_digit_base10(const CString& str) { static CString allowedChars = _T("0123456789,-"); const unsigned int length = str.GetLength(); for (unsigned int i = 0; i < length; ++i) { char buffer[2] = {0}; buffer[0] = str[i]; if (-1 == allowedChars.FindOneOf(buffer)) { return false; } } return true; }
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac