1.

Write a note on manipulators.

Answer»

Manipulator functions: Manipulator functions are special stream functions that change certain characteristics of the input and output. They are useful in the formatting of input and output streams.

Following are a few standard manipulators normally used in the stream classes:

1. end1

2. setbase

3. setw

4. setfill

5. setprecision

1. end1:

The end1 is an output manipulator to generate a carriage return or line feed character. The end1 may be used several times in a C++ statement.

For example,

cout<< “ a “ << end1 << “b” << end1;

A program to display a message on two lines using the end1 manipulator and the corresponding output is given below.

2. setbase():

The setbase() manipulator is used to convert the base of one numeric value into another base. Dec, hex, and oct are the common base converters in C++.

3. setw(): 

The setw ( ) stands for the set width. The setw ( ) manipulator is used to specify the minimum number of character positions on the output field a variable will consume. The general format of the setw manipulator function is setw(int w).

4. setfill():

The setfill ( ) manipulator function is used to specify a different character to fill the unused field width of the value. The general syntax of the setfill () manipulator is setfill( char f) which changes the fill character to f. The default fill character is a space.

5. setprccision(): 

The setprecision () is used to control the number of digits of an output stream display of a floating-point value. The general syntax of the setprecision () manipulator is setprecision (int p) which sets the precision for floating-point insertions to p. The default precision is 6.



Discussion

No Comment Found