Coupon Accepted Successfully!



The output formats can be controlled using manipulators. The header file iomanip.h has a set of functions. The effect of these manipulators is similar to ios class member functions. Every ios member function has two formats. The first format is used for setting and the second format is used to understand the previous setting. But the manipulator does not return the previous setting. The manipulator can be used with cout() statement as follows.


cout<<m1 <<m2 <<v1;


Here, m1 and m2 are two manipulators and v1 is any valid C++ variable.

Table describes the most useful manipulators. The manipulators hex, dec, oct, ws, endl, and flush are defined in iostream.h. The manipulators setbase, width(), fill(), etc., that require an argument are defined in iomanip.h.

Table: Pre-defined Manipulators



setw (int n)

The field width is fixed to n


Sets the base of the number system

setprecision(int p)

The precision point is fixed to p

setfill (char f)

The fill character is set to the character stored in variable f

setiosglags(long l)

Format flag is set to l

resetiosflags(long l)

Removes the flags indicated by l


Splits a new line


Omits white space on input


Does not omit white space on input


Adds null character to close an output string


Flushes the buffer stream


Locks the file associated with the file handle


Omits the leading white spaces present before the first field

hex, oct ,dec

Displays the number in hexadecimal, octal, and decimal format

3.42 Write a program to display formatted output using manipulators.

Explanation: In the above program, the manipulator setw (10) sets the field width to 10. The message “hello” is displayed at 10th column. The endl inserts a new line. In the second cout() statement, the setprecision (2) manipulator sets the decimal point to 2. The number 2.5555 will be displayed as 2.56 at column 15. The manipulator setiosflag sets the hexadecimal setting for the display of number. The last cout() statement displays the equivalent hexadecimal of 84, that is, 54.


3.43 Write a program to display the given decimal number in hexadecimal and octal format.

Explanation: In the above program, the integer variable x is declared and initialized with 84. The first cout statement displays the decimal number to its equivalent hexadecimal number. The manipulator hex converts a decimal number to its hexadecimal equivalent. The second cout statement converts a decimal number to its equivalent octal number.

3.44 Write a program to read a number in hexadecimal format using cin statement. 
Display the number in decimal format.

Explanation: In the above program, the cin statement reads a number in hex format. The cout statement displays its equivalent decimal number with the use of dec manipulator.


3.45 Write a program to demonstrate the use of endl manipulator.

Explanation: In the above program, endl manipulator is used to split the line. The two strings are displayed on two separate lines.


3.46 Write a program to demonstrate the use of flush() statement.

Explanation: In the above program, the statement flush (cout) flushes the buffer. This statement can be used as cout<<flush. For more information on buffer, read Section 3.4.

Test Your Skills Now!
Take a Quiz now
Reviewer Name