简体   繁体   English

在C ++中使用isDigit()函数的正确方法

[英]Correct way of using isDigit() function in c++

I am new to C++ and I have studied some basics of C language. 我是C ++的新手,我已经学习了C语言的一些基础知识。 Here's my code snippet. 这是我的代码段。

#include "iostream"

using namespace std;

int main(){
int a=108;
if(!isdigit(a)){
    cout<<"The number is not a digit";
}
else
cout<<"It's a Number!";
}

I dont know why, but it satisfies the condition. 我不知道为什么,但是它满足条件。 It should have outputted, It's a Number! 它应该已经输出, It's a Number! Please correct me and also if u have a better solution to this, do suggest! 请纠正我,如果您对此有更好的解决方案,请提出建议! (To make it more clear) I want to check whether the entered int is actually composed of digits. (为了使其更清楚),我想检查输入的int是否实际上由数字组成。 Thank you 谢谢

First of all, I'm not sure if you realise that there is a difference between a digit and a number . 首先,我不确定您是否意识到数字数字之间存在差异。 A digit is a single character from 0 to 9, a number is composed of digits. 数字是0到9之间的单个字符,数字由数字组成。

Second, std::isigit has a lousy, confusing legacy interface. 其次, std::isigit有一个糟糕的,令人困惑的旧界面。 As documentation will tell you, it takes an int but requires its argument to be representable as unsigned char or EOF to avoid undefined behaviour. 正如文档将告诉您的那样,它采用一个int但要求其参数可表示为unsigned char或EOF,以避免未定义的行为。 The int you pass to the function represents a single character; 传递给函数的int表示单个字符; whether the mapping is according to ASCII or not is not mandated by C++ and thus implementation-defined. 映射是否根据ASCII是否由C ++强制要求,因此不由实现定义。

Nevertheless, your C++ implementation very likely uses ASCII or a superset thereof. 但是,您的C ++实现很可能使用ASCII或其超集。 In ASCII , 108 is the lower-case letter 'l'. ASCII中 ,108是小写字母“ l”。 isdigit therefore returns false. 因此isdigit返回false。

I can see where your confusion comes from. 我可以看到您的困惑来自何处。 The prototype of isdigit says it takes a single int parameter; isdigit的原型表示它需要一个int参数。 however, all parameters of type int are digits, so that would be pointless to check! 但是,所有int类型的参数都是数字,因此检查毫无意义!

Here's when you can see the big difference between cplusplus.com and cppreference.com . 在这里,您可以看到cplusplus.comcppreference.com之间的巨大差异。 The former shows little information, while the latter explains a lot more. 前者显示的信息很少,而后者解释的信息更多。 cppreference gives you the real hint: cppreference给您真正的提示:

The behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF 如果ch的值不能表示为无符号char且不等于EOF,则该行为是不确定的

The function is expecting a value between [0,127] and you can see on the page linked that the digits 0123456789 are represented by the numbers [48,57]. 该函数期望的值为[0,127],并且在链接的页面上您可以看到数字0123456789由数字[48,57]表示。 As others have pointed out, 108 is actually the ASCII character l . 正如其他人指出的那样, 108实际上是ASCII字符l

for (unsigned int i = 0; i < 128; ++i)
{
    if (std::isdigit(i)) 
    {
        std::cout << i << " is a digit";
    }
}

You can't check a number like 108 , you would have to check each digit. 您无法检查类似108的数字,而必须检查每个数字。

isdigit使用int108的字符表示形式,该值是l ASCII ,而不是数字。

Function Prototype of isdigit() isdigit()的函数原型

int isdigit(int argument);

if you pass a=108 to the function it will convert the value to it's equivalent ASCII Value and return the result false . 如果将a=108传递给该函数,它将将该值转换为等效的ASCII值并返回结果false Because 108 is equivalent to 'l' and 'l' is not a digit. 因为108等于'l''l'不是数字。

Now pass a = 48 to the function because 48 equivalent to char '0' now the function will return true . 现在将a = 48传递给该函数,因为48等于char '0'现在该函数将return true

You can also read this and this tutorial for more. 您还可以阅读教程为多。

you are using isdigit wrong, as you were told in the answers above it's meant to be used with character representations, to check whether a certain char is a digit or not. 您使用的isdigit错误,如您在上面的答案中所述,它应与字符表示形式一起使用,以检查某个字符是否为数字。 you can check this page for more help on isdigit: http://www.tutorialspoint.com/c_standard_library/c_function_isdigit.htm 您可以检查此页面以获取有关isdigit的更多帮助: http ://www.tutorialspoint.com/c_standard_library/c_function_isdigit.htm

to your question - I guess you are trying to check if the number you are sending is a single digit number. 您的问题-我想您正在尝试检查发送的号码是否为一位数字。 for this you can simply do: 为此,您可以简单地执行以下操作:

if (a >= 0 && a <= 9){
    // a is a single digit...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM