-
Cout control
I'm working at the Exercises at the end of chapter 4 of my old C++ instruction book, and the first one which has the out put of ints be limited to 10 columns and lines seemed easy untill I tried to have the numbers line up right. I need to find out the right way to control the output so short and long numbers line up right.
Is there some instructions on that?
-
You could use if() statements to find out how much space they need:
Code:
if(number < 10)
cout << " " << number;
else if(number < 100)
cout << " " << number;
else if(number < 1000)
cout << " " << number;
else if(number < 10000)
cout << " " << number;
If you're using decimals then my method may not work too well lol. Good luck soldier!
-
Try out std::setw().
Example code:
Code:
#include <iostream>
#include <iomanip>
int main()
{
unsigned int num1 = 51234511;
unsigned int num2 = 12;
std::cout << std::setw(10) << num1 << std::endl << std::setw(10) << num2 << std::endl;
std::cin.get();
return 0;
}
My output: