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

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;
}``````
``````#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``````

@ 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);
}``````

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);``````

ceil(log(integer)/log(10))

Be a part of the DaniWeb community

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