You cannot copy content of this page.

Use of manipulators in C++

Use of manipulators in C++




Use of manipulators in C++

Manipulators are stream functions available in iomanip.h header file and are used to change the default formats of input and output.  These are used with stream insertion and extraction operators.  They are used for defining a specified format of input and output.  They provide features similar to that of ios member functions.  Moreover, manipulators may be concatenated to result in a single statement.  Some of the standard manipulators available in iomanip.h header file are endl, setw, setfill, hex, oct, dec, setprecision, flush, setiosflags etc.  Manipulators are different from ios member functions as manipulator does not return the previous format state as is the case with ios member functions.

Consider the following example:

#include <iostream.h>
#include <iomanip.h>

void main()
{
float x = 5.45;
cout<<setw(10);
cout<<setprecision(4);
cout<<setfill(‘*’);
cout<<x<<endl;
}

OUTPUT:

******5.45

Consider another example:

#include <iostream.h>
#include <iomanip.h>

void main()
{
int n=52; //decimal number
cout<<endl<<“Decimal Number=”<<dec<<n;
cout<<endl<<“Hexadecimal Number=”<<hex<<n;
cout<<endl<<“Octal Number=”<<oct<<n;
}

Output:

Decimal Number=52
Hexadecimal Number=34
Octal Number=64

Comments are closed
x Close

Like Us On Facebook