How to Manipulate cout Object using C++ IOS Library?
The C++ ios base class includes aspects for formatting cout objects that display various formatting characteristics. Using class scope static constants, the following ios base class can style cout object to display trailing decimal points, add + before positive integers, and various other formatting capabilities.
Static Constants for Class Scope:
Class scope static constants defined in the ios base class declaration assist in configuring various formatting capabilities. The scope resolution operator (::) is used with the constant name in class scope. Individual bits known as flags are class scope static constants declared in the ios base class. To activate formatting abilities, set the flag or bit to 1, which signifies that the bit is set to 1.
There are two kinds of class scope static constants:
- Independent flags imply that no flag or bit is required for the second argument.
- Non-independent flags imply that we must use a flag or bit for the second argument. It offers three sets of formatting flags as a second argument and two or three constants as a first argument.
Class scope static independent formatting constants and their functions are shown in the table below.
||Formatting Constants Name
||Show the trailing decimal points.
||Before positive numbers, use +.
||Show bool values as "true" or "false".
||Use 0x for hex values and 0 for Oct values as base prefixes.
||For hex values and scientific notation, use capital letters.
setf(): The ios base class includes a setf() function for setting the particular bit or flag. The Setf() function returns two prototypes.
- fmtflags- It will accept either a bit or a flag.
- Return Value: It returns the formatting constant's result.
- The fmtflags argument is a bitmask type that stores individual bit values and is used to format flags or set a bit to 1.
- The cout object can be used to call the Setf() function. cout.setf(ios base::showpos) is an example.
- The formatting constant's return value can be preserved. For example, ios_base::fmtflags prev = cout.setf(ios_base::showpos). As a result, prior stores as a bit or flag the outcome of the Class scope static constants declared in the ios_base class.
The following C++ program follows the setf() function:
C Programming Language:
The table below shows various Class scope static non-independent formatting constants and their function-specific values.
||Show a value at the field's left end.
||Show a value to the left of the field and the remainder of the number to the right.
||Show a value at the field's right end.
||To receive fixed-point input and show floating-point values.
||To receive inputs in scientific notation and show it as floating-point numbers.
||To receive input and output hexadecimal values.
||Taking input and displaying decimal numbers.
||Taking input and displaying octal values
static class scope Fields are going to serve as the second argument to specify which bits to clear, while constants will act as the first argument to specify which bits to set. Using the second prototype, we will call the setf() function to format the cout object.
- fmtflags- It only takes two bits or flags.
- Return Value: It gives the formatting constant's result.
- The cout object can be used to call the Setf() function. cout.setf(ios_base::fixed, ios_base::floatfield) is an example.
- The second argument specifies which bit to remove. The setf() function formatted the cout object to show in fixed-point notation, which we utilised. The constant ios_base::fixed converts bit scientific notation to fixed-point notation. This is known as cleaning the bits. And the first parameter assigns one of those bits to the value 1.
- The formatting constant's return value can be preserved. For instance, ios_base::fmtflags prev_s = cout.setf(ios_base::fixed, ios_base::floatfield). As a result, in the ios_base class, prev_s keeps the result of Class scope static constants as an individual flag.
The C++ code to accomplish the aforesaid strategy is provided below-
unsetf(): The ios_base class includes an unsetf() function that can be used to bring back the initial result. The setf() function sets a bit to 1 and unsetf() returns it to 0.
void unsetf (fmtflags);
- fmtflags- It takes one bit or flag.
- Return Value: It doesn't return the formatting constant's result.
The C++ code for implementing the unsetf() function is shown below:
The resulted number: 31.4300
The original number: 31.43