Sunday 14 July 2013

Manipulators

Manipulators are operators that are used to format the data display. The most commonly used manipulators are endl and setw.

The endl manipulator, when used in the output statement, causes a linefeed to be inserted. It has the same effect as using the new line character "\n". 

For Example,, The statement

cout<<"m= "<<m<<endl;

cout<<"d= "<<d<<endl;

 would cause 2 lines of output, one for each variable. If we assumes the values of the variables as 20 and 2272 respectively, the output will appear as follows:

m=20

d=2272

It is important to note that this form is not the ideal output. It should rather appear as under:

m=   20

d=2272

Here, the numbers are right justified. This form of output is possible only if we can specify a common field width for all the members and force them to be printed right justified. The setw Manipulator does this job. It is used as follows

cout<<setw(5)<<sum<<endl;

The manipulator 5 specifies a field width 5 for printing the value of the variable sum. This value is right justified within the field shown below:

      345


No comments:

Post a Comment