-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Monday 9 November 2015

C++ manipulators

C++ manipulators

Manipulators are used to change/rotate the output in different styles.They are the mlost common way to control the format of output in C++.

Some of the most important manipulators that are used in C++ are:
  • endl
  • setw
  • setprecision
Today,in my blog,I will discuss the manipulators mentioned above and their functionality in C++.

'endl' Manipulator:

                                      The word  'endl' stands for end of line.It is used to move the cursor to the beginning of the next line.It requires no parameter.

Example:

cout<<"codingwithcplusplus.blogspot.com"<<endl;
cout<<"Blogging my passion";

It will show the output as below:

codingwithcplusplus.blogspot.com
Blogging my passion

'setw' Manipulators:

                                                    The word 'setw' stands for set width.It is used to display the value of an expression in specified columns.The output is right justified by default.It is necessary to include header file "iomanip.h" to use this manipulator.

Example:

cout<<"Hello"<<setw(4)<<"World";

It will show the output as below:

Hello    World

I think no more explanation is required. :-)

'setprecision' Manipulator:

                                               It is used to set the number of digits to be displayed after decimal point.The value is rounded with the use of this manipulator.

Example:

Suppose the value of x=10.0

cout<<setprecision(2)<<x;

It will show the output as:

10.00

Now,consider

cout<<setprecision(5)<<x;

It will show the output as:

10.00000








No comments:

Post a Comment