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