7

I was recently making a program which needed to check the number of digits in a number inputted by the user. As a result I made the following code:

int x;    
cout << "Enter a number: ";
cin >> x;
x /= 10;
while(x > 0)
{
  count++;
  x = x/10;
}

From what I can tell (even with my limited experience) is that it seems crude and rather unelegant.

Does anyone have an idea on how to improve this code (while not using an inbuilt c++ function)?

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
E.O.
  • 794
  • 6
  • 16
  • 24
  • I am assuming `x` is an `int` or else this will *never* reach `0` – Naftali Jul 06 '11 at 19:08
  • who do you do the divison before the loop? – Karoly Horvath Jul 06 '11 at 19:08
  • "Crude and unelegant" is what you're stuck with if you can't use inbuilt functions. – Mark Ransom Jul 06 '11 at 19:17
  • What do you mean, no inbuilt functions? On ARM CPUs for example, the division operator is an inbuilt function. – Zan Lynx Jul 06 '11 at 19:22
  • 1
    Sorry, gotta clarify. I am assuming there is a method to simply count the number of digits in an integer using some function in the C++ standard library(i may of course be wrong). To learn more about C++ I have decided to try to make most of the code myself, just to get a feel of how stuff works hence the attempt at try to count the number of digits "manually". – E.O. Jul 06 '11 at 19:27

7 Answers7

12

In your particular example you could read the number as a string and count the number of characters.

But for the general case, you can do it your way or you can use a base-10 logarithm.

Here is the logarithm example:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double n;
    cout << "Enter a number: ";
    cin >> n;

    cout << "Log 10 is " << log10(n) << endl;
    cout << "Digits are " << ceil(log10(fabs(n)+1)) << endl;
    return 0;
}
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
5
int count = (x == 0) ? 1 : (int)(std::log10(std::abs((double)(x)))))) + 1;
David Hammen
  • 32,454
  • 9
  • 60
  • 108
2

You could read the user input as a string, and then count the characters? (After sanitising and trimming, etc.)

Alternatively, you could get a library to do the hard work for you; convert the value back to a string, and then count the characters:

cin >> x;
stringstream ss;
ss << x;
int len = ss.str().length();
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

If x is an integer, and by "built in function" you aren't excluding logarithms, then you could do

double doub_x=double(x);
double digits=log(abs(doub_x))/log(10.0);
int digits= int(num_digits);
Dan
  • 12,157
  • 12
  • 50
  • 84
1

Given a very pipelined cpu with conditional moves, this example may be quicker:

if (x > 100000000) { x /= 100000000; count += 8; }
if (x > 10000) { x /= 10000; count += 4; }
if (x > 100) { x /= 100; count += 2; }
if (x > 10) { x /= 10; count += 1; }

as it is fully unrolled. A good compiler may also unroll the while loop to a maximum of 10 iterations though.

dascandy
  • 7,184
  • 1
  • 29
  • 50
0
#include<iostream>
using namespace std;
int main()
{
int count=0;
    double x;
    cout << "Enter a number: ";
    cin >> x;
    x /= 10;
    while(x > 1)
    {
      count++;
      x = x/10;
    }
    cout<<count+1;
}
reza
  • 1
-1

Bar the suggestions of reading the number as a string, your current method of counting the number of significant decimal digits is fine. You could make it shorter, but this could arguably be less clear (extra set of parenthesis added to keep gcc from issuing warnings):

while((x = x/10))
  count++;
Lyke
  • 4,575
  • 6
  • 27
  • 26