Flags without Bitfields
The different flags that does not have corresponding bit fields are as follows, :
(i) ios::showbase: This flag makes use of base indicator to display an output.
(ii) ios::showpos: This flag displays the preceeding positive number.
(iii) ios::showpoint: This flag displays the trailing decimal point and zeros.
(iv) ios: uppercase: This flag makes use of capital letters to display the hexadecimal (hex) output.
(v) ios:: skipws: This flag skips the white spaces that appear in the input data.
(vi) ios :: unitbuf: This flag flushes away the streams after completing all the insertions.
(vii) ios::stdio: This flag flushes away the future insertions related to the stdout and stderr streams.
(viii) ios:: stdio: This flag sets the stream in accordance to the standard input and output of C+,
(ix) ios::boolalpha: This flag converts the boolean values in the form of text i.e., either “true” or ‘’false”,
Example
#include<iostream:h>#include<conio.h>int main(){clrscr();cout.setf(ios::skipws);cout<<endl<<“WEL COME”;cout.setf(ios::showpos);cout<<endi<<1028;getch();return 0;}
Output
In this program, ios::skipws removes the white space and displays WELCOME. Finally, ios::showpos shows positive sign(+) before the number i.e., +1028.
2. Manipulators
Manipulators are used for manipulating (controlling) the output formats. They are similar to that of ios class member functions and flags. The only difference is that, the ios class member function returns the previous settings, whereas, the manipulator does not return the previous setting.
Manipulator along with cout statement can be written, as,
cout<<manip1<<manip2<<varl;
Where,
manip1, manip2 are manipulators and varl is C++ variable.
Pre-defined Manipulators
The various pre-defined manipulators are as follows,
(a) setw(int x): This manipulator sets the field width to the fixed size of x.
(b) setbase: This manipulator sets the base of the number system.
(c) setprecision (int y): This manipulator sets the floating point precision to y.
(d) setfill(char z): This manipulator sets the fill character to the variable ‘z’. In other words, it sets the fill character to the character stored in variable ‘z
(e) setiosflags(long d): This manipulator sets the format flag to the variable ‘d’.
(f) resetiosflags (long d): This manipulator eliminates the flags indicated by the variable 'd'.
(g) endl: This manipulator divides (splits) a new line and flushes the buffer stream.
(h) skipws: This manipulator skips (or removes) the blank spaces from the input data.
(j) noskipws: This manipulator does not skip blank spaces (white spaces) of the input data.
(j) ends: This manipulator closes the output sting by adding null character to it.
(k) flush: This manipulator flushes off the buffer streams. :
(l) lock: This manipulator grants lock on the file associated with the respective file handling function.
(m) ws: This manipulator eliminates the blank a present before the available first field.
(n) hex, oct, dee: This manipulator displays the hexadecimal, octal and decimal format for the number system.
Example
#iniclude<iostream.h>#include<iomanip.h>#include<conio.h>int main(){clrscr();cout<<setw(5)<<“Manipulators”;cout<<setiosflags(ios::oct);cout<<“\n” << “The octal number of 84 is :” <<84;cout<<endl;cout<<setw(10) <<setprecision(3)<<4.6666;getch();return 0;}
Output
In this program, the manipulator setw(5) is used to set the width of field to ‘5’. So, the string ‘Manipulator’ is displayed at column 5. The ‘setiosflag’ manipulator is used to set the octal setting. So, the equivalent octal number 124 is displayed using cout statement. Finally, the manipulator setprecision (3) sets the number to ‘3’ decimal point. So, the number 4.6666 is displayed
as 4.667 at 10th column.
3. Custom/User-defined Manipulators
User-defined manipulators or custom-defined manipulators are nothing but the manipulators defined by the programmer or user in accordance to the requirement of the program.
Syntax
ostream and manip_name(ostream &output)
{
statement1;
statement2;
statement3;
------------
return output;
};
Example
#include<iostream.h>#include<iomanip.h>#include<conio.h>ostream &newline(ostream &output){output<<“\n”;return output;}void main(){clrscr();cout<<1<<newline<<2 << newline<<3;getch();}
Output