***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Sunday, July 4, 2021

Formatted Console I/O Operations

C++ supports a number of features that could be used for formatting the output. These features include:

·         ios class function and flags.

·         manipulators.

·         User-defined output functions.

I. The ios class contains a large number of member functions that would help us to format the output in a number of ways. Some of the functions are:

    1. Width()

    The width() function is used to define the width of a field necessary for the output of an item. As it is a         member function object is required to invoke it like

    cout.width(w);

    here w is the field width.

    The output will be printed in a field of w character wide at the right end of field.The width() function can     specify the field width for only one item(the item that follows immediately).After printing one item(as per     the specification) it will revert back the default.

    Example:

    cout.width(5);

    cout<<543<<12<<”\n”;

    will produce the following output:


    2. Precision()

    By default, the floating numbers are printed with six digits after the decimal points. This function can     be used to specify the number of digits to be displayed after the decimal point while printing the             floating point numbers.

    cout.precision(d);

    where d is the number of digits to the right of decimal point.

    Example:

    cout.precision(3);

    cout<<sqrt(2)<<”\n”;

    1.141 will be printed

    Unlike the function width(),precision() retains the setting in effect until it is reset.

    3. Fill()

    The unused portion of field width are filled with white spaces, by default. The fill() function can be         used to fill the unused positions by any desired character.It is used in the following form:

    cout.fill(ch);

    Where ch represents the character which is used for filling the unused positions.Example:

    cout.fill(‘*’);

    cout.width(10);

    cout<<5250<<”\n”;

    The output would be:

    Like precision (),fill() stays in effect until it is changed.

    4. Setf()

    The setf() a member function of the ios class, can provide answers left justified.The setf() function can be     used as follows:

    cout.setf(arg1.arg2)

    The arg1 is one of the formatting flags defined in the class ios.The formatting flag specifies the format         action required for the output.Another ios constant,arg2,known as bit field specifies the group to which         the formatting flag belongs.

    Example:

    cout.fill(‘*’);

    cout.setf(ios::left,ios::adjustfield);

    cout.width(15);

    cout<<”TABLE 1”<<”\n”;

    This will produce the following output:


    5. Unsetf()

    To clear the flags specified

II. Manipulators are special functions that can be included in the I/O statements to alter the format parameter of stream. To access these manipulators, the file iomanip should be included in the program.

Manipulators and their ios equivalents are:


In addition to these standard library manipulators we can create our own manipulator functions to provide any special output formats.

Designing our own manipulators:-

The general form for creating a manipulator without any argument is

ostream & manipulator (ostream & output)

{

……………

…………….(code)

……………..

return output;

}

Example:

#include <iostream.h>

#include <iomanip.h>

ostream &currency (ostream & output)

{

output<< “Rs”;

return output;

}

void main()

{

cout<<currency <<7864.5;

}

output:

Rs7864.5



No comments:

Post a Comment