How do I find out how many digits in an integer?
Is there a short way to determine how many digits are in an integer? I'm new to C++, and writing a program that's supposed to a number in one column, and its square in the next. I'm using setw to manipulate the width of the coulms.
Here is those code:
#include <iostream>
#include <iomanip>
using std::cout; using std::setw; using std::streamsize;
int main()
{
const int MAX = 100;
for (int i = 0; i != MAX;++i)
{
cout << setw(2) << i << setw(5) << i * i << std::endl;
}
}
This works fine as is, but when I change MAX to 1000, the output gets screwed. I know it's because neither column is wide enough to hold the output, so they get jammed together. I want to make the program robust, so that the output would scale to accomodate shrinking and growing MAX.
I have an idea that if I can do something like this it would work:
#include <iostream>
#include <iomanip>
using std::cout; using std::setw; using std::streamsize;
int main()
{
const int MAX = 100;
for (int i = 0; i != MAX;++i)
{
int square = i * i;
cout << setw(length_of_i) << i << setw(length_of_square) << square << std::endl;
}
}
Thoughts?
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
>>Is there a short way to determine how many digits are in an integer
In a loop keep dividing it by 10 until the result is 0
while(n > 0)
{
n /= 10;
count++;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I thought of writing a function to do that, but I was sort of looking for an inline solution to see maybe if it was possible. I feel like I'm missing what the book is trying to get me to learn here... Anyway for the sake of moving on I'lll roll with this.
Question's still open for anyone who might know an inline solution.
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
Inline solution below
int __inline count_digits(int n)
{
// blabla
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
He didn't misunderstand.
There are two ways to look at "inline":
1. the OP wants to avoid using function call syntax
2. AD's code actually avoids calling a function
Either way the best solution takes multiple lines of code, as posted.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
I think you misunderstood the other poster.
It was a joke :)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
> I think you misunderstood the other poster.
> I don't think that word means what you think it means.
I amaze at the arrogance and rudeness some people possess. Do you really believe yourself superior to AD's intellect, and mine?
I don't really care much what you think. I know what the word inline means. Look it up yourself.
> The OP was using the way AD wasn't.
Thank you Captain Obvious.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
Ok ok. Just to clarify I wanted to see if I could've done it all within the same function, maybe inline was the wrong word to use. I was hoping someone would point out some sort of manipulator or function or something that I might have missed and was supposed to learn.
Who knows, maybe the book meant for me to use a function, come to think of it.
If anyone has the book Accelerated C++ it's one of the exercises at the end of Chapter Four (or five if you don't start counting at zero). Anyway thanks AD, although Sarehu is right in that the columns still get screwed because I would have to know the length of the widest one before hand. I guess I have to calculate them all first and then store them and print them back.
Thanks for the help guys. I'm marking this as solved, since the solution I had in mind looks quite unreasonable in hindsight.
EDIT: instead of calculating before hand and storing, if I base the length calculations off MAX it should work just fine :)
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140