0% found this document useful (0 votes)
14 views

Manipulators (8)

Manipulators are functions that modify input/output streams and format data display in C++. Key manipulators include 'endl' for new lines, 'setw' for setting field width, and 'setprecision' for controlling decimal precision. They are included via the iomanip.h header file and can be used in various ways to enhance output formatting.

Uploaded by

devarsh.uchat123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Manipulators (8)

Manipulators are functions that modify input/output streams and format data display in C++. Key manipulators include 'endl' for new lines, 'setw' for setting field width, and 'setprecision' for controlling decimal precision. They are included via the iomanip.h header file and can be used in various ways to enhance output formatting.

Uploaded by

devarsh.uchat123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Manipulators

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

• setw (val): It is used to set the field width in output operations.


• The setw() function is an output manipulator that inserts whitespace
between two variables. You must enter an integer value equal to
the needed space.
Syntax:
setw ( int n)
As an example,
int a=15; int b=20;
cout << setw(10) << a << setw(10) << b << endl;
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

setw (int n) To set field width to n

setprecision (int p) The precision is fixed to p


Setw( )
• #include <iomanip>
• #include <iostream>
• int main() {
• int x = 42;
• double y = 3.14;
• std::cout << std::setw(10) << x << std::setw(10) << y << std::endl;
• return 0;
•}
Setprecision( ) cout << num << endl;
#include <iomanip>
#include <ios>
#include <iostream> // Using setprecision()
using namespace std; cout << "Setting the precision
using"
int main()
{ << " setprecision to 9
: \n "
// Initializing the decimal
double num = 3.142857142857; << setprecision(9);
cout << "Before setting the
precision: \n"
<< num << endl; cout << num << endl;
// Using setprecision()
cout << "Setting the precision
using" return 0;
}
<< " setprecision to 5: \n"
<< setprecision(5);
Setbase()
• Hex Value = 64
• #include <iostream>
• #include <iomanip>
• using namespace std; • Octal Value= 144
• int main()
• {
• int number = 100;
• Setbase Value= 144
• cout << "Hex Value =" << " " << hex << number << endl;
• cout << "Octal Value=" << " " << oct << number << endl;
• cout << "Setbase Value=" << " " << setbase(8) << number <<
• Setbase Value= 64
endl;
• cout << "Setbase Value=" << " " << setbase(16) << number <<
endl;
• return 0;
• }

You might also like