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?

Recommended Answers

All 12 Replies

>>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++;
}

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.

Inline solution below

int __inline count_digits(int n)
{
   // blabla
}

Question's still open for anyone who might know an inline solution.

There is x ? 1 + static_cast<int>(floor(log10(static_cast<double>(x)))) : 1; , but that solution is dumb (and doesn't consider negative numbers). I say it's dumb because casting to double means you have to deal with precision issues, and because it is tedious to convince yourself that your code is correct for all possible inputs. There is no practical benefit to writing a solution inline, and it's actually a detriment when you'll have to write it multiple times.

Also, you want to set the width to the width of the maximum number you'll have to print in each column. Setting it to the width of i or square is probably not what you want, because the widths of your columns will change for different values.

Inline solution below

I think you misunderstood the other poster.

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.

He didn't misunderstand.

I don't think that word means what you think it means.

There are two ways to look at "inline":

The OP was using the way AD wasn't.

I think you misunderstood the other poster.

It was a joke :)

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

commented: I think you misunderstood me, too. +1

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

I don't understand why you would be touched off by straightforward responses. Also, haven't you ever seen The Princess Bride?

Do you really believe yourself superior to AD's intellect, and mine?

Relative to AD's, I don't know.

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 :)

while(n > 0)
{
   n  /= 10;
   count++;
}

How about negative number? How about if my number is 0?

while (++count && n!=0)
    n /= 10;
commented: you cleared my doubt thank you... +2

hello... how to determine the number of digits in an inputted value?.... please help....

commented: Read the thread -6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.