4.7. Formatted I/O
Q23. Write about formatted data.
Answer:
Formatted Data
The data which has undergone formatting is referred as formatted data. Formatting is a way of representing data by performing necessary changes to the “setting” criterion depending on the user’s requirement. The various settings include,
(i) Changing the number format
(ii) Modifying the field width
(iii) Changing the decimal point.
Generally, formatting is done using manipulators together with I/O functions.
Program
#include <iostream.h>#include <conio.h>int main(){clrscr();inta = 13;cout <<“\n The value of a is ” <<a<<endl; // displays decimal number 13cout <<“\n The hexadecimal value of a is ” <<hex<<a; // displays hexadecimal number of 13 i.e., dcout <<endl;cout.width(14); //displays the number with a width of 14cout <<a;getch();return 0;}
Output
Formatted Input/Output
Formatted console I/O functions used in C++ for formatting the cae.
1. ios class functions and flags
2. Manipulators +
3. Custom/user-defined manipulators.
1. ios Class Functions and Flags
ios Class Function
The various ios class functions are as follows,
(i) width() :
(ii) precision()
(iii) fill()
(iy) self()
(i) ios::width()
A function width() is a number function that is used to set the width of a field in order to display the output value. The declaration of this function can be done in either of the following ways,
(a) int width();
This function on its invocation returns the present settings of the width.
(b) int width(int);
This functions on its invocation sets the width size as integer value (which is specified within the argument) and returns the previous settings of the width.
However, it should be assured that the width size for each and every item is specified separately. This is because, if in case the width size of a field is smaller than the size of the value that is to be printed, then C++ widens the field width to fit the value.
(ii) ios::precision()
The precision() function is also a number function that specifies the number of digits that are to be displayed after the decimal point where floating point numbers are to be printed. The declaration of this function can be done in either of the following ‘ways.
(a) int precision();This function on its invocation returns the present setting of floating point precision.(b) int precision(int);This function on its invocation sets the floating point and returns the previous setting of this precision.
(iii) ios::fill()
The function fill()-is used to fill the empty locations by other required characters. The declaration of this function can also be done in éither of the following ways,
(a) char fill();This function on its invocation returns the present settings of fill character.(b) char fill(char);This function on its invocation resets the fill character and finally returns the previous fill setting.
(iv) . ios::self()
It is a member function of ios char which sets the formatting flag when invoked.
The declaration of this function can be done in either of the following ways,
(a) DataType self (argl, arg2);This function removes the bits marked in var as defined by the data number x and then resets the bits marked in var 'x'(b) DataType self (datatype)This function on its invocation sets the flag in accordance to the bits marked in the parameterized ‘data type.
Program
#include<iostream.h>#include<conio.h>int main(){clrscr();cout.width(10);int a = cout.width(5);cout<<a;return 0;}
Output
This program has two width function calls. The first width( ) function call sets the column width at position 10. The next width( ) function call sets the column width at position 5 and returns the first column position i.e., 10. This value is taken by “a”. Finally, cout function displays 10 at column position 5.
Flags with Bitfields
The various bit-fields along with their format flags are,
(i) ios::adjustfield(ii) ios::floatfield(iii) ios::basefield.
(i) ios::adjustfield
This bitfield is a data member associated with the setf() function. It specifies the action of the value required by the output.
The action of the output value in bit-field ios::adjustfied as follows,
ios::left (Left justified output)
ios::right (Right justified output)
ios::internal (Padding after sign and base)
The declaration of ios::adjustfield can be done in the following manner,
static const long adjustfield;
(ii) ios::floatfield
This bit-field is another data member associated with a setf() function. It sets the floating point notation to scientific notation or fixed point notation.
The different ios::float field along with its flag format are as follows,
ios::scientific (scientific notation)
ios::fixed (fixed point notation)
The declaration of ios::floatfield can be done in the following manner,
static const long floatfield;
(iii) ios::basefield
This field is also a data member associated with setf() function. It sets the notations to decimal base, octal base and hexadecimal base.
The different ios::basefield along with their flag format are as follows,
ios::dec (Decimal base)
ios::oct (Octal base)
ios::hex (hexadecimal base).
The declaration of ios::basefield can be done in the following manner,
static const long basefield;
Example
#include<iostream.h>#include<conio.h>void main(){int number;clrscr( );cout<<“Enter a number:”;cin>>number;cout<<“The representation of integer in the form of decimal, octal and hexadecimal is: ”;cout.setf(ios::dec, ios::basefield);cout<<number<<“ ,”;cout.setf(ios::oct, ios::basefield);cout<<number<<“ and ”;cout.setf(ios::hex, ios::basefield);cout<<number}getch();}
Output