关于isdigit函数

isdigit函数只能判断字符型是否是数字

如下

#include <iostream>
#include <cstdio>

using namespace std;

int main() {
    char c;//此时输入的是整型数字
    while (cin>>c) {
        if (isdigit(c)) {
            cout<<c<<"是数字"<<endl;
        } else cout<<c<<"不是数字"<<endl;
    }
    return 0;
}

//程序输出如下:

/*
1
1是数字
2
2是数字
3
3是数字
a
a不是数字
b
b不是数字
c
c不是数字
*/

//识别正确
//但是如果把char c 改成int c 则无法识别数字和字符的区别
//更改后的错误程序输出如下:

/*
1
1不是数字
2
2不是数字
3
3不是数字
a

Process returned 0 (0x0)   execution time : 3.267 s
Press any key to continue.
*/


Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐