Vous êtes sur la page 1sur 13

Manipulator Functions

 The manipulator functions format output, so that it is


presented to user in more readable fashion.
 Some of the output manipulators are following.

Manipulator use
Endl New line
Dec Format output as decimal.
Oct Format output as octal.
hex Format output as hexadecimal.
Fixed Sets floating point decimals.
Showpoint Shows decimal in floating point values.
Setw() Set width of output field.
Setprecision() Specifies number of decimals for floating point.
Setfill() Specifies fill character.
Manipulator Functions Cont…
 The manipulators are found in two different libraries.
 Those without parameter arguments are found in the
basic input/output stream library <iostream>.
o E.g. endl, dec, oct, hex, fixed, showpoint.
 Those with parameter arguments are found in the
special library known as the input/output manipulator
library <iomanip>.
o E.g. setw(..), setprecision(..), setfill(..).
General Manipulators
 There are three general manipulators
 Newline
 Set width
 Set fill

 They known as general manipulators because they can


be used with all type of data.
New Line
 The newline manipulator terminates the current line
and starts a new one.
 It has the same effect as the newline(‘\n’) character.

#include<iostream.h>
#include<conio.h>
using namespace std;
void main()
{
cout<<"Hello world"<<endl;
getch();
}
Set Width
 Set width allows us to set the minimum width for an output
field.
 Set width manipulator also controls the alignment of the output.
 Right justification.
 Left justification.

Right Justification
places the data in an output area with the data orientation to the
right and fill characters (spaces) on the left.

Left Justification
starts the data on the left of an output area and adds trailing fill
characters on the right.
Set Width cont..
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
using namespace std;
void main()
{
int d123=123;
float f123=1.23;
char chA='A';

cout<<"Set width demonstration"<<endl<<endl;

cout<<d123<<f123<<chA<<"\t |no width"<<endl<<endl;

cout<<setw(5)<<d123
<<setw(5)<<f123
<<setw(5)<<chA
<<"\t |width 5 space each"<<endl<<endl;

cout.setf(ios::left);// for left justification


cout<<setw(10)<<d123
<<setw(10)<<f123
<<setw(10)<<chA
<<" |width 10 space each"<<endl;

getch();
}
Set Fill
 When the width of a print area is larger than the data values to be placed in it,
fill characters are used to fill non-data area.

 #include<iostream.h>
 #include<conio.h>
 #include<iomanip.h>
 using namespace std;
 void main()
 {
 int amount= 1234;
 cout<<"Demonstrate Fill Characters"<<endl;
 cout<<setw(10)<<amount
 <<"\tAmount with space fill"<<endl;

 cout<<setw(10)<<setfill('*')<<amount
 <<"\tAmount with check protection fill"<<endl;

 cout.setf(ios::left);
 cout<<setw(10)<<setfill('*')<<amount
 <<"\tAmount with check protection fill (left justified)“
 <<endl;

 getch();
 }
Integer Manipulators
 Integer manipulators are used to change the display
format for the integer values.
 Default integer manipulator is decimal manipulator
(dec)
 Other integer manipulators are
 Octal (oct) tells the cout to print the values in octal
numbering system.
 Hexadecimal (hex) tells to print in hexadecimal format.
Integer Manipulator cont..
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
using namespace std;
void main()
{
int d123=123;
cout<<"Demonstrate Integer Manipulators"<<endl;

cout<<"Value in decimal:\t";
cout<<setw(5)<<d123<<endl;

cout<<"Value in Hexadecimal:\t";
cout<<hex;
cout<<setw(5)<<d123<<endl;

cout<<"Value in Octal:\t\t";
cout<<oct;
cout<<setw(5)<<d123<<endl;

cout<<"Value in Decimal:\t";
cout<<dec;
cout<<setw(5)<<d123<<endl;

getch();
}
Floating Point Manipulator
 There are three floating point manipulators.
 Fixed
 Set precision
 Showpoint

 Generally if you use one, you will want to use all three.
Floating Point Manipulator cont..
 Fixed: tells the cout that floating point numbers are to
be displayed with fixed-point rather than floating-
point numbers.
 Set Precision: is used to control the number of
decimal places to be displayed.
 Show Point: used to display value with a decimal
point. Mostly used when we set zero precision.
Floating Point Manipulator cont..
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
using namespace std;
void main()
{
cout<<"Demonstrate Float Manipulation"<<endl;

float f1=12.4;

cout<<f1<<"\t\t\tWith no manipulation"<<endl;

cout<<fixed;
cout<<f1<<"\t\twith fixed added"<<endl;

cout<<setprecision(2);
cout<<f1<<"\t\t\twith set precision added"<<endl;

cout<<setprecision(0);
cout<<f1<<showpoint<<"\t\t\twith showpoint"<<endl;
getch();
}

Vous aimerez peut-être aussi