Formatted Console i/o Operations in C++

0
592

Formatted Console i/o Operations in C++

Formatted Console i/o Operations in C++ is the easiest topic in computer science, In this article, you will learn easily Formatted Console i/o Operations in C++. In this tutorial, we are going to learn about Formatted Console i/o Operations in C++.

Formatted Console io Operations in C++
Formatted Console io Operations in C++

C++ aids in the formatting of I/O operations such as deciding the number of digits to be shown after the decimal point, setting the number base, and so on.

Programme Example

If we want to add the + sign as a prefix to our output, we can do so using formatting:

stream.setf(ios::showpos)  
If input=100, output will be +100 

If we want to use the formatting to add trailing zeros to our output, we can do so as follows:

stream.setf(ios::showpoint) 
If input=100.0, output will be 100.000

The streams specified in C++, such as cin, cout, cerr, and clog, are referred to as stream.

There are two options for doing so:

  • Using the ios class or various ios member functions.
  • Using manipulators(special functions)
  1. Formatting using the ios members:

The format flags on the stream specify how it should be formatted. We can use the setf function to set the flags that enable us to show a value in a specific format. The ios class declares the values (showbase, showpoint, oct, hex, etc) in a bitmask enumeration called fmtflags. The format flags are set or cleared using these values.

Few standard ios class functions are:

width(): The width method is used to set the required field width. The output will be displayed in the given width
precision(): The precision method is used to set the number of the decimal point to a float value
fill(): The fill method is used to set a character to fill in the blank space of a field
set(): The set method is used to set various flags for formatting output
unset(): The unset method is used To remove the flag setting

2. Formatting using Manipulators

The second way to change a stream’s format parameters is to use manipulators, which are special functions that can be used in an I/O expression.
The following are the traditional manipulators:

flush: The flush manipulator of stream manipulators in C++ is used to Flush a stream
hex: The hex manipulator of stream manipulators in C++ is used to Turns on hex flag
internal: The internal manipulator of stream manipulators in C++ is used to Turns on internal flag
left: The left manipulator of stream manipulators in C++ is used to Turns on the left flag
noboolalpha: The noboolalpha manipulator of stream manipulators in C++ is used to Turns off bool alpha flag
noshowbase: The noshowbase manipulator of stream manipulators in C++ is used to Turns off showcase flag
noshowpoint: The noshowpoint manipulator of stream manipulators in C++ is used to Turns off show point flag
noshowpos: The noshowpos manipulator of stream manipulators in C++ is used to Turns off showpos flag
noskipws: The noskipws manipulator of stream manipulators in C++ is used to Turns off skipws flag
nounitbuf: The nounitbuf manipulator of stream manipulators in C++ is used to Turns off the unit buff flag
nouppercase: The nouppercase manipulator of stream manipulators in C++ is used to Turns off the uppercase flag
oct: The oct manipulator of stream manipulators in C++ is used to Turns on oct flag
resetiosflags(fmtflags f): The resetiosflags manipulator of stream manipulators in C++ is used to Turns off the flag specified in f
right: The right manipulator of stream manipulators in C++ is used to Turns on the right flag
scientific: The scientific manipulator of stream manipulators in C++ is used to Turns on scientific flag
setbase(int base): The setbase manipulator of stream manipulators in C++ is used to Set the number base to base
setfill(int ch): The setfill manipulator of stream manipulators in C++ is used to Set the fill character to ch
setiosflags(fmtflags f): The setiosflags manipulator of stream manipulators in C++ is used to Turns on the flag specified in f
setprecision(int p): The setprecision manipulator of stream manipulators in C++ is used to Set the number of digits of precision
setw(int w): The setw manipulator of stream manipulators in C++ is used to Set the field width to w
showbase: The showbase manipulator of stream manipulators in C++ is used to Turns on showbase flag
showpoint: The showpoint manipulator of stream manipulators in C++ is used to Turns on show point flag
showpos: The showpos manipulator of stream manipulators in C++ is used to Turns on showpos flag
skipws: The skipws manipulator of stream manipulators in C++ is used to Turns on skipws flag
unitbuf: The unitbuf manipulator of stream manipulators in C++ is used to turn on unitbuf flag
uppercase: The uppercase manipulator of stream manipulators in C++ is used to turn on the uppercase flag
ws: The ws manipulator of stream manipulators in C++ is used to skip leading white space

Read More –

LEAVE A REPLY

Please enter your comment!
Please enter your name here