Computer Science - Object Oriented Programming Using C++ - Discussion
Discussion Forum : Object Oriented Programming Using C++ - Section 1 (Q.No. 4)
4.
Format flags may be combined using
Discussion:
21 comments Page 1 of 3.
Shashank Singh said:
8 years ago
cout and cin contain several single-bit flags for format control. They are named in an enum in class ios as:
ios::skipws // skips whitspace on input
ios::left // left justification
ios::right // right justifiction
ios::internal // pads after sign or base character
ios::dec // decimal format for integers
ios::oct // octal format for integers
ios::hex // hex format for integers
ios::showbase // show the base character for octal or hex
ios::showpoint // show the decimal point for all floats
ios::uppercase // uppercase A-F for hex
ios::showpos // show +ve sign for numbers
ios::scientific // use exponential notation
ios::fixed // used oridnary decimal notation
ios::unitbuf // flush the buffer
These flags can be set with the member function setf. e.g.
cout.setf(ios::showpos);
They can be or'd together:
cout.setf(ios::showpos | ios::uppercase);
or some bits can be unset while one is being set:
cout.setf(ios::oct, ios::dec | ios::oct | ios::hex);
This sets the bit for oct, after unsetting the bits for oct, dec and hex, ensuring that only one of the bits is turned on.
unsetf turns bits off:
cout.unsetf(ios::showpos);
ios::skipws // skips whitspace on input
ios::left // left justification
ios::right // right justifiction
ios::internal // pads after sign or base character
ios::dec // decimal format for integers
ios::oct // octal format for integers
ios::hex // hex format for integers
ios::showbase // show the base character for octal or hex
ios::showpoint // show the decimal point for all floats
ios::uppercase // uppercase A-F for hex
ios::showpos // show +ve sign for numbers
ios::scientific // use exponential notation
ios::fixed // used oridnary decimal notation
ios::unitbuf // flush the buffer
These flags can be set with the member function setf. e.g.
cout.setf(ios::showpos);
They can be or'd together:
cout.setf(ios::showpos | ios::uppercase);
or some bits can be unset while one is being set:
cout.setf(ios::oct, ios::dec | ios::oct | ios::hex);
This sets the bit for oct, after unsetting the bits for oct, dec and hex, ensuring that only one of the bits is turned on.
unsetf turns bits off:
cout.unsetf(ios::showpos);
(1)
Anshuman kansal said:
1 decade ago
The class ios contains data members to store all the formatting information pertaining to a stream. Some of this data has a range of values and is stored in variables: the floating-point precision, the output field width, and the character used to pad the output (normally a space). The rest of the formatting is determined by flags, which are usually combined to save space and are referred to collectively as the format flags.
Sundar said:
1 decade ago
Let me explain in clear.
// Assume two 16-bit flags contains values as given below
intFlag1 = (00000000 10101010) In Binary.
intFlag2 = (00000010 01010101) In Binary.
// Now combine it with Bitwise-OR operator
intResult = intFlag1 | intFlag2 ;
// The combined result will be.
intResult = (00000010 11111111) In Binary.
Hope this will help you. Have a nice day!
// Assume two 16-bit flags contains values as given below
intFlag1 = (00000000 10101010) In Binary.
intFlag2 = (00000010 01010101) In Binary.
// Now combine it with Bitwise-OR operator
intResult = intFlag1 | intFlag2 ;
// The combined result will be.
intResult = (00000010 11111111) In Binary.
Hope this will help you. Have a nice day!
(2)
Kiran said:
1 decade ago
The <iostream> library defines a rich set of format flags. These flags enable you to control the width, alignment, precision, and so on for the displayed data.
Anshuman said:
1 decade ago
Actually bitwise OR operator is denoted by pipe symbol (|) which is use to combine.
Tushar said:
8 years ago
Can anyone tell me what is format flags, and what is its use?
Somya Agrawal said:
9 years ago
What are format flags? Please describe it in detail.
Johny said:
8 years ago
It is actually very beneficial. Thanks @Sunder.
Muhammad Usman said:
10 years ago
Can any one explain that what are format flags?
Supriya said:
3 years ago
3what is mean by format flag? Explain, please.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers