C++ Manipulators - endl, setw, setprecision, setf

Formatting output using manipulators

Formatted output is very important in development field for easily read and understand.

C++ offers the several input/output manipulators for formatting, commonly used manipulators are given below..

ManipulatorDeclaration in
endl iostream.h
setw iomanip.h
setprecision iomanip.h
setf iomanip.h

endl

endl manipulator is used to Terminate a line and flushes the buffer.

Difference b/w '\n' and endl

When writing output in C++,you can use either std::endl or '\n' to produce a newline, but each has a different effect.

  • std::endl sends a newline character '\n' and flushes the output buffer.
  • '\n' sends the newline character, but does not flush the output buffer.

The distinction is very important if you're writing debugging messages that you really need to see immediately, you should always use std::endl rather than '\n' to force the flush to take place immediately.

The following is an example of how to use both versions, although you cannot see the flushing occuring in this example.

#include <iostream.h>
int main()
{
	cout<<"USING '\\n' ...\n";
	cout<<"Line 1 \nLine 2 \nLine 3 \n";
	cout<<"USING end ..."<< endl;
	cout<< "Line 1" << endl << "Line 2" << endl << "Line 3" << endl;
	return 0;
}

Output

USING '\n' ...
Line 1
Line 2
Line 3
USING end ...
Line 1
Line 2
Line 3



setw() and setfill() manipulators

setw manipulator sets the width of the filed assigned for the output.

The field width determines the minimum number of characters to be written in some output representations. If the standard width of the representation is shorter than the field width, the representation is padded with fill characters (using setfill).

setfill character is used in output insertion operations to fill spaces when results have to be padded to the field width.

Syntax

    setw([number_of_characters]);
    setfill([character]);

Consider the example

#include <iostream.h>
    #include <iomanip.h>
    int main()
    {
	    cout<<"USING setw() ..............\n";
	    cout<< setw(10) <<11<<"\n";
	    cout<< setw(10) <<2222<<"\n";
	    cout<< setw(10) <<33333<<"\n";
	    cout<< setw(10) <<4<<"\n";

	    cout<<"USING setw() & setfill() [type- I]...\n";
	    cout<< setfill('0');
	    cout<< setw(10) <<11<<"\n";
	    cout<< setw(10) <<2222<<"\n";
	    cout<< setw(10) <<33333<<"\n";
	    cout<< setw(10) <<4<<"\n";

	    cout<<"USING setw() & setfill() [type-II]...\n";
	    cout<< setfill('-')<< setw(10) <<11<<"\n";
	    cout<< setfill('*')<< setw(10) <<2222<<"\n";
	    cout<< setfill('@')<< setw(10) <<33333<<"\n";
	    cout<< setfill('#')<< setw(10) <<4<<"\n";
	    return 0;
    }

Output

    USING setw() ..............
            11
          2222
         33333
             4
    USING setw() & setfill() [type- I]...
    0000000011
    0000002222
    0000033333
    0000000004
    USING setw() & setfill() [type-II]...
    --------11
    ******2222
    @@@@@33333
    #########4

setf() and setprecision() manipulator

setprecision manipulator sets the total number of digits to be displayed, when floating point numbers are printed.

Syntax

    setprecision([number_of_digits]);
    cout<<setprecision(5)<<1234.537;
    // output will be : 1234.5

On the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision.

In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The number of digits before the decimal point does not matter in this case.

Syntax

    setf([flag_value],[field bitmask]);
field bitmaskflag values
adjustfield left, right or internal
basefield dec, oct or hex
floatfield scientific or fixed

Consider the example

#include <iostream.h>
#include <iomanip.h>
int main()
{
	cout<<"USING fixed .......................\n";
	cout.setf(ios::floatfield,ios::fixed);
	cout<< setprecision(5)<<1234.537<< endl;

	cout<<"USING scientific ..................\n";
	cout.setf(ios::floatfield,ios::scientific);
	cout<< setprecision(5)<<1234.537<< endl;
	return 0;
}

Output

    USING fixed .......................
    1234.53700
    USING scientific ..................
    1234.5

Consider the example to illustrate base fields

#include <iostream.h>
#include <iomanip.h>
int main()
{
	int num=10;

	cout<<"Decimal value is :"<< num << endl;

	cout.setf(ios::basefield,ios::oct);
	cout<<"Octal value is :"<< num << endl;

	cout.setf(ios::basefield,ios::hex);
	cout<<"Hex value is :"<< num << endl;
	return 0;
}




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.