In C++, if you want to get output then cout is used. All functions are in iomanip. You can call by the use of std.
Handling with Spacing problems with the use of iomanip
If you want to get an output in a well organized way, then it is necessary to give proper spaces. In such a program, things should be adjusted accurately. So, here is some information related to spacing.
Arranging the turf breadth with the help of setw
For arranging smallest amount of gaps among outputs, std::setw is used. it puts insertion operator between the output. It gives the space of an int. if the input is less than the space, then gap will adjusted for stuffing.
cout<<setw(11)<<"eleven"<<"five"<<"five";
It will give output in this way:
If there are lengthy constituent then it is probable to indicate the length for output during execution. Even you can alter the stuffing with the use of setfill. It is clear that setfill can be used for operating “stream”.
With the use of this code, padding char looks like a dash. Now, the breadth of the coming output will be approximately 81. Now the result will be:
As the padding char has become a dash so you have to change it for new execution.
Arranging the text in a line with the help of iomanip
You can indicate the output to right or left with the help of flags.
Using the facts of “iomanip”:
We have an idea of gaps and alignment, so our data will be printed rightly.
struct person
{
string firstname;
string lastname;
};
Your output will be arranged in a pleasant way if we have vector related to the persons.
int field_one_width = 0, field_two_width = 0;
for ( vector<person>::iterator iter = people.begin();
iter != people.end();
++iter )
{
if ( iter->firstname.length() > field_one_width )
{
field_one_width = iter->firstname.length();
}
if ( iter->lastname.length() > field_two_width )
{
field_two_width = iter->lastname.length();
}
}
for ( vector<person>::iterator iter = people.begin();
iter != people.end();
++iter )
{
cout<<setw(field_one_width)<<left<<iter->firstname;
cout<<" ";
cout<<setw(field_two_width)<<left<<iter->lastname;
}
We can locate new space for lengthy names by the use of setw.
Publishing Figures
If you want to get an authentic output, then it is better to write a value leading to the prefix “ox”.
In order to locate the utmost numbers, it is suggested to use setprecision.
It is seen that the setprecision modifies the precision awaiting for the new turn when it will be surpass
to a stream.
production in diverse bases
The function setbase will give back a value in the form of 10, 16 or 8. The key in will be interpret as num but will be published in particular base.
In spite pf writing the full name, you can write it as dec, hex, and oct. the digits in decimal form will get published as it is.
std::cout<< setbase(18) << 36;
Now, you are able enough to get your output without writing “printf”.
Leave a Reply