Programming in C++ (15UAMA61) Formatted Console I/O Opeations By A.Sujatha M.Sc.,M.Phil.,PGDCA ., Department of Mathematics
Formatted Console I/O Opeations Formatted console input/output functions are used for performing input/output operations at console and the resulting data is formatted and transformed. Some of the most important formatted console input/output functions are –
Functions Description width() Using this function, we can specify the width of a value to be displayed in the output at the console. precision() Using this function, we can specify the number of digits( num_of_digits ) to the right of decimal, to be printed in the output. fill() Using this function, we can fill the unused white spaces in a value( to be printed at the console ), with a character of our choice. setf() Using this function, we can set the flags , which allow us to display a value in a particular format. unsetf() Using this function, we can clear the flags specified.
Using the width() function Using this function, we can specify the width of a value to be displayed in the output at the console. Let's see the syntax of width() function - cout.width (w); Where, w is the value to be displayed in the output at the console. Example of width() function - //The width() function defines width of the next value to be displayed in the output at the console.
#include< iostream > using namespace std; int main() { char ch = 'a'; //Adjusting the width of the next value to displayed to 5 columns. cout.width (5); cout << ch <<"\n"; int i = 1; //width of the next value to be displayed in the output will not be adjusted to 5 columns. cout << i ; }
Filling and padding : Fill: The member function can be used to fill the unusual position of the field by any desired character.It can be invoked by object cout . Syntax: Cout.fill (‘ ch ’) Where ch represents the character which is used for filling the used position. Ex: Cout.fill (‘*’); Cout.width (‘/0’); Cout <<5250<< endl ; Output: x x x x x x 5 2 5
Setf ( ) It is a member function of ios class.It is used to set flags. It can be invoked by using the object cout . Syntax: Cout.setf (arg1,arg2); Where arg1 is one of the formatting flags define in the class ios arg2 is nothing but a bitfield . Hence formatting flag says the format action for her output. ios :: skipus 5.skip white space on input ios :: unitpuf 6.flush all stream after all insertion ios :: stdio 7.flush stdout and stderr after insertion Note: The flags set by setup( ) remain effective until they are reset. A format flag can be reset any no. of time in a programme .
Managing output with manipulators: The header file iomanip provides a set of functions called manipulators. They are used to manipulate the output formats. Manipulators and their meanings:
Manipulators Meaning equivalent Setw ( int w) Set the field width of w Width ( ) Set precision (int d) Set the floating point precision to d Precision( ) Set fill(int c) Set the fill character to c Fill ( ) Setiosflags (long f) Set the format flag f Setf( ) endl Insert new line and flush stream “/n”
Designing out own Manipulators We can design our own manipulators for some special characters. Syntax: For creating a manipulator without any argument is O stream & manipulator( ostream & output) { ------- ------- ------- Return(output); } Ex: Ostream & unit( ostream&output ) { Output<<”inches”; Return<<output; }
The above function defines a manipulator called unit that displays inches. This statement cout <<36<<unit; will produce the output 36 inches #include< iostream > #include< iomanip.h > Using namespace std; //user defined manipulators Ostream & currency( ostream &output) { Output<<”Rs”; Return output; } Ostream &form ( ostream & output) { Output.setf ( cos :: show pos);