The unformatted I/O statements that it is impossible to display output in a required user format or input the values in the desired form. In certain 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 overcome the problems of unformatted I/O operations in C++, the concept of manipulators was introduced.
The manipulators in C++ are special functions that can be used with insertion (<<) and extraction (>>) operators to manipulate or format the data in the desired way. Certain manipulators are used with << operator to display the output in a particular format, whereas certain manipulators are used with >> operator to input the data in the desired form. The manipulators are used to set field widths, set precision, inserting a new line, skipping white space etc. In a single I/O statement, we can have more than one manipulator, which can be chained as shown
cout<<manipl<<var1<<manip2<<var2; cout<<manipl<<manip2<<var1;
To use most of the manipulators, we need to include the header file iomanip.h in the program.
Manipulator | Purpose | Header File |
endl | causes line feed to be inserted i.e. ‘\n’ | iostream.h |
dec,oct,hex | set the desired number system | iostream.h |
setbase (b) | output integers in base b | iomanip.h |
setw(w) | read or write values to w characters | iomanip.h |
setfill (c) | fills the whitespace with character c | iomanip.h |
setprecision(n) | set floating point precision to n | iomanip.h |
Now we shall discuss the above manipulators in detail.
Note: One should note that the manipulators endl, setw(), setfill() can be used with all types of data, whereas manipulators setbase(), Dec, Oct, hex are used with integer data types. Also, setprecision() manipulators used only with the float data type.
Another thing to note is that while using the manipulators with arguments (like setw() etc.). You need to include iomanip.h header file, whereas while using manipulators without arguments(oct, hex, endl, etc.), you need to include iostream.h header file.
We’ll be covering the following topics in this tutorial:
endl Manipulator
The endl manipulator stands for endline and is used with an 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.’
#include<iostraam.h> #include<conio.h> int main() { cout<<"Entar name"<<endl; cout<<"Myname is Thakur"; qetch(); return 0; } Output Enter name Myname is Thakur
Dec, Oct,Hex Manipulator
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 a decimal using the manipulator’s oct, hex or dec, respectively. These manipulators are preceded by the appropriate variables to be used with.
#include<iostream.h> #include<conio.h> int: main() { int i; cout<<"Enter hexadecimal number ="; cin>>hex>>i; cout:<<"Hexadecimal value = "<<hex<<i<<endl; cout<<"Octal Value = "<<oct<<i<<endl; cout<<"Dcimal Value = "<<dec<<i<<endl; getch(); return 0; } Output : Enter hexadecimal = f Hexadecimal = f Octal value = 17 Decimal value = 15
In the above program, the variable i is inputted in hexadecimal form using hex manipulator in cin statement. To display the number in different notations like hexadecimal, octal and decimal, the manipulator’s hex, oct and dee are used.
The base of a number remains the same until changed explicitly. For example, suppose the base of a number i of integer type is changed to hexadecimal (hex) during output. In that case, it will display the output only in hexadecimal form until being specified explicitly using dee 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. It is a function that takes a 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.
#include<iostream.h> #include<iomanip.h> int main() { int num; cout<<"Enter number in Octal form = "; cin>>setbase(8)>>num; cout<<"Value of number in decimal form = "<<setbase(10)<<num<<endl; cout<<"Value of number in octal form = "<<setbase(8)<<num<<endl; cout<<"Value of number in hexadecimal form = "<<setbase(l6)<<num; return 0; } Output Enter numberin Octal form = 21 Value of number in decimal fonn = 17 Value of number in octal fonn = 21 Value of number in hexadecimal fonn = 11
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 systems by using setbase(b) manipulator with arguments values 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 so that it becomes more understandable. It can be used to format only one value at a time.
#include<iostream.h> #include<iomanip.h> #include<conio.h> int main() { int age = 22,rollno = 910l; cout<<setw(l2)<<"My Rollno is"<<setw(8)<<rollno<<endl; cout<<setw(l2)<<"My Aqe is"<<setw(8)<<age; getch(); return 0; }
In the above program, the setw() manipulator causes the number or string that follows it in the cout statement to be displayed within the specified width. The value to be displayed is right-justified. The values of variable rollno and age are displayed within 8 spaces specified using setw(S) manipulator. The value is padded with leading blank spaces as it doesn’t 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. For example
cout<<setw(2)<<"Roll is"<<setw(2)<<rollno;
the output is
setfill(c) Manipulator
The setfill() manipulator is used in conjunction with the 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 a 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 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 a 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.
#inclucle<iostream.h> #include<iomanip.h> #include<conio.h> int main() { int age = 22,rollno = 910l; cout<<setfill('#'); cout<<setw(4)<<age<<setw(6)<<rollno<<endl; cout<<setw(6)<<age<<setw(8)<<rollno; getch(); return 0; } Output : ##22##9101 ####22####9101
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 the floating-point number displayed is 6. This precision can be modified by using a setprecision () manipulator. This function takes an integer argument n that specifies the number of digits displayed after the decimal point. The floating-point number will be rounded to the specified precision.
#include<iostream.h> #includi<iomanip.h> #inelude<eonio.h> int main() { float a = 129.455396; cout<<setprecision(2)<<a<<endl; cout<<setprecision(3)<<a; getch(); return 0; } Output : 129.46 129.455
In the above program, the setprecision (2) rounds the number a to 2 decimal places after the decimal point, i.e. 129.46. Similarly, for others.