# Thread: fail to count digit of an integer (fail at 9)

1. ## fail to count digit of an integer (fail at 9)

hi guys
I"m new to C++. I'm actually physics major and conducting self-study for visual C++.
Any help will be greatly appreciated

I came across a problem of counting how many digits are there in an input integer.
My code is
Code:
```#include <iostream>
using namespace std;
int main()
{
int number;
int digit=0;

cout << "Enter an integer number:";
cin >> number;
cout << endl;
while (number)
{
number /= 10;
digit++;
}
cout << "Number of digit: " << digit << endl;
return 0;
}```
It worked well, but as I typed in more-than-9-digit number, the output was just 9
for example:
Input: 1234567890123
output: 9 (supposed to be more than that!)

I kept checking but didn't get any answer!
Can you please help?
thanks!!

2. just put this statement after reading the number:
Code:
`cout << "You entered: " << number << endl;`
and you will get to know yourself!

3. What manav is hinting at is that integers have a limited range, usually a bit more than 2000000000. So if you enter a much larger number, either the whole number won't be accepted, or the number will drop the upper part (and this gets complicated, because it relies on the binary formatting of the number, so it's not easy to say from the decimal input what the number will actually be). By doing what manav says and output what was input, you can see if the number is accepted in full or not.

I beleive one of the error bits gets set in the input stream when a number is overflowing (too big to be accepted), but I'm not 100% sure.

--
Mats

4. thanks guys!!
that's really helpful!

you guys reminded that int has certain range and I went over it!

Popular pages Recent additions