Manipulators (8)
Manipulators (8)
Manipulators
• Manipulators are helping functions that can modify input/output
stream.
• Manipulators are special functions that can be included in the I/O
Statement to alter the format parameters of a stream.
• Used to format the data display
• Include the header file iomanip.h
Manipulators without arguments:
• The most important manipulators defined by the IOStream library are
provided below.
• endl: It is defined in ostream. It is used to enter a new line and after
entering a new line it flushes (i.e. it forces all the output written on the
screen or in the file) the output stream.
• ws: It is defined in istream and is used to ignore the whitespaces in the
string sequence.
• ends: It is also defined in ostream and it inserts a null character into the
output stream. It typically works with std::ostrstream, when the associated
output buffer needs to be null-terminated to be processed as a C string.
• flush: It is also defined in ostream and it flushes the output stream, i.e. it
forces all the output written on the screen or in the file. Without flush, the
output would be the same, but may not appear in real-time.
Manipulators with arguments
• setprecision (val): It sets val as the new value for the precision of
floating-point values.
It is an output manipulator that controls the number of digits to
display after the decimal for a floating point integer.
Syntax:
setprecision (int p)
Example:
• float A = 1.34255;
• cout <<setprecision(3) << A << endl;
Manipulators with arguments
• Manipulator Meaning