--%>

Input and output formatting in C++

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

 

1.      Ios class functions and flags

 

2.      User defined output functions

3.      Manipulators

The ion class contains a large number of member functions that would help us to format the output in a number of ways. The most important ones among us to format among them.

Function task

Width (): to state the needed field size for displaying an output value.

Precision value (): To state the number of digits to be shown after the decimal point of float value.

Fill ()   to specifying a character that is used to fill the unused portion of a field.

Self () to specify format flags that can control the form of output display (such as left justification and right justification)

Unset f () to clear the flags specified

  Manipulators are special functions that can be incorporated in the I/O statement to change the format parameters of a stream. table 2 shows some important manipulator functions that are frequently used. To access these manipulators, the file should be included in the programme. In addition to these functions supported by the C++ library, we can create our own manipulator functions to provide any special output formats.

Defining field width: Width () : We can use the width () function to define the width of a field necessary for the output item. As, it is a member function; we need to utilize an object to invoke it, as shown below-

Cout. Width (w): where w is the field (number of columns). The output will be printed in a field of w characters broad at the right end of the field. The width () function can specify the width for only one item (the item that follows immediately). After printing one item (as per the specifications) it will lapse back to the default. For examples, the statements

The value 543 is printed right justified in the first five columns. The specification width (5) does not keep the setting for printing the number 12. This can be improved as follow

Cout. Width (5);

Cout << 543;

 Cout. Width (5);

 Cout << 12 << "\n";

Remember that the field width should be specified for each item separately. C++ truncates the values and therefore, if the specified field width is smaller than the size of the values to be printed, C++ expends the field to fit the value.

Setting precision : precision (): by default the floating numbers are printed with sixth digit after the decimal point. How we can specify the number of digits to be displayed after the decimal point while the floating point numbers. This can be done by using the precisions () members a function follows:

Cout. Precision (d);

where d is number of digits at the right of the decimal point. For instance, the statements

Cout. Precision (3);

Cout << sqrt (2) << "\n";

Cout << 3.14159 << "\n";

Will produce the following output:

1.      141 (truncated)

2.      142 (rounded to the nearest cent)

3.      5 (no trailing zeros)

Not that, unlike the function width (), precision () retains the setting in effect until it is reset. That is why we have to declare only our statement for the precision setting which is used by all the three outputs.

   Related Questions in Programming Languages

  • Q : Maintaining audit trail of product

    The accuracy of product table data is crucial and the Brwebean’s. owner prefers to have an audit file which contains information regarding all DML activity on the BB_PRODUCT table. This information must point out the user id of the user running a DML statement,

  • Q : Common Language Infrastructure or CLI

    What is the Common Language Infrastructure (CLI)? What relation does .NET have with the CLI?

  • Q : Explain Sign extension Sign extension :

    Sign extension: Whenever an integer value from a type with a specific range is stored in a variable with a larger range, Java employs sign extension to determine the resultant value. The most important bit in the original value is employed to fill the

  • Q : Explain the applications of packing

    Explain the applications of packing life cycle.

  • Q : Protocol stack of XML Web Services

    Explain the protocol stack of XML Web Services.

  • Q : Explain the term QTP Environment

    Explain the term QTP Environment?

  • Q : Use of private Q. Explain the use of

    Q. Explain the use of private, public, protected access specifies.  

  • Q : Security features in the UNIX State the

    State the various security features within the UNIX?

  • Q : Define the term Blank final variable

    Blank final variable: A final variable which is not initialized as portion of its declaration. This variable should be initialized in either an instance initialization block or every of the constructors for its class before it is employed. A static bl

  • Q : Compute Days Between Ordering and

    An analyst in the quality assurance office reviews the time lapse between receiving an order and shipping an order. Any orders which have not been shipped in a day of the order being positioned are investigated. Build a function named ORD_SHIP_SF which computes the nu