C++/MFC solution Convert string to integer only such that if it is alphanumeric or float it should show as invalid values.
-
Hi All, I want to covert a string to integer in MFC.Consider the string as char str[10]; and integer as int num; My case is as following:- 1) Suppose str = "10" num should be = 10 2) Suppose str = "10.88" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. 3) Suppose str = "10ab" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. Is there any direct API by which we can identify the above cases. I have tried atoi() but it returns the remaining integer value after removing the float or alphanumeric values. Or I have to try it by creating my own method.
-
Hi All, I want to covert a string to integer in MFC.Consider the string as char str[10]; and integer as int num; My case is as following:- 1) Suppose str = "10" num should be = 10 2) Suppose str = "10.88" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. 3) Suppose str = "10ab" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. Is there any direct API by which we can identify the above cases. I have tried atoi() but it returns the remaining integer value after removing the float or alphanumeric values. Or I have to try it by creating my own method.
You could use
strtol()
[^] and its variants, as it will give you a pointer to the character that stops the scan of the string. You can then check this character and decide what actions to take. I do not know of any MFC class that offers a more comprehensive solution, but a search of the MFC library should tell you.One of these days I'm going to think of a really clever signature.
-
Hi All, I want to covert a string to integer in MFC.Consider the string as char str[10]; and integer as int num; My case is as following:- 1) Suppose str = "10" num should be = 10 2) Suppose str = "10.88" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. 3) Suppose str = "10ab" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. Is there any direct API by which we can identify the above cases. I have tried atoi() but it returns the remaining integer value after removing the float or alphanumeric values. Or I have to try it by creating my own method.
Not sure if this is applicable to MFC, but the stringstream library will do this:
#include
#includeint main( )
{
std::string inString( "10.88" );
int outNum;std::stringstream ss( inString );
ss >> outNum;
std::cout << "As Number [" << outNum << "]" << std::endl;
}Sample output would be: As Number [10]
-
Not sure if this is applicable to MFC, but the stringstream library will do this:
#include
#includeint main( )
{
std::string inString( "10.88" );
int outNum;std::stringstream ss( inString );
ss >> outNum;
std::cout << "As Number [" << outNum << "]" << std::endl;
}Sample output would be: As Number [10]
-
Hi All, I want to covert a string to integer in MFC.Consider the string as char str[10]; and integer as int num; My case is as following:- 1) Suppose str = "10" num should be = 10 2) Suppose str = "10.88" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. 3) Suppose str = "10ab" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. Is there any direct API by which we can identify the above cases. I have tried atoi() but it returns the remaining integer value after removing the float or alphanumeric values. Or I have to try it by creating my own method.
You could simply use
atoi()
to convert to an integer, then format that value to a string and compare to your original string. Since you are not asking for anything super efficient or elegant, this should do the trick.CString str = "10.88";
int nValue = atoi(str);
CString strValue;
strValue.Format("%d", nValue);
if (strValue != str)
{
nValue = 0;
}Soren Madsen
-
Hi All, I want to covert a string to integer in MFC.Consider the string as char str[10]; and integer as int num; My case is as following:- 1) Suppose str = "10" num should be = 10 2) Suppose str = "10.88" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. 3) Suppose str = "10ab" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. Is there any direct API by which we can identify the above cases. I have tried atoi() but it returns the remaining integer value after removing the float or alphanumeric values. Or I have to try it by creating my own method.
CString str=10.27; CString res = str.SpanIncluding(_T("0123456789")); if(res == str) { int nValue = atoi(str); } else { MessageBox("string is not integer"); } //Cheers