Reputation: 2510
I am new to C++ and I have studied some basics of C language. 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!
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. Thank you
Upvotes: 2
Views: 14124
Reputation: 7111
I can see where your confusion comes from. The prototype of isdigit
says it takes a single int
parameter; however, all parameters of type int
are digits, so that would be pointless to check!
Here's when you can see the big difference between cplusplus.com and cppreference.com. The former shows little information, while the latter explains a lot more. cppreference gives you the real hint:
The behavior is undefined if the value of ch is not representable as unsigned char and is not equal to 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]. As others have pointed out, 108
is actually the ASCII character 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.
Upvotes: 4
Reputation: 860
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. you can check this page for more help on 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...
}
Upvotes: 0
Reputation: 655
Function Prototype of 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
. Because 108 is equivalent to 'l'
and 'l'
is not a digit.
Now pass a = 48
to the function because 48 equivalent to char '0'
now the function will return
true
.
You can also read this and this tutorial for more.
Upvotes: 2
Reputation: 27528
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.
Second, std::isigit
has a lousy, confusing legacy interface. 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. The int
you pass to the function represents a single character; whether the mapping is according to ASCII or not is not mandated by C++ and thus implementation-defined.
Nevertheless, your C++ implementation very likely uses ASCII or a superset thereof. In ASCII, 108 is the lower-case letter 'l'. isdigit
therefore returns false.
Upvotes: 6
Reputation: 36483
isdigit
uses the character representation of the int
value 108
, which is ASCII for l
, which is not a digit.
Upvotes: 2