--%>

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 Zip file Zip file : It is a file

    Zip file: It is a file employed to store compressed versions of the files. In connection with Java bytecode files, such have mostly been superseded by the Java Archive (abbreviated as JAR) files.

  • Q : Windows Communication Foundation or WCF

    Why did Microsoft develop the Windows Communication Foundation (WCF) technology?

  • Q : Explain Interpreter Interpreter : A

    Interpreter: A program that executes a translated version of the source program by implementing a virtual machine. The interpreters usually simulate the actions of an idealized Central Processing Unit. An interpreter for Java should implement the Java

  • Q : What is Reader class Reader class : It

    Reader class: It is sub-class of the Reader abstract, stated in the java.io package. Reader classes translate input from the host-dependent character set encoding into the Unicode.

  • Q : State the term multi threading State

    State the term multi threading.

  • Q : Explain Multiple inheritance Multiple

    Multiple inheritance: The capability of a class or interface to expand more than one interface or class. In Java, multiple inheritance is only accessible in the circumstances which are shown below: An interf

  • Q : Environment Modeling in Java Pathfinder

    Environment Modeling: In JPF, Java class files can be processed in two different ways: A) As ordinary Java classes managed and executed by the host JVM (e.g., standard Java library classes, JPF implementation class

  • Q : Explain Increment operator Increment

    Increment operator: The operator (++) which adds one to its operand. It consists of two forms: pre-increment (++x) and post-increment (x++). In its pre-increment form, the outcome of the expression is the value of its argument subsequent to the increm

  • Q : What are ORB-IOR-a servant-a POA manager

    In CORBA, what are i) an ORB ii) an IOR iii) a servant iv) a POA and v) a POA manager?

  • Q : Explain the way to back-up active

    Explain the way to back-up active directory.