# Thread: counting # of digits

1. ## counting # of digits

Hello,

I have been trying to count the number of digits in an integer. I know I am close, but I am not quite sure what I am doing wrong. Could someone please point me in the right direction?

Code:
```using namespace std;
int main()
{
long A;
long B;
int n=0;

cout << "enter value: " ;
cin >> A;
cin.ignore();

if (1000000000 < A >= 0) {
++n;
(A /= 10);
cout << A << " is " << n << " digits long" << endl;
}

}```
My output is always 1 for A. I know I am just misunderstanding something simple.

Thank you in advance for any help on this problem.

2. You need some sort of loop, that keeps dividing the value by 10 and counting how many times that can be done.

Code:
`if (1000000000 < A >= 0)`
WTF is that?

3. ## Got it

I think I just got it.

Code:
```#include <iostream.h>
#include <windows.h>
#include <ctype.h>

using namespace std;
int main()
{
long A;
long int
long B;
int n=0;

cout << "enter value: " ;
cin >> A;
cin.ignore();
int C = A;
do {
++n;
A /= 10 ;
}
while (A!=0)
;
cout << C << " is " << n << " digits" << endl;
return n;
}```
Thanks for the feedback though, and the wtf part was lack of sleep while messing with this. I didn't realize I had left that in the posted code.

4. Alternatively, you could do something like this:
Code:
`if (C>0) n=log10(static_cast<double>(C));`

5. Originally Posted by NeonBlack
Alternatively, you could do something like this:
Code:
`if (C>0) n=log10(static_cast<double>(C));`
log10 of 100 is 2. Number of digits in 100 is not 2.

Correct formula is:
Code:
`std::floor(std::log10(static_cast<double>(C))+1)`