-2

I'm trying to determinate the amount of digits in integer in C++ and so far I've tried the following methods:

1.

unsigned GetNumberOfDigits (unsigned i)
{
    return i > 0 ? (int) log10 ((double) i) + 1 : 1;
}

2.

int length_of_int(int input){
    int length = 0;
    while(input > 0){
        length++;
        input /= 10;
        if(input == 0)
            length++;
    }
    return length;
}

3.

int NumDigits(int x)  
{  
    x = abs(x);  
    return (x < 10 ? 1 :   
        (x < 100 ? 2 :   
        (x < 1000 ? 3 :   
        (x < 10000 ? 4 :   
        (x < 100000 ? 5 :   
        (x < 1000000 ? 6 :   
        (x < 10000000 ? 7 :  
        (x < 100000000 ? 8 :  
        (x < 1000000000 ? 9 :  
        10)))))))));  
}

And none works in my case, such as "000101" it has 6 digits, but it either says 4 or 3. Any help?

The purpose of this is checking a valid date, in format YYMMDD. I am aware that this type of format has Y2K-error but it's specified in the task it has to be that one.

user2699298
  • 1,446
  • 3
  • 17
  • 33
  • 3
    As an integer, 000101 is the same as 101... so it really has 3 digits. Maybe you should use a string, if left-side 0's matter. It's even easier to get the number of digits for a string, but it also accepts characters. – GabrielF Jan 05 '14 at 03:27
  • 2
    The only way to get 6 digits for 000101 is by taking the number as a string. –  Jan 05 '14 at 03:27
  • @awesomeyi I think you didn't get the gist here. –  Jan 05 '14 at 03:28
  • I got these codes off that topic to be honest. Is there no way to do this with an integer? Because the exercise I'm doing requires an `int` to be used. – user2699298 Jan 05 '14 at 03:29
  • 1
    In additon to the first two comments: As soon as `00010` is interpreted as integer the information about the leading zeros is lost. You have to use a string approach starting from the input on. @user2699298 This is logically impossible. –  Jan 05 '14 at 03:29
  • Either you have misunderstood the exercise or it's a trick question or your teacher/lecturer should lose his license –  Jan 05 '14 at 03:30
  • Then, either you misread your exercise, or it asks you to count only significant digits, which leading 0's are not. You could have thousands of them if you wanted. It's mathematically and programatically the _same_ thing. – GabrielF Jan 05 '14 at 03:31
  • @GabrielF Here's what the deal.. I have to validate a "date", in format YYMMDD. So, if the year is 00 (2000), there is a problem.. – user2699298 Jan 05 '14 at 03:32
  • The length of a year is always 4 (for reasonable non-historical dates) and that of a `YYMMDD` format date always 6. Also `YYMMDD` is usually not a number, it is a string. –  Jan 05 '14 at 03:33
  • @user2699298 then your answer is always 6 isn't it? YYMMDD ... – PierreBdR Jan 05 '14 at 03:33
  • Yes, my answer is "always" 6 (I do checking with that function, unless I enter 00 in the beginning) and then there's a problem. – user2699298 Jan 05 '14 at 03:35
  • 1
    @user2699298 The point is, that you do not need to calculate anything. The result is **always** 6. I believe however that this is an instance of a [XY-Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Maybe you want to post the whole exercise and your ideas. –  Jan 05 '14 at 03:35
  • Well... you could do this using integer... you just keep in mind the 0's are supposed to be there, conventionally. You don't need to count... you **define** it to be 6 digits long. Let's say you wanted to print the value, you do `format("%.6d", yourint)`... you are specifically defining it to be 6 digit long. But again, are you sure you must use integer? I still would use string. – GabrielF Jan 05 '14 at 03:36
  • @user2699298 you don't understand, if you know your number is a date, then you should not check anything and return 6 ... however, you might want to check it's a valid date before – PierreBdR Jan 05 '14 at 03:36
  • Please, edit your question to add the part about it being a date (and not a number) and be very clear about what you want to do – PierreBdR Jan 05 '14 at 03:37
  • @GabrielF Well, I could maybe use a string to take the input, validate and then if valid, convert into integer? – user2699298 Jan 05 '14 at 03:42
  • It's still not clear, @user2699298. You should really add details about the exercise. – GabrielF Jan 05 '14 at 03:42
  • @GabrielF The exercise is way too big. It's a whole project. And it's not in English (in my native language). I need help on this part only, to validate the date. – user2699298 Jan 05 '14 at 03:44
  • You definitely should... if you need to validate the input, then leading 0's are important and you should first validate it as a string (or as a date, if you can use other data types, like [tm struc](http://www.cplusplus.com/reference/ctime/tm/)). But it's not clear why you would need to convert that to integer later. – GabrielF Jan 05 '14 at 03:45
  • @GabrielF Because it's specified in the task by the professor, if I don't, I would get -points. I think I'll take the input as string, validate, check length and then if everything is valid convert into an integer. – user2699298 Jan 05 '14 at 03:46
  • put a 1 in front of those 00's and remove it before using it, by adding 1000000 to the value. (to remove just subtract 1000000), but yeah this is stupid you just have to use string instead of integer.. even databases don't store dates in integer they use DATETIME or just DATE datatype, which are most likely strings. – SSpoke Jan 05 '14 at 03:47
  • 1
    If what you need is to validate a date input, take the input as a string and separate it in 3 different integer variables first: year, month and day... then you validate, checking the apropriate ranges. Then you get all into a single integer by multiplying year by 10000, month by 100 and day by 1. – GabrielF Jan 05 '14 at 03:51

1 Answers1

2

You wrote And none works in my case, such as "000101" it has 6 digits, but it either says 4 or 3. Any help?

if the integer was 000101 then the first 3 zeros would get removed, it would become 101.
If it was a string you just count how much letters in the string.
Seems you want to represent binary I would use bit array for this
Edit: Okay it's not binary it's a date which should be stored into string to avoid this Y2K bug.

int count = strlen("000101");
SSpoke
  • 5,656
  • 10
  • 72
  • 124