Loading....
Coupon Accepted Successfully!

 

Formatted Console I/O Operations

C++ provides various formatted console I/O functions for formatting the output. They are of three types.

  1. Ios class function and flags
  2.  Manipulators
  3. User-defined output functions

The ios grants operations common to both input and output. The classes (istream, ostream, and iostream) derived from ios are special I/O with high-level formatting operations: The iostream class is automatically loaded in the program by the compiler. Figure  describes the hierarchy of stream classes.

  1. istream performs formatted input.
  2. ostream performs formatted output.
  3. iostream performs formatted input and output.

streambuf allows an abstraction for connecting to a physical device. Classes derived from it work with files, memory, etc. The ios communicates to a streambuf. It keeps information on the state of the streambuf (good, bad, eof, etc.) and saves flags for use by istream and ostream.

  • The streambuf class controls the buffer and its related member functions. It allows the ability to fill, flush, and empty the buffer.
  • The streambuf is an object of ios class. The base class of input and output stream classes is ios class.
  • The istream and ostream classes are derived classes of ios class and control input and output streams.
  • The iostream class is a derived class of istream and ostream. It provides input and output functions to control console operations.

Table: describes the functions of ios class in brief.

Table: ios Class Function

Function

Working

width()

To set the required field width. The output will be displayed in given width.

precision()

To set number of decimal point to a float value.

fill()

To set a character to fill in the blank space of a field.

setf()

To set various flags for formatting output.

unsetf()

To remove the flag setting.

  1. ios::width (member functions)
     
    The width() function can be declared in two ways.
     
    (a) int width();
     
    (b) int width (int);
     
    int width();
     
    If this function is declared as given above, it returns the present width setting.

    int width (int);
     
    If this function is declared as given above, it sets the width as per the given integer and returns the previous width setting. The setting should be reset for each input or output value if a width other than the default is desired.

3.30 Write a program to set column width and display the characters at specified position.

Explanation: The first cout. width() statement sets the width 5. The cout statement sets the column width position at 5 and the cout statement displays the character “A” at column 5. Similarly, the column width is set 15 and the character ‘B’ is displayed at column 15.

3.31 Write a program to use both the formats of width() function and display the result.

Explanation: In the above program, the width() function call sets the width at column 50. The second width() function call sets the width at column 1 and returns the previous setting, that is 50. The integer x collects this value. The cout() statement displays the number 50 at column 1.

  1. ios::precision
     
    This function can be declared in two ways.
     
    (a) int precision (int);
     
    (b) int precision();
     
    int precision (int);
     
    If the function is declared as given above, it sets the floating-point precision and returns the previous setting. The precision should be reset for every value being output if we want a precision result other than the default.

    int precision();
     
    If the function is used as given above, it returns the current setting of floating-point precision.

3.32 Write a program to set precision to two and display the float number

Explanation: In the above program, the cout.precision() statement sets float point precision to 2. The cout statement displays 3.14 instead of 3.1452.


3.33 Write a program to set number of precision points. Display the results of 22/7 in different precision settings.

Explanation: In the above program, the for loop executes from 10 to 1 in reverse order. Each time the equation 22/7 is calculated and the result is stored in the variable pi. The precision is set according to the value of variable x. The output of the program is as given above.

  1. ios::fill
     
    This function can be declared in two ways.
     
    (a) char fill();
     
    (b) char fill (char);
     
    char fill();
     
    If the function is used as given above, it returns the current setting of fill character.

    char fill (char);
     
    If the function is used as given above, it resets the fill character and returns the previous setting.

3.34 Write a program to fill empty spaces in a line with a specific symbol (*).

Explanation: In the above program, the statement cout.fill (‘*’); fills the blank spaces with * symbols. The cout. width() sets the width to 10 and the character ‘H’ is displayed at column 10. The column 1 to 9 remains blank. The symbol ‘*’ is displayed instead of blank spaces. This effect is due to cout.fill() statement as shown below.

 

*

*

*

*

*

*

*

*

*

H

 


3.35 Write a program to fill blank spaces with different symbols.

Explanation: This program is similar to the last one. Here, two different characters are used to fill the blank spaces in the specified width. The width is specified using cout.width() statement. The output can be observed as follows.

 

 

Tip: The character ‘\’ (back slash) is not allowed in cout.fill() statement to fill the blanks. This is because it is used with escape sequences.

 

 

3.36 Write a program to show the effect cout.fill().

Explanation: The first line displays ABC following blank spaces because the fill(); is not used. In the second statement, the symbol ‘#’ is displayed because the fill character is set to ‘#’.


 

 

3.37 Write a program to fill the unused filled area with ‘*’

Explanation: In the above program, the precision setting and display of floating point number using precision is the same as in the previous example. In the for loop, each time width() is set to the value of variable j. The value of j increases at each iteration. The floating precision number decreases from top to bottom, and blank spaces remain in the specified field area by the statement width(). The unused area is filled with ‘*’. The setting of fill character is done before the for loop statement. The fill character may be set to any character. The fill() statement after for loop body displays the character used for filling.

  1. ios::setf
     
    This function can be declared in two ways.
     
    (a) long setf (long sb, long f);
     
    (b) long setf (long);
     
    long setf (long sb, long f);
     
    The bits according to those marked in variable f are removed in the data member x_flags, and then reset to be those marked in variable sb. Using the constants in the formatting flag enumeration of class ios can specify the value of variable sb.

    long setf (long);
     
    If the above declaration is used in the program, it sets the flags according to those marked in the given long. The flags are set in the data member x_flags of class ios. Using the constants in the formatting flags can specify the long enumeration of class ios. It returns the previous settings.




Test Your Skills Now!
Take a Quiz now
Reviewer Name