How to Manipulate cout Object using C++ IOS Library?
C++ ios_base class has its aspects to format cout object to display different formatting features. For example, the following ios_base class can format cout object to display trailing decimal points, add + before positive numbers, and several other formatting features using class scope static constants.
Class Scope Static Constants:
Class scope static constants that are defined in the ios_base class declaration acts as a helping companion to format different formatting features. Class scope means to use the scope resolution operator (::) with the constant name. Class scope static constants defined in the ios_base class are individual bits also called flags. To enable formatting features set the flag or bit which means setting the bit to 1.
Class scope static constants can be of two types:
- Independent flags- It means no need to take any flag or bit for the second argument.
- Non-independent flags- It means we need to take a flag or bit for the second argument. It provides three sets of formatting flags to be used as a second argument and has two or three constants to use as a first argument.
Independent Flags: The below table demonstrates Class scope static independent formatting constants and their functions-
S No. | Formatting Constants Name | Purpose |
---|---|---|
1. | ios_base::boolalpha | Display bool values as “true” and “false”. |
2. | ios_base::showpoint | Display trailing decimal points. |
3. | ios_base::uppercase | Use uppercase letters for hex values, scientific notation. |
4. | ios_base::showpos | Use + before positive numbers. |
5. | ios_base::showbase | Use base prefixes 0x for hex values, 0 for Oct values. |
setf(): The ios_base class provides a setf() function whose purpose is to set the individual bit or flag. Setf() method provides two prototypes.
Syntax:
fmtflags setf (fmtflags);
Parameters:
- fmtflags- It accepts a bit or a flag.
Return Value: It returns the result of the formatting constant.
- The argument fmtflags is a bitmask type that is used to store individual bit values and its purpose is to format flags or set a bit to 1.
- Setf() function can be invoke by using the cout object. For example, cout.setf(ios_base::showpos).
- The return value of the formatting constant can be saved. For example, ios_base::fmtflags prev = cout.setf(ios_base::showpos). Hence, prev stores the result of the Class scope static constants defined in the ios_base class as a bit or flag.
Below is the C++ program to implement setf() function:
C++
// C++ program to format cout object // to display plus sign before positive // decimal numbers as C++ treats // hexadecimal and octal numbers // as unsigned. #include <bits/stdc++.h> using namespace std; // Driver code int main() { double num = 21.34; // Setting the cout object // to show plus sign cout.setf(ios_base::showpos); // Prints the result cout << "The result is: " << num << endl; return 0; } |
The result is: +21.34
Non-Independent Flags:
The below table demonstrates different Class scope static non-independent formatting constants and their function-
Non-Independent Flags | Constants | Purpose |
---|---|---|
ios_base::basefield | ios_base::dec | To take input and display decimal values |
ios_base::oct | To take input and display octal values | |
ios_base::hex | To take input and display hexadecimal values | |
ios_base::floatfield | ios_base::fixed | To take input as fixed-point notation and display in floating-point values |
ios_base::scientific | To take input as scientific point notation and display in floating-point values |
|
ios_base::adjustfield | ios_base::left | Display a value at the left end of the field. |
ios_base::right | Display a value at the right end of the field. | |
ios_base::internal | Display a value at the left of the field and the rest of the number at the right of the field. |
Class Scope static Fields will be used as the second argument to indicate which bits to clear and constants used as the first argument to indicate which bit is to set. We will be invoking setf() function to format cout object using the second prototype.
Syntax-
fmtflags setf (fmtflags, fmtflags);
Parameters:
- fmtflags- It accepts 2 bits or flags.
Return Value: It returns the result of the formatting constant.
- Setf() function can be invoked by using the cout object. For example, cout.setf(ios_base::fixed, ios_base::floatfield).
- Here the second argument indicates which bit to clear. The setf() function formatted cout object to display in fixed-point notation and we have used scientific notation. The ios_base::fixed constant will change the bit scientific notation to fixed-point notation. This is called clearing the bits. And the first argument sets one of those bits to 1.
- The return value of the formatting constant can be saved. For example, ios_base::fmtflags prev_s = cout.setf(ios_base::fixed, ios_base::floatfield). Hence, prev_s stores the result of Class scope static constants as individual flag defined in the ios_base class.
Below is the C++ program to implement the above approach-
C++
// C++ program to format cout // object to display in floating // point values #include <bits/stdc++.h> using namespace std; // Driver code int main() { double num = 21.34; // Formatting the cout object // to display in fixed-point // notation cout.setf(ios_base::fixed, ios_base::floatfield); // Instructing the cout object // to show precision of 4 cout.precision(4); // Prints the result cout << "The result is: " << num << endl; return 0; } |
The result is: 21.3400
unsetf(): The ios_base class provides a unsetf() function has its aspects to bring back to its original result. The setf() function sets a bit to 1, and unsetf() function sets a bit back to 0.
Syntax:
void unsetf (fmtflags);
Parameters:
- fmtflags- It accepts 1 bit or flag.
Return Value: It does not return the result of the formatting constant.
Below is the C++ program to implement the unsetf() function:
C++
// C++ program to illustrate // unsetf() function #include <bits/stdc++.h> using namespace std; // Driver code int main() { double num = 21.34; // Formatting the cout object cout.setf(ios_base::fixed, ios_base::floatfield); cout.precision(4); // Prints the result cout << "The resulted number: " << num << endl; // Restoring the cout object cout.unsetf(ios_base::floatfield); // Prints the original number cout << "The original number: " << num << endl; return 0; } |
The resulted number: 21.3400 The original number: 21.34
Contact Us