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 : What is Reflection Reflection : It is

    Reflection: It is the ability to find out what fields, methods, constructors, and so forth, are stated for an object or class. Reflection is supported by the Class ‘class’, and other classes in the java.lang.reflect package. Reflection mak

  • Q : Use of setjmp and longjmp Use of

    Use of setjmp() and longjmp(): In C/C++, setjmp() saves the contents of the registers at a particular state in the program and longjmp() will restore that state later. In this way, longjmp() “returns” to the state of the program when setjm

  • Q : Requirement of Timer control in AJAX

    Explain the requirement of a Timer control in the AJAX.

  • Q : Explain Decrement operator Decrement

    Decrement operator: It is an operator (--) which adds one to its operand. This has two forms: pre-decrement (--x) and post-decrement (x--). In its pre-decrement form, the outcome of the expression is the value of its argument subsequent to the decreme

  • Q : Various controls of ASP.NET AJAX Name

    Name the various controls of ASP.NET AJAX?

  • Q : Define the term Stack trace Define the

    Define the term Stack trace: It is a display of the runtime stack.

  • Q : Explain the relationship between XHTML

    Explain the relationship between XHTML and the Web?

  • Q : Identify Customers in SQL The company

    The company wish to provide an incentive of free shipping to such customers who have not returned for 2 months. Build a procedure named PROMO_SHIP_SP which determines these customers are and then updates the BB_PROMOLIST table accordingly. The procedure employs the be

  • Q : Define the term Manifest file Define

    Define the term Manifest file: It is a file which is held in a Java Archive (JAR) file, explaining the contents of the archive.

  • Q : What is Abstract method Abstract method

    Abstract method: This is a method with the abstract reserved word in its header. The abstract method has no method body. Methods stated in an interface are for all time abstract. The body of an abstract method should be stated in a su

©TutorsGlobe All rights reserved 2022-2023.