counting # of digits

This is a discussion on counting # of digits within the C++ Programming forums, part of the General Programming Boards category; Hello, I have been trying to count the number of digits in an integer. I know I am close, but ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    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. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    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. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Alternatively, you could do something like this:
    Code:
    if (C>0) n=log10(static_cast<double>(C));
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by NeonBlack View Post
    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)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting quantity of different digits
    By cowa in forum C Programming
    Replies: 1
    Last Post: 11-17-2009, 11:09 AM
  2. Counting number of digits in a variable
    By Saeid87 in forum C Programming
    Replies: 6
    Last Post: 06-05-2009, 01:13 PM
  3. Counting number of digits?
    By scatterice in forum C Programming
    Replies: 7
    Last Post: 05-12-2009, 01:30 PM
  4. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  5. Counting integer digits
    By Lionmane in forum C Programming
    Replies: 22
    Last Post: 05-24-2005, 10:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21