You'll need to use the modulo (or remainder) operator: %
It does what it says: cout << (123 % 10);
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
Hi,
wt is the function that can use to count number of digits in a long integer?
e.g. 12345678 -> 8
11 ->2
456 -> 3
6724 -> 4
I don't know that there is a function that does that. You can do two things though. You can use the log10 function from cmath: http://www.cplusplus.com/reference/clibrary/cmath/log10.html
That function takes and returns doubles, so you'll need to do some typecasting and adjust by one.
Or you can set up a for-loop and divide by 10 each time through the loop using integer division. Count how many times you go through the loop.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
or....
int getNumDecimalDigits(int value)
{
char myString[16];
sprintf(myString,"%d",value);
return strlen(myString);
}
:P
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
int getNumDecimalDigits(int value)
{
char myString[16];
sprintf(myString,"%d",value);
return strlen(myString);
}
That is not the best way to do it, heres a better way:
// Will work with negative numbers
int CountDidgets(int val) {
int d = 1, c;
if (val >= 0) for (c = 10; c <= val; c *= 10) d++;
else for (c = -10 ; c >= val; c *= 10) d++;
return (c < 0) ? ++d : d;
}
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
The results are identical
Not exactly, the function I made isMuch more efficient and doesnt make any calls to any other functions. And I think the minus sign should count as a didget as if you are don't, trying to assign it to a char buffer and it wont have enough space if it's a negative number.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
Ive had that function for a year or two now, never failed on me o.0
Ed would also argue that any sane programmer will use a slow and correct function rather than a fast and broken function.
I agree there, although I would rather have both.
But I dont see how its broken.. :icon_confused:
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
You could also change the function very easily so you have a choise:
int CountDidgets(int val, bool signInc = 0) {
int d = 1, c;
if (val >= 0) for (c = 10; c <= val; c *= 10) d++;
else for (c= -10 ; c >= val; c *= 10) d++;
return (signInc && c < 0) ? ++d : d;
}
Now were both happy :)
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
best shmest.
I'm not trying to give bulletproof homework solutions.
i was just pointing out how one could "count characters" just as easily as one could divide/modulo
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
But I dont see how its broken.. :icon_confused:
Edward made a good point there, if you try
cout << CountDidget(INT_MAX) << '\n';
What happens :?:
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
Edward made a good point there, if you try
cout << CountDidget(INT_MAX) << '\n';
Im just gona have to keep changing ;)
template<typename type>
int CountDidgets(type val, bool signInc = 0) {
int d = 1; long long c;
if (val >= 0) for (c = 10; c <= val; c *= 10) d++;
else for (c= -10 ; c >= val; c *= 10) d++;
return (signInc && c < 0) ? ++d : d;
}
Now try this:
cout << CountDidgets<unsigned int>(INT_MAX) << '\n';
Should work now :icon_wink:
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
what is the function that can be used to count the number of days that a thread has been dead and buried?
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179