MANIPULATORS in C++

MANIPULATORS in C++

The I / O statements that we have discussed so far are unformatted as it is not possible to display output in a required user format or inputting the values ​​in desired form.  In ceratin situations, we may need to format the I / O as per user requirements.  For example: 
1) The square root of a number should be displayed upto 2 decimal places.  
2) The number to be inputted must be in a hexadecimal form.  
To overcame the problems of unformatted I / O operations in C ++, the concept of manipulators was introduced.  The manipulators are special functions that can be used with insertion («<) and extraction >>) operators to manipulate or format the data in a desired way.  There are certain manipulators that are used with «operator to display the output in a particular format whereas certain manipulators are used with >> operator to input the data in a desired form.  The manipulators are used to set field widths, set precision, inserting new line, skipping white space etc.

There are various types of manipulators we have discussed.

endl MANIPULATOR: 

The endl manipulator stands for endline and is used with insertion operator (<) that moves the cursor to the next line.  If we do not use endl, the next output will be displayed in the same line.  The endl has the same function as that of '\ n'.  


dec, oct, hex MANIPULATORS: 

All the numbers are displayed and read in decimal notation by default.  However, you may change the base of an integer value to octal or hexadecimal or back to decimal by using the manipulators oct, hex or dec respectively.  These manipulators are preceded by the appropriate variables to be used with.


    In the above program, the value of variable i is inputted in hexadecimal form using hex manipulator in cin statement.  In order to display the number in different notations like hexadecimal, octal and decimal the manipulators hex, oct and dec are used respectively.  
     The base of a number remains the same until changed explicitly.  For example, if the base of a number i of integer type is changed to hexadecimal (hex) during outputting then it will display the output only in hexadecimal form until being specified explicitly using dec or oct manipulators.  

setbase (b) MANIPULATOR:

 The setbase () manipulator is used to change the base of a numeric value during inputting and outputting.  It is an alternative to dec, oct and hex manipulators discussed earlier.  It is a function that takes single integer argument (b) having values ​​8, 10 or 16 to set the base of the numeric value to octal, decimal and hexadecimal respectively.  The default base is 10.

        In the above program, the value of variable num is inputted in octal form using setbase (8) manipulator in the cin statement.  The value of the variable num is displayed in different number system by using setbase (b) manipulator with arguments value 10, 8 and 16.  

setw (w) MANIPULATOR:

 The setw () stands for set width.  It is a function that takes a single integer argument which specifies the amount of space used to display the required value.  We typically use the setw () manipulator for displaying output in such a way that it becomes more understandable.  It can be used to format only one value at a time.

          In the above program the setw () manipulator causes the number or string that follows it in the cout statement to be displayed within the width that we specify.  The value to be displayed is right justified.  The values ​​of variable rollno and age are displayed within 8 spaces specified using setw (8) manipulator.  The value is padded with leading blank spaces as it does not fill the whole width.  
             If the value is larger than the specified width, it will not truncate the value and the value is printed as it is.  

setfill (c) MANIPULATOR: 

The setfill () manipulator is used in conjunction with setw () manipulator.  As we have already discussed that using setw () manipulator, the compiler leaves the empty spaces on the left side of the required value if the set width is greater than the needed space.  If you wish to fill the blank space with an alternative character instead of blank space, you can use the setfill () manipulator.
       Generally, you will not want to change the fill character.  However, one common example of when you may want to is when creating a program that prints cheques.  To prevent the cheque amount from being altered by the user, computer - generated cheque amounts are usually printed with leading asterisks ().  This is done in C ++ with setfill) manipulator.
    The setfill () manipulator takes a single character as an argument and fills the empty spaces with the specified character c on the left of the value displayed if the width specified using setw () manipulator is greater than the value to be displayed.  



setprecision (n) MANIPULATOR:

 The setprecision () manipulator is used to control the precision of floating point numbers i.e.  the number of digits to the right of the decimal point.  By default, the precision of floating point number displayed is 6. This precision can be modified by using setprecision () manipulator.  This function takes an integer argument n that specifies the number of digits to be displayed after the decimal point.  The floating point number will be rounded to the specified precision. 
Setprecision(n) Manipulator



      In the above program, the setprecision (2) rounds the number a to 2 decimal places after the decimal point i.e.  129.46.  Alike for others.

No comments

Powered by Blogger.