C++ set manipulator functions
- Manipulator functions are special stream functions that change certain characteristics of the input output.
- These are used to change the format flags and different values for astream.
- By using these you can change the output values and take place the inputted formatted
values. - All these functions are added in < iomanip.h > header file.
1. endl
2. setw
3. setbase
4. setfill
5. hex, dec, oct
6. ends
7. flush
endl :
-This manipulator is used in output statements like cout mainly.
-This manipulator moves the cursor to the next line.
-It works like '\n'(new line).
Syntax :
<< endl;
Eg :
cout<< "\n Enter the value " << endl;
setw() :
-It stands for set width.
-This is output manipulator.
- The purpose of this manipulator is to dispaly the minimum number of character locations
which a variable will take place.
-It works same as tab. The difference is that tab has fixed width while we can chnge the width
with setw().
Syntax :
setw(int width);
Eg :
cout << "Amount is " << setw(5) << amount;
Here it will display five spaces between the message "Amount is" and the value displayed
from variable amount.
hex,dec and oct :
-hex is used to convert any octal or decimal value to Hexadecimal number.
-dec is used to convert any octal or hexadecimal number to decimal number.
-oct is used to convert any hexadecimal or decimal number to oct number.
setbase() :
-Used to convert the base of one numeric value to another base value.
- It works similar to hex, oct and dec manipulators.
Syntax :
setbase(base value);
setfill() :
- This is used to fill blank spaces with a particular character.
-It works with setw() set manipulator.
Syntax :
setfill(char);
Eg :
setfill("@") --- It will fill the blank spaces with @ symbol.
ends :
-It is used with '\' escape sequence.
- It will set end of the string by taking null character.
Eg :
int x=122;
cout << "\n"<< "n"<< "no is" << x << ends;
flush :
-When there is no need of string or numeric data in a variable, then we can use this function.
- It is used to clean the memory variable.
Syntax :
cout.flush();
2 comments:
good useful.
Athar Ahmad
Thanks...:)
Post a Comment