C++ isdigit()函数,参数只能是字符类型

/*
    有关isdigit()函数,参数只能是字符类型
*/
#include<iostream>
#include<cctype>

using namespace std;

int main()
{
    /* 无论输入什么数字,结果都是非数字
    int a;
    cout << "输入一个数字,非数字自动退出" << endl;
    while (cin >> a)
    {
        if (isdigit(a))
            cout << "是数字";
        else
            cout << "不是数字";
    }
    */

   //要正确使用需要使用字符
    char a;
    cout << "输入一个数字,'#'退出" << endl;
    while (cin >> a && a != '#')
    {
        if (isdigit(a))
            cout << "是数字" << endl;
        else
            cout << "不是数字" << endl;
    }
    return 0;
}
posted @ 2020-09-07 10:39  河马哥  阅读(328)  评论(0编辑  收藏  举报