Explain Manipulator functions With Example in C++
Manipulator functions:
Manipulators are special functions that are specifically designed to modify the working of a stream. They can be embedded in the i/o statements to modify the form parameters of a stream. there can be more than one manipulator in a statement and they can be chained. The main advantage of using manipulator function is that they facilitate the formatting of input and output stream. Manipulators are categorized into two types.
Non-parameterized manipulators
Parameterized manipulators
Non-parameterized manipulators are
dec, hex, oct, ws, endl,ends,flush
Parameterized manipulators:
setw(int width), setprecision(int prec), setfill(int fchar), setbase (int base),
setiosflags(long flags), resetiosflags(long flags).
The following are the list of standard manipulators. To carry out the operations of these manipulator functions in a user program, the header file input and output manipulator <iomanip.h> must be included.
endl: the endl is an out put manipulator to generate carriage return or line feed character. The endl may be used several number of times in a c++ statement
For example: 1. cout << “a” << endl;
A program to display a message on tow lines using the endl manipulator and the corresponding output is given below
#include <iostream.h>
void main()
{ cout << “this is the test “ <<endl;
cout << “program “ ; }
output this is the test
program
The endl is same as the non-graphic character to generate line feed (\n)
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)
In addition to the conversion facilities such as bases like dec, hex, oct, the setbase manipulator is also used to define the base of a number as a numeric value. The prototype setbase() manipulator is defined in the iomanip.h header file and it should be included in user program. The hex, dec, oct manipulators change the base of inserted or extracted integer values. The original default manipulator for stream input and output is dec.
Example
The program to show the base of a numeric value of a variable using setbase manipulator function
#include <iosteam.h>
#include <iomanip.h>
void main()
{ int value;
cout << “enter the values “<<endl;
cin >> value;
cout <<”decimal base = “ << setbase(10)<< value <<endl;
cout << “hexadecimal base =” <<setbase(16) <<value <<endl;
cout << “octal base “ <<setbase(8) <<value <<endl;
}
setw()
The setw() stands for the setwidth. The setw manipulator is used to specify the minimum number of character positions consumed on the output field of a variable.
The general format of the setw manipulator function is setw(int w)
This changes the field width to w, only for the next insertion. The default field width is 0
For example:
cout << setw() <<a <<endl
cout << setw(10) <<a <<endl;
setfill()
The setfill() manipulator is used to specify a different character to fill the unused field width of the value.
The general syntax of the setfill(n) manipulator is setfill(char f)
Which changes the fill character to f. the default fill character is a space.
For example setfill(‘*’) // fill a asterisk(*) character.
setprecision(int prec) : sets the precision used for floating point output. The number of digits to be shown after the decimal point is given by the integer prec.
For example:
#include <iostream.h>
#include <iomanip.h>
void main()
{ float f = 123.23456;
cout << setprecision(3) << f <<endl;
}
out put : 123.234
Labels: C++