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

Data Manipulators

The document discusses various manipulators in C++ that are used to format output streams. It lists manipulators without arguments like endl, ends, ws, flush(), and setiosflags(); and manipulators with arguments like setw(), setprecision(), setfill(), and setbase(). Some key manipulators described are endl for new lines, setw() for field width, setprecision() for floating point precision, and setbase() for changing numeric bases.

Uploaded by

Sarthak Roy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Data Manipulators

The document discusses various manipulators in C++ that are used to format output streams. It lists manipulators without arguments like endl, ends, ws, flush(), and setiosflags(); and manipulators with arguments like setw(), setprecision(), setfill(), and setbase(). Some key manipulators described are endl for new lines, setw() for field width, setprecision() for floating point precision, and setbase() for changing numeric bases.

Uploaded by

Sarthak Roy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Manipulators

Data Manipulators
Manipulators are operators/function/keywords used in C++ for formatting output.
The data is manipulated by the programmer’s choice of display.
Manipulators are helping functions that can modify the input/output stream. It
does not mean that we change the value of a variable, it only modifies the I/O
stream using insertion (<<) and extraction (>>) operators.
Manipulators are functions and keywords specifically designed to be used in
conjunction with the insertion (<<) and extraction (>>) operators .
We have to use <iomanip.h> header files for manipulators.

Following are some of the most widely used C++ manipulators:


1. Manipulators without arguments:
2. Manipulators with arguments:
Data Manipulators
1. endl: the endl is an output manipulator to generate a carriage return or line (new line)feed character. The endl may be used
several times in a C++ statement.
For example:
cout << “ a “ << endl << “b” << endl;
2. Setw () The setw ( ) stands for the set width. The setw ( ) manipulator is used to specify the minimum number of character
positions on the output field a variable will consume.
The general format of the setw manipulator function is
setw( int w )
Which changes the field width to w, but only for the next insertion. The default field width is 0.
For example:
cout << setw (10) << a << endl;
3.Setfill() The setfill ( ) manipulator function is used to specify a different character to fill the unused field width of the value.
The general syntax of the setfill ( ) manipulator is
setfill( char f)
which changes the fill character to f. The default fill character is a space.
For example,
cout<<setw(10)<< setfill ( ‘ . ’ ) <<a; / / fill a dot ( . ) character
Data Manipulators
4. Setprecision(): The setprecision ( ) is used to control the number of digits of an output stream display of a floating point value.
The setprecision ( ) manipulator prototype is defined in the header file <iomanip.h>.
The general syntax of the setprecision manipulator is: Setprecision (int p)
Which sets the precision for floating point insertions to p. The default precision is 6
Example: float x = 0.1;
cout << setprecision(3) << x << endl;
5. Setbase() : The setbase() manipulator is used to convert the base of one numeric value into another base. Following are the common base converters in
C++.
dec - decimal base (base = 10)
hex - hexadecimal base (base = 16)
oct - octal base (base = 8)
Example: int value;
cout << “ Enter number” << endl;
cin >> value;
cout << “ decimal base = “ << setbase (10)<<value<<endl;
cout << “ hexadecimal base = “ << setbase (16)<<value<<endl;
cout << “ Octal base = “ << setbase (8) << value << endl;
Data Manipulators
6. ws:This function extracts as many whitespace characters as possible from the current position in the input
sequence. This extraction stops as soon as a non-whitespace character is found. These extracted whitespace
characters are discarded.its work with input stream.
Ex: cout<<"\n Enter the string:";
cin>>ws;
cin>>s;
cout<<s;

7. 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. The flush manipulator does
not insert a newline character into the stream before it flushes the buffer.
Ex: cout.flush();
Data Manipulators
8. ios::left: The left() method of stream manipulators in C++ is used to set the adjustfield format flag for the specified str
stream. This flag sets the adjustfield to left. It means that the number in the output will be padded to the field width by inserting
fill characters at the end. its used with setiosflags().
Synatx: setiosflags(ios::left);
Ex: cout<<setiosflags(ios::left)<<value;

9. ios::right: : The right() method of stream manipulators in C++ is used to set the adjustfield format flag for the specified str
stream. This flag sets the adjustfield to right. It means that the number in the output will be padded to the field width by inserting
fill characters at the starting. its used with setiosflags().
Synatx: setiosflags(ios::right);
Ex: cout<<setiosflags(ios::right)<<value;
10. ios::showpos: Sets the showpos format flag for the str stream.

When the showpos format flag is set, a plus sign (+) precedes every non-negative numerical value inserted into the stream.
Synatx: setiosflags(ios::showpos);
Ex: cout<<setiosflags(ios::showpos)<<value;
Data Manipulators
9.ends: C++ manipulator ends function is used to insert a null terminating character on os. The ends manipulator does not take
any argument whenever it is invoked. It causes a null character to the output.
Ex: cout << "a";
cout << "b" << ends;
cout << "c" << endl;

Output: ab c
Data Manipulators
1. Manipulators without arguments:
• endl
• ends
• ws
• flush()
• setiosflags():
1. ios::showpos
2. ios::left
3. ios::right
2. Manipulators with arguments:
setw()
setprecision()
setfill()
setbase()

You might also like