943,816 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 10640
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 3rd, 2008
0

count digits

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
savinki is offline Offline
29 posts
since Apr 2008
Jun 4th, 2008
0

Re: count digits

You'll need to use the modulo (or remainder) operator: %

It does what it says:
cout << (123 % 10);

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Jun 4th, 2008
0

Re: count digits

Click to Expand / Collapse  Quote originally posted by savinki ...
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/c...ath/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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Jun 4th, 2008
0

Re: count digits

or....
  1. int getNumDecimalDigits(int value)
  2. {
  3. char myString[16];
  4. sprintf(myString,"%d",value);
  5. return strlen(myString);
  6. }



.
Last edited by jephthah; Jun 4th, 2008 at 2:21 am.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jun 4th, 2008
-1

Re: count digits

Click to Expand / Collapse  Quote originally posted by savinki ...
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
Hey Saviniki try this code:
#include<stdio.h>
void main()
{
long int a;
int count=0,;
printf("Enter a interger :");
scanf("%d",&a);
while(a>0)
{
a=a/10;
count++;
}
printf("No. of digits in the no entered : %d",count);
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deepak1331 is offline Offline
2 posts
since Dec 2007
Jun 4th, 2008
0

Re: count digits

@Deepak
Do not give away the solution. Read the last point of this link http://www.daniweb.com/forums/thread78223.html

Also posting C code in C++ forum is not a good idea that too without code tags.
Last edited by hammerhead; Jun 4th, 2008 at 4:37 am.
Reputation Points: 46
Solved Threads: 24
Posting Whiz in Training
hammerhead is offline Offline
248 posts
since May 2006
Jun 4th, 2008
-1

Re: count digits

Quote ...
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:

CPP Syntax (Toggle Plain Text)
  1. // Will work with negative numbers
  2. int CountDidgets(int val) {
  3. int d = 1, c;
  4. if (val >= 0) for (c = 10; c <= val; c *= 10) d++;
  5. else for (c = -10 ; c >= val; c *= 10) d++;
  6. return (c < 0) ? ++d : d;
  7. }
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jun 4th, 2008
0

Re: count digits

> That is not the best way to do it
There's no such thing as the best overall way to do it. Your way might be better in Edward's program but worse in jephthah's, or vice versa. Though jephthah's function has the benefit of simplicity.

> heres a better way
The results are identical for the most part. Ed would argue that the sign has no business being counted as a digit, but that's something that depends on the needs of the program as well. One big problem with your better way is that it fails to handle INT_MIN and INT_MAX.

Edward would do something more like this for the counting method:
C++ Syntax (Toggle Plain Text)
  1. int CountDigits(int value)
  2. {
  3. int count = 0;
  4.  
  5. value = std::abs(value);
  6.  
  7. do {
  8. ++count;
  9. value /= 10;
  10. } while (value != 0);
  11.  
  12. return count;
  13. }
Last edited by Radical Edward; Jun 4th, 2008 at 3:40 pm.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Jun 4th, 2008
0

Re: count digits

Quote ...
The results are identical
Not exactly, the function I made is Much 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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jun 4th, 2008
0

Re: count digits

> the function I made is Much more efficient
Edward would argue that an infinite loop for some inputs isn't much more efficient. Ed would also argue that any sane programmer will use a slow and correct function rather than a fast and broken function. Finally, pushing for efficiency at the cost of clarity without bottleneck benchmarks to back up the decision is usually a bad idea.

> trying to assign it to a char buffer and it wont have enough space if it's a negative number
That makes sense if all you use this function for is allocating buffers. There are other uses as well, and counting the sign would be treated as an error in many of them. I think you should avoid counting the sign and let the caller decide what to do:
C++ Syntax (Toggle Plain Text)
  1. int n = CountDigits(value);
  2.  
  3. if (value < 0 || includeSign)
  4. ++n;
  5.  
  6. ...
But that's just a personal preference. As long as your choice is well documented, the caller can do the opposite just as easily:
C++ Syntax (Toggle Plain Text)
  1. int n = CountDigits(value);
  2.  
  3. if (value < 0)
  4. --n;
  5.  
  6. ...
Last edited by Radical Edward; Jun 4th, 2008 at 4:17 pm.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: golden section program problems
Next Thread in C++ Forum Timeline: C++ operator new gets memory in shared memory





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC