How To Manipulate Cout Object Using C++ I/Os Library - C++ Programming Tutorial
C++ Course / Object-Oriented Programming / How To Manipulate Cout Object Using C++ I/Os Library

How To Manipulate Cout Object Using C++ I/Os Library

BLUF: Mastering How To Manipulate Cout Object Using C++ I/Os Library is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: How To Manipulate Cout Object Using C++ I/Os Library

C++ is renowned for its efficiency. Learn how How To Manipulate Cout Object Using C++ I/Os Library enables low-level control and high-performance computing in the tutorial below.

The C++ ios base class incorporates features for formatting cout objects to exhibit different formatting attributes. By utilizing class scope static constants, this ios base class enables customization of cout objects to show trailing decimal points, prepend + before positive integers, and offer a range of other formatting functionalities.

Static Constants for Class Scope:

Class-level static constants declared within the base class declaration of ios play a key role in setting up different formatting options. These constants are accessed using the scope resolution operator (::) within the class scope. Flags, representing individual bits, are examples of such static constants within the ios base class. Enabling specific formatting functionalities involves setting these flags or bits to 1, indicating their activation.

There are two types of class-level static constants:

  • For independent flags, the second argument does not need any flag or bit.
  • In contrast, non-independent flags require a flag or bit in the second argument. This includes three formatting flag sets for the second argument and two or three constants for the first argument.
  • Independent flags:

Class scope static autonomous formatting constants and their corresponding functions are displayed in the table underneath.

Sl. No. Formatting Constants Name Purpose
1 ios_base::showpoint Show the trailing decimal points.
2 ios_base::showpos Before positive numbers, use +.
3 ios_base::boolalpha Show bool values as "true" or "false".
4 ios_base::showbase Use 0x for hex values and 0 for Oct values as base prefixes.
5 ios_base::uppercase 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.

Syntax:

Example

fmtflags setf (fmtflags);

Parameters:

  • 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, iosbase::fmtflags prev = cout.setf(iosbase::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 C++ program below demonstrates the use of the setf function:

C Programming Language:

Example

#include <bits/stdc++.h>
using namespace std;

int main()
{
	double number = 31.43;

	cout.setf(ios_base::showpos);

	cout << "The output is: " <<
			number << endl;

	return 0;
}

Output

Output

The output is: +31.43

Non-Independent Flags:

The following table displays different static non-independent formatting constants within the Class scope along with their corresponding values specific to certain functions.

  1. ios_base::adjustfield
Constants Purpose
ios_base::left Show a value at the field's left end.
ios_base::internal Show a value to the left of the field and the remainder of the number to the right.
ios_base::right Show a value at the field's right end.
  1. ios_base::floatfield
Constants Purpose
ios_base::fixed To receive fixed-point input and show floating-point values.
ios_base::scientific To receive inputs in scientific notation and show it as floating-point numbers.
  1. ios_base::basefield
Constants Purpose
ios_base::hex To receive input and output hexadecimal values.
ios_base::dec Taking input and displaying decimal numbers.
ios_base::oct 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.

Syntax-

Example

fmtflags setf (fmtflags, fmtflags);

Parameters:

  • 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(iosbase::fixed, iosbase::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, iosbase::fmtflags prevs = cout.setf(iosbase::fixed, iosbase::floatfield). As a result, in the iosbase class, prevs keeps the result of Class scope static constants as an individual flag.

The C++ code to achieve the aforementioned approach is presented below-

C++ Program:

Example

#include <bits/stdc++.h>
using namespace std;

int main()
{
	double number = 31.43;

	cout.setf(ios_base::fixed,
			ios_base::floatfield);
	cout.precision(4);
	
	cout << "The Output is: " <<
			number << endl;
	return 0;
}

Output

Output

The Output is: 31.4300

The unsetf function within the ios_base class allows for reverting back to the original state. By using setf, a bit is set to 1, and unsetf resets it back to 0.

Syntax:

void unsetf (fmtflags);

Parameters:

The parameter

  • fmtflags accepts a single bit or flag value.

The return value of this function does not include the outcome of the formatting constant.

The C++ code for executing the unsetf method is displayed here:

C++ Program:

Example

#include <bits/stdc++.h>
using namespace std;

int main()
{
	double number = 31.43;
	cout.setf(ios_base::fixed,
			ios_base::floatfield);
	cout.precision(4);
	cout << "The resulted number: " <<
			number << endl;

	cout.unsetf(ios_base::floatfield);

	cout << "The original number: " <<
			number << endl;
	
	return 0;
}

Output

Output

The resulted number: 31.4300
The original number: 31.43

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience