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 : Explain the applications of packing

    Explain the applications of packing life cycle.

  • Q : Role of ScriptManagerProxy control?

    Explain the role of ScriptManagerProxy control?

  • Q : What is Round robin allocation Round

    Round robin allocation: It is an allocation of time slices which repeatedly cycles regarding a set of eligible threads in the fixed order.

  • Q : Explain Cast Cast : Where Java does not

    Cast: Where Java does not allow the utilization of a source value of one type, it is essential to use a cast to force the compiler to admit the use for the target type. The care must be taken with casting values of primitive types, as this frequently

  • 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 : Explain Structured programming

    Structured programming: It is a style of programming generally related with languages like FORTRAN, C, Pascal and so forth. Employing structured programming methods, a problem is frequently resolved employing a divide and conquer approach like stepwis

  • Q : Define Bogor Bogor (Robby, Dwyer, and

    Bogor (Robby, Dwyer, and Hatcliff 2006) is an extensible software model-checking framework which includes: Software model checking algorithms Visualizations A user interface designed to

  • Q : BAT files and shell scripts Using the

    Using the Web as a resource for your investigation, find out and explain what BAT files and shell scripts contain, and what they are used for. In the light of your findings, discuss why an operating system would provide both a graphical user interface

  • Q : What is Pop-up menu Pop-up menu : A

    Pop-up menu: A menu of actions which is generally not visible on the screen till a mouse button is clicked. The Popup menus assist to keep a user interface from becoming cluttered.

  • Q : Define the term Boolean Boolean : It is

    Boolean: It is one of the Java's primitive types. The Boolean type has merely two values: true and false.

©TutorsGlobe All rights reserved 2022-2023.