954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Counting the amount of numbers in one integer?

Hello ladies and gents,

Was wondering if any of you could help me out, how can I count the amount of numbers(digits) there are in one integer, for example:

int integer = 123;

integer has three digits.

int secInteger = 10;

secInteger has two digits.

The reason for asking is, that I want to use this number to actually use "setw(integer)" so that it takes on the width of this number.

I'm trying to create two columns, one wich has an index of i from any number that I enter, the second will have the square of each value of i.

It's for this exercise in Accelerated C++

#include <iostream>
#include <iomanip>

using std::cin;				using std::setw;
using std::cout;			using std::string;
using std::endl;

int squares (const int&, const int &);

int main()
{
	int maxlen;
	cout << "Give an integer number, bigger then 0 for wich "
		    "you wish to calculate it's squares." << endl;

	do
	{
		cin >> maxlen;
		if(maxlen <= 0)
			cout << "You've entered a wrong number, please try again." << endl;
	}
	while (maxlen <= 0);

	int width = maxlen * maxlen;	//this will give me the maximum value, but, I need
									//the amount of digits that it contains.

	cout << "Calculate the squares of " << maxlen << " sequential values." << endl;

	for (int i = 1; i < maxlen; ++i)
		cout << setw(?) << i << setw(?)  << squares(i, i) << endl;
	

	std::cout << "Press enter to exit." << std::endl;
	std::cin.ignore(2, '\0');

	return 0;
}

int squares (const int& x, const int& y)
{
	int result = x * y;

	return result;
}
JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

to find the number of digits in int x

if (x < 0)
	x = -x;
int count = 1;
x = x/10;
while( x > 0)
{
	count++;
	x = x/10;
}
campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
		int a =23545;
		ostringstream o;
		o<<a;
		cout<<o.str().size()<<endl;
   return 0;
}
./Temp
5
winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

@ campkev, darn, I tried to do something like you but dividing by two. Thanks.

@ winbatch, I tried to use a string in which I thought I could use the string.length() to see how much numbers there where. It did give me the correct amount of numbers, but didn't lend itself of being used in combination with the iteration of the loop and I couldn't get it to be incremented either.

So, thanks for your solution gents, here's my exercise with both your examples integrated.

int squares (const int&, const int &);
int maxNumber (int);
int maxWidth (int);

int main()
{
	int maxlen;
	cout << "Give an integer number, bigger then 0 for wich \n"
		    "you wish to calculate it's squares." << endl;

	do
	{
		cin >> maxlen;
		if(maxlen <= 0)
			cout << "You've entered a wrong number, please try again." << endl;
	}
	while (maxlen <= 0);

	int widthNumbers = maxNumber(maxlen);
	int widthMaxNumbers = maxWidth(maxlen);

	cout << "The results are:" << endl;

	for (int i = 1; i <= maxlen; ++i)
		cout << setw(widthNumbers) << i << setw(widthMaxNumbers)  << squares(i, i) << endl;
	

	std::cout << "Press enter to exit." << std::endl;
	std::cin.ignore(2, '\0');

	return 0;
}

int squares (const int& x, const int& y)
{
	int result = x * y;

	return result;
}

int maxNumber (int maxLength)
{
	int count = 1;

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

	return count;
}

int maxWidth(int maxLength)
{
	ostringstream dig;

	int maxwidth = maxLength * maxLength;
	dig << maxwidth;
	maxwidth = dig.str().size();

	
	return (maxwidth + 1);
}
JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

loop is simple to do

std::string maxlen;
int n = 0;
do
	{
		cin >> maxlen;
                n = atoi(maxlen.c_str());
		if(n <= 0)
			cout << "You've entered a wrong number, please try again." << endl;
	} while( n <= 0);
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You