Open In App

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: 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:

Return Value: It returns the result of the formatting constant.

Below is the C++ program to implement setf() function:




// 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;
}

Output
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:

Return Value: It returns the result of the formatting constant.

Below is the C++ program to implement the above approach-




// 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;
}

Output
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:

Return Value: It does not return the result of the formatting constant.

Below is the C++ program to implement the unsetf() function:




// 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;
}

Output
The resulted number: 21.3400
The original number: 21.34

Article Tags :