Exforsys.com
 
Home Tutorials C++
 

C++ Manipulators

 

C++ Manipulators

In this C++ tutorial, you will learn what a manipulator is, endl manipulator, setw manipulator, setfill manipulator and setprecision manipulator explained along with syntax and examples.

<

 

Sponsored Links

 

/h4>

What is a Manipulator?

Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer’s choice of display.


There are numerous manipulators available in C++.  Some of the more commonly used manipulators are provided here below:


endl Manipulator:

This manipulator has the same functionality as the ‘\n’ newline character.


For example:



cout << "Exforsys" << endl;
cout << "Training";



produces the output:


Exforsys
Training


setw Manipulator:

This manipulator sets the minimum field width on output. The syntax is:


setw(x)


Here setw causes the number or string that follows it to be printed within a field of x characters wide and x is the argument set in setw manipulator. The header file that must be included while using setw manipulator is <iomanip.h>



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


void main( )
{
int x1=12345,x2= 23456, x3=7892;
cout << setw(8) << ”Exforsys” << setw(20) << ”Values” << endl
<< setw(8) << “E1234567” << setw(20)<< x1 << end
<< setw(8) << “S1234567” << setw(20)<< x2 << end
<< setw(8) << “A1234567” << setw(20)<< x3 << end;
}



The output of the above example is:

setw(8)   setw(20)
Exforsys Values
E1234567 12345
S1234567 23456
A1234567 7892


setfill Manipulator:

This is used after setw manipulator. If a value does not entirely fill a field, then the character specified in the setfill argument of the manipulator is used for filling the fields.



#include <iostream.h>
#include <iomanip.h>
void main( )
{
cout << setw(10) << setfill('$') << 50 << 33 << endl;
}



The output of the above program is


$$$$$$$$5033


This is because the setw sets 10 width for the field and the number 50 has only 2 positions in it. So the remaining 8 positions are filled with $ symbol which is specified in the setfill argument.


setprecision Manipulator:

The setprecision Manipulator is used with floating point numbers. It is used to set the number of digits printed to the right of the decimal point. This may be used in two forms:


  • fixed
  • scientific

These two forms are used when the keywords fixed or scientific are appropriately used before the setprecision manipulator. The keyword fixed before the setprecision manipulator prints the floating point number in fixed notation. The keyword scientific before the setprecision manipulator prints the floating point number in scientific notation.



#include <iostream.h>
#include <iomanip.h>
void main( )
{
float x = 0.1;
cout << fixed << setprecision(3) << x << endl;
cout << sceintific << x << endl;
}



The above gives ouput as:


0.100
1.000000e-001


 

Sponsored Links

 

/p>

The first cout statement contains fixed notation and the setprecision contains argument 3. This means that three digits after the decimal point and in fixed notation will output the first cout statement as 0.100. The second cout produces the output in scientific notation. The default value is used since no setprecision value is provided.



Read Next: C++ Decision Making Statements



 
Related Topics


 

Comments


smit said:

  how to write a star programme ...in all manne i mean reverse star left hand side star and ..all position of star ...eg... *
* *
* * *
* * * *
* * * * *

pls help ...it is used through manipulators
April 19, 2007, 11:02 pm

HUFFLEPUFF said:

  use loops...

#include
using namespace std;
int main()
{
int x,y;
for(x=1;x
January 11, 2008, 8:53 pm

innocent said:

  how to code using manipulators in c++ program
November 11, 2008, 2:21 am

Shubhra Jain said:

  Why we use namespace std in beginning of C++ programme?
March 13, 2009, 3:42 am

Krishan Singh said:

  Why do we use namespace std in beginning of C++ programme?
April 3, 2009, 7:20 am

Programmer 101 said:

  because if you don't use namespace std, you will have to use

example. std::cout<<"Example";

with the namespace std there is no need for std::.
April 13, 2009, 4:40 pm

ShahzadAshrafi said:

  #include<iostream.h>
#include<iomanip.h>
int main()
{
for (int a = 0, b = 1; a < 10; a++)
{
cout << endl;
for (;;)
{
cout << setfill('*') << setw(b++) << '*';
if (a < 10)
break;
}
}
cout << endl;
return 0;
}
May 2, 2009, 1:14 am

tayyab said:

  #include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
for (a=2;a<=5;a++)
{
for(b=1;b<=a;b++)
cout<<"x";
cout<<endl;
}
}
February 16, 2010, 5:45 am

smriti said:

  void main()
{
clrscr();
int i, n=5;;
for (i=1; i<n; i++)
{
cout<<setw(i+1)<<setfill('*');
cout<<"n";
}

getch();
}
March 8, 2010, 4:48 am

Amit said:

  #include<iostream.h>
#include<iomanip.h>
int main()
{
for (int a = 0, b = 1; a < 10; a++)
{
cout << endl;
for (;;)
{
cout << setfill('*') << setw(b++) << '*';
if (a < 10)
break;
}
}
cout << endl;
return 0;
}
May 19, 2010, 1:45 pm

S.A said:

  what is basic code for start programme in C++
June 15, 2010, 11:36 am

chetan said:

  How we can make
+50***********
***********+50
+***********50
June 22, 2010, 10:59 am

happy said:

  How we can use manipulators?
plz tell me
August 8, 2010, 8:09 pm

ananya said:

  What are the types of manipulators?
August 15, 2010, 4:08 am

hajra said:

  Please tell :-
why we use "setfill" &"setw" in our program???
September 1, 2010, 2:42 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

 
 


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape