Wednesday, April 3, 2013

Manipulators


What is a Manipulator?
Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer's choice of display.
Types of Manipulators:
There are numerous manipulators available in C++. Some of the more commonly used manipulators are provided here below:

1)     endl Manipulator:
This manipulator has the same functionality as the 'n' newline character.
2)     setw Manipulator:
This manipulator sets the minimum field width on output. The syntax is:
setw(x)

Here setw causes the number or string that follows it to be printed within a field of x characters wide and x is the argument set in setw manipulator. The header file that must be included while using setw manipulator is .

3)     setfill Manipulator:

This is used after setw manipulator. If a value does not entirely fill a field, then the character specified in the setfill argument of the manipulator is used for filling the fields.

4)     setprecision Manipulator:

The setprecision Manipulator is used with floating point numbers. It is used to set the number of digits printed to the right of the decimal point. This may be used in two forms:

  • fixed
  • scientific
These two forms are used when the keywords fixed or scientific are appropriately used before the setprecision manipulator. The keyword fixed before the setprecision manipulator prints the floating point number in fixed notation. The keyword scientific, before the setprecision manipulator, prints the floating point number in scientific notation.
Do we need separate headerfile for manipulators ? If yes what is the headerfile ?
Yes we need a separate headerfile for manipulators .The declaration for the manipulators (except end1) are not in the usual iostreamheaderfile , but in a separate header file called IOMANIP . When we use these manipulators we must #include this headerfile in our program.

No comments:

Post a Comment