You are on page 1of 7

Session Objectives

Discuss Manipulators
Discuss setfill() manipulator Discuss setw() manipulator Discuss setprecision() manipulator

Manipulators
Manipulator function are special stream functions that change

certain characteristics of input and output.


All the manipulator functions prototypes are defined in the

header file <iomanip.h.>


Some Examples are :
endl (new line character) setbase(covert the base value in to another bases value) setw( set the width of the output) setfill(fills the given character in the unused field) setprecision(displays the number of digits after the decimal point in the floating numbers.

setfill(char):it can be used to fills the given character in the unused field
#include<iostream.h> #include<iomanip.h> #include<conio.h> void main() { int a=10,b=20; clrscr(); cout<<setfill('*'); cout<<setw(5)<<a<<setw(5)<<b<<endl; cout<<setfill('$'); cout<<setw(7)<<a<<setw(7)<<b<<endl; getche(); }

setw(): it can be used to set the width of the output field


#include<iostream.h> #include<iomanip.h> #include<conio.h> void main() { int a=10,b=20; clrscr(); cout<<setw(5)<<a<<setw(5)<<b<<endl; cout<<setw(15)<<a<<setw(15)<<b<<endl; getche(); }

setprecision():This manipulator control display the number of digits after the decimal point for the floating numbers
#include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { int a=10,b=3; clrscr(); float c=(float)a/b; //Casting Type cout<<setprecision(1)<<c<<endl; cout<<setprecision(2)<<c<<endl; cout<<setprecision(3)<<c<<endl; getche(); }

These are special functions that are used with cout statements for getting formatted outputs The ios base class contains member functions like width(),precision(),file(),setf() and unsetf() to display formatted output

You might also like