这个函数是用于判断输入字符是否为数字的。如果是返回true,否则返回false。有两点需要注意:
第一,判断的是字符而不是字符串(你输入453它当作字符串处理);
第二,返回true表示的是数字
例子:
// locale_is_digit.cpp
// compile with: /EHsc
#include
#include
using namespace std;
int main( )
{
locale loc ( "German_Germany" );
bool result1 = isdigit ( 'L', loc );
bool result2 = isdigit ( '@', loc );
bool result3 = isdigit ( '3', loc );
if ( result1 )
cout << "The character 'L' in the locale is "
<< "a numeric character." << endl;
else
cout << "The character 'L' in the locale is "
<< " not a numeric character." << endl;
if ( result2 )
cout << "The character '@' in the locale is "
<< "a numeric character." << endl;
else
cout << "The character '@' in the locale is "
<< " not a numeric character." << endl;
if ( result3 )
cout << "The character '3' in the locale is "
<< "a numeric character." << endl;
else
cout << "The character '3' in the locale is "
<< " not a numeric character." << endl;
}
Output
The character 'L' in the locale is not a numeric character.
The character '@' in the locale is not a numeric character.
The character '3' in the locale is a numeric character.
参考资料:MSDN of VS2005
=======================================
re:“我输入任意长度的数,应该用什么函数进行判断?”
据我所知好像没有这样现成的函数,可以自己写一个。。
我的想法是设一个string类型的变量(这里用string而不用字符串数组是因为string没有空间的限制),用cin>>string;然后对string的字符逐个判断其ASCII码,小于'0'或大于'9'则表示不是数字....
不过这样好像比较麻烦..呃..你看着办吧...
其实idigit()只是判断单个字符,不能用来判断超过一个数字的整数。
可以直接判断输入流是否正确,如:
int a;
cin>>a;
然后通过cin.good()或cin.fail()函数来判断。如果输入流产生错误,再次使用需要调用cin.clear()来清除错误位,clear里面的参数请参看有关手册。