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 : Define Object serialization Object

    Object serialization: The writing of an object's contents in such a manner that its state can be restored, either at a later time, or in a different procedure. This can be employed to store objects between runs of a program, or to transfer or shifts o

  • Q : Explain Redundant value Redundant value

    Redundant value: The value of a data type which has no use or meaning in a particular context. For example, negative values would be redundant a class employing integer attributes to the model assignment marks. In several applications, redundant patte

  • Q : Concept of object oriented analysis

    Q. Explain the concept of object oriented analysis with explanation of all steps of analysis. 

    Q : Define the term Multiprogramming system

    Define the term Multiprogramming system: It is an operating system which is able to run multiple programs parallel.

  • Q : What is an Initializer Initializer : A

    Initializer: A block stated at the outermost level of a class - identical to a method devoid of a header. The initializer blocks are executed, in order, whenever an instance is formed. They are executed prior to the constructor of the defining class,

  • Q : Define Short-circuit operator

    Short-circuit operator: It is an operator in which only as a lot of operands are computed as are required to determine the final outcome of the operation. The logical-and (&&) and logical-or (||) operators are most general illustration, though

  • Q : Are you sure that XHTML element name

    Are you sure that XHTML element name case sensitive? Answer: XHTML element names surely are case sensitive. Every element names should be written within lower case l

  • Q : For creating a thread explain the limit

    For creating a thread explain the limit on per process.

  • Q : Explain Branch instruction Branch

    Branch instruction: It stores a new instruction address into the program counter. The consequence of this is the next instruction to be fetched will not generally be the one instantly following the branch instruction. Therefore the normal chronologica

©TutorsGlobe All rights reserved 2022-2023.