Top 5 C++ Manipulators Every Programmer Should Know

Posted by Utkarsh Jain 2 Comments
The main purpose of C++ manipulators is to format the output. These manipulators increase the enhance ability in the program.These manipulators allow the programmer to show data in different according to his wish. Some important manipulators used in C++ are as follows:

1: endl


2: setw

3: setprecision

4: showpoint

5: setfill


To use these manipulators in a program, iomanip.h shoud be included in the program. However one manipulator called endl can be used without adding that header file. And these manipulator functions are designed to be used in conjunction with the insertion(<<) and extraction(>>) operators. For example


cout<<endl;

'endl' Manipulator:

endl is probably the most used manipulator in C++. The word endl stands for end of line and it is used to move the cursor to the beginning of next line. It works similar to '\n' escape sequence and requires no parameter.

Syntax:

cout<<endl;

Example:

cout<<"Hello"<<endl<<"World";

The above line first prints "Hello" and then endl manipulator displays a new line and then "World" is printed on the next line.



'setw' manipulator:

The setw manipulator is used to display the value of an expression in specified columns. The value of expression can be string or number. If the value of expression is less than specified columns then the additional columns are left blank from left side. The word setw stands for set width.

Syntax:

setw(n)

The n indicates the number of columns in which the value is to be displayed. This specified width is just applied to the value that is inserted after it.

Example:

cout<<setw(10)<<"Hello"<<setw(10)<<"C++"<<setw(10)<<"World";


'setprecision' manipulator:


The setprecision manipulator is used to set the number of digits to be displayed after the decimal point. It is applied to all subsequent floating point numbers written to that output stream. The value is rounded with the use of this manipulator. This manipulator has no impact on integer type variables.

Syntax:

setprecision(n)

The n indicates the number of digits to be displayed after the decimal point.

Example:

float n=2.12345;

cout<<setprecision(6)<<n<<endl;

cout<<setprecision(5)<<n<<endl;

cout<<setprecision(4)<<n<<endl;

cout<<setprecision(3)<<n<<endl;





'showpoint' manipulator:


In C++, the decimal part of a floating-point number is not displayed if it is zero by default. The showpoint manipulator is used to display the decimal part even if the decimal part is zero. When this manipulator is set then all the floating values being used in that stream displays the trailing zeros after decimal point. There is also a noshowpoint manipulator which tells the compiler not to show decimal part if it is zero. The precision setting can be modified using the setprecision manipulator.

Syntax:

cout<<showpoint;

Example:

float a = 30;

cout<<showpoint<<a<<endl;

cout<<noshowpoint<<a;



'setfill' manipulator:


The setfill manipulator is used to replace the leading or trailing blanks in the output by the specified character. It requires one parameter to specify the fill character. The parameter can be a character constant , character variable or an integer that represents the fill character in ASCII system. The manipulator must be used with fixed width output.


Syntax:

setfill('c');

Here c indicates a character or its equivalent ASCII value.

Example:

cout<<setw(18)<<setfill('@')<<"Hello World"<<endl;

cout<<setw(18)<<setfill('+')<<"Hello World"<<endl;

cout<<setw(18)<<setfill('*')<<"Hello World"<<endl;




Author Bio


Kamal Choudhary is a tech geek who writes about c++ programming tutorials on C++ Beginner. He is a student of computer science in University of Gujrat, pakistan. He loves to write about computer programming. You can find his full bio here. Follow him on twitter @ikamalchoudhary
Read More
back to top