Free Training


C Language  |  CSS  |  MainFrame  |  VBScript  |  PHP  |  XML  |  C++ Tutorials  |  Ajax  |  JavaScript  |  CSS3  |  UML  |  jQuery  |  Microsoft AJAX

C++ Tutorials

 
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.


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



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

Post Your Comment:

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

Weekly Offers

Sponsored Links