iomanip in C++
In this article, we will discuss the iomanip in C++ with its examples and outputs.
iomanip is a library in C++. It stands for "input/output manipulation" and is part of the C++ Standard Library. The iomanip library provides a set of tools (manipulators) to manipulate the formatting and presentation of data when reading from or writing to streams, such as std::cout for output and std::cin for input.
The iomanip library is included in the <iomanip> header file, and it provides various manipulators that you can use in combination with stream insertion (<<) and stream extraction (>>) operators. These manipulators allow you to control the appearance of numeric values, strings, and other data types in the output stream and adjust the formatting when reading data from the input stream.
Some common manipulators provided by iomanip include std::setw, std::setprecision, std::fixed, std::scientific, std::left, std::right, and std::setfill, among others. These manipulators offer a flexible and powerful way to format data for display and are particularly useful when working with tables, columns, or any formatted output.
By including the <iomanip> header, you can use these manipulators to enhance the visual representation of your program's output and improve the readability of data presented to the user.
Manipulators are special functions that modify the behavior of the C++ streams, such as std::cout for output and std::cin for input. They do not take regular function call syntax, like parentheses (), but instead, they are used in conjunction with the stream insertion (<<) and stream extraction (>>) operators.
std::setiosflags Manipulator
The std::setiosflags is a manipulator used in combination with the std::cout stream to set specific formatting flags. It allows you to modify the formatting options for the output stream, making it easier to control how data is displayed.
The std::setiosflags manipulator is defined in the <iomanip> header and takes an argument of type std::ios_base::fmtflags, which represents a combination of formatting flags. The formatting flags are used to control various aspects of the output stream, such as setting the output to scientific notation, fixed-point notation, aligning the output, and more.
Example program:
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
int number = 2023;
double phi = 1.61803399;
// Using only one flag in setiosflags
cout<<setiosflags(ios::fixed);
cout<< phi <<endl;
// Using more than one flag in setiosflags
cout<< hex;
cout<<setiosflags (ios::showbase | ios::uppercase);
cout<< number <<endl;
return 0;
}
Output:
![iomanip in C++](https://static.tutorialandexample.com/cpp/iomanip-in-cpp1.png)
std::resetiosflags Manipulator
The std::resetiosflags is a manipulator used in combination with the std::cout stream to reset specific formatting flags that were previously set using std::setiosflags. It allows you to clear or unset specific formatting options, reverting the stream to its default formatting state.
The std::resetiosflags manipulator is defined in the <iomanip> header and takes an argument of type std::ios_base::fmtflags, which represents a combination of formatting flags. By specifying the formatting flags as the argument to std::resetiosflags, you can reset those specific flags to their default values.
To use std::resetiosflags, you need to specify the formatting flags you want to reset as the flags argument. You can combine multiple flags using the bitwise OR (|) operator if you want to reset multiple formatting options simultaneously.
Example program:
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
int number = 2023;
double phi = 1.61803399;
// Using only one flag in setiosflags
cout<<setiosflags(ios::fixed);
cout<< phi <<endl;
// Removing the flags using resetiosflags
cout<<resetiosflags(ios::fixed);
cout<< phi <<endl;
// Using more than one flag in setiosflags
cout<<setiosflags (ios::showbase | ios::uppercase);
cout<< hex << number <<endl;
// Removing the flags using resetiosflags
cout<<resetiosflags (ios::showbase | ios::uppercase);
// Here the base is not printed
// The capital letters are replaced by default lower case letters
cout<< hex << number <<endl;
return 0;
}
Output:
![iomanip in C++](https://static.tutorialandexample.com/cpp/iomanip-in-cpp2.png)
std::setfill Manipulator
The std::setfill is a manipulator provided by the <iomanip> header that allows you to set the fill character used for padding when formatting the output. It is used in conjunction with the std::cout stream to control how empty spaces are filled when printing data with a specified width.
The std::setfill manipulator takes an argument of type char and sets the specified character as the fill character for the output stream. The fill character is used to pad the output when the printed data is narrower than the specified field width.
To use std::setfill, you need to specify the character you want to use as the fill character as the c argument. The character will be repeated as needed to fill the empty spaces in the output when using other manipulators like std::setw.
Example program:
#include <iostream>
#include <iomanip>
int main() {
using namespace std;
int number = 1947;
cout<< "Default fill: {" << number << "}" <<endl;
cout<<setfill('$');
cout<< "Fill the spaces with '$': (" <<setw(10) << number << ")" <<endl;
cout<<setfill('@');
cout<< "Fill the spaces with '@': [" <<setw(10) << number << "]" <<endl;
return 0;
}
Output:
![iomanip in C++](https://static.tutorialandexample.com/cpp/iomanip-in-cpp3.png)
std::setbase(char c)
The std::setbase is a manipulator provided by the <iomanip> header that allows you to set the numeric base used for displaying integer values when formatting the output. It is used in conjunction with the std::cout stream to control how integer values are represented in different number systems, such as decimal, hexadecimal, or octal.
The std::setbase manipulator takes an argument of type int and sets the numeric base for the output stream. The valid numeric base values are 8, 10, or 16, representing octal, decimal, and hexadecimal, respectively.
To use std::setbase, you need to specify the desired numeric base as the base argument. The output of integer values will be formatted according to the specified base.
Example program:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int num = 255;
// Printing number in decimal form
cout<< "Normal : " <<num<<endl;
// Printing number in octal form
cout<< "Octal : " <<setbase(8) <<num<<endl;
// Printing number in hexa decimal form
cout<< "Hexadecimal : " <<setbase(16) <<num<<endl;
return 0;
}
Output:
![iomanip in C++](https://static.tutorialandexample.com/cpp/iomanip-in-cpp4.png)
std::setprecision Manipulator
The std::setprecision is a manipulator provided by the <iomanip> header that allows you to control the precision (number of decimal places) when formatting floating-point values for output. It is used in conjunction with the std::cout stream to set the number of digits to be displayed after the decimal point for floating-point numbers.
The std::setprecision manipulator takes an argument of type int and sets the desired precision for the output stream.
Example program:
#include <iostream>
#include <iomanip>
int main() {
using namespace std;
double phi = 1.61803399;
cout<< "Intial precision : " << phi <<endl;
// Here only 3 numbers after the point will printed
cout<< "Precision set to 4 is : " <<setprecision(4) << phi <<endl;
// Here only 1 numbers after the point will printed
cout<< "Precision set to 2 is : " <<setprecision(2) << phi <<endl;
cout<< "Precision set to 15 is : " <<setprecision(15) << phi <<endl;
cout<< "Precision set to 0 is : " <<setprecision(0) << phi <<endl;
return 0;
}
Output:
![iomanip in C++](https://static.tutorialandexample.com/cpp/iomanip-in-cpp5.png)
std::setw Manipulator
The std::setw is a manipulator provided by the <iomanip> header that allows you to set the width of the output field when formatting data for output. It is used in conjunction with the std::cout stream to control the minimum width of the output when printing values.
The std::setw manipulator takes an argument of type int and sets the desired width for the output stream.
To use std::setw, you need to specify the desired width n as the argument. The output will be formatted with a field width of at least n characters, and if the printed value is narrower than the specified width, it will be padded with the specified fill character (by default, it is a space character).
Example Program:
#include <iostream>
#include <iomanip>
int main() {
using namespace std;
int num = 2023;
cout<< "width: [" <<num<< "]" <<endl;
// spaces are printed according to the given number in the function
cout<< "Width set to 10 : [" <<setw(10) <<num<< "]" <<endl;
// fancy printing of spaces
cout<< "{" <<setw(10) <<num<<setw(5) << "}" <<endl;
return 0;
}
Output:
![iomanip in C++](https://static.tutorialandexample.com/cpp/iomanip-in-cpp6.png)
std::showpoint Manipulator
The std::showpoint is a manipulator provided by the <iomanip> header that controls the display of the decimal point for floating-point values when formatting the output. It is used in conjunction with the std::cout stream to specify whether to show or hide the decimal point when printing floating-point numbers.
The std::showpoint manipulator affects how floating-point values are displayed, especially when they have no fractional part. To use std::showpoint, you simply insert it into the stream using the << operator before outputting floating-point values.
Example Program:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double num = 19.0;
// Decimal point is not displayed for whole numbers
cout<< "Default : " <<num<<endl;
// Display the decimal point even for whole numbers
cout<< "showpoint : " <<showpoint<<num<<endl;
return 0;
}
Output:
![iomanip in C++](https://static.tutorialandexample.com/cpp/iomanip-in-cpp7.png)
std::left and std::right Manipulators
The std::left and std::right are manipulators provided by the <iomanip> header that control the justification (alignment) of data when formatting the output. They are used in conjunction with the std::cout stream to specify whether the data should be left-justified or right-justified within a specified field width.
std::left:
The std::left is a manipulator that left-justifies the output within the specified field width. When using std::left, the data is aligned to the left, and any remaining empty spaces within the field width are filled to the right with the specified fill character (by default, it is a space character).
std::right:
The std::right is a manipulator that right-justifies the output within the specified field width. When using std::right, the data is aligned to the right, and any remaining empty spaces within the field width are filled to the left with the specified fill character (by default, it is a space character).
To use std::left or std::right, you simply insert the desired manipulator into the stream using the << operator before outputting the data.
Example Program:
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int number = 1947;
// Number is printend on the right
cout<< "right justified" <<endl;
cout<<setw(10) << number <<setw(10) << "hello" <<endl;
// Number is printef on the left
cout<< "left justified" <<endl;
cout<< left <<setw(10) << number <<setw(10) << "hello" <<endl;
// Fancy printing
cout<< left <<setw(20) << "British India" << left << number <<setw(20) << right << "Independent India"<<endl;
}
Output:
![iomanip in C++](https://static.tutorialandexample.com/cpp/iomanip-in-cpp8.png)
Conclusion:
In conclusion, the <iomanip> library in C++ provides a powerful set of manipulators that allow programmers to have fine-grained control over the formatting and presentation of data when working with streams. These manipulators can be used in conjunction with std::cout for output and std::cin for input, offering a flexible and elegant way to display and read data in various formats.
The std::setw, std::setprecision, std::setbase, std::setfill, std::setiosflags, and std::resetiosflags manipulators provide a wide range of functionalities. The std::setw allows us to set the width of the output field, making it easier to align data in columns and tables. The std::setprecision controls the number of decimal places displayed for floating-point values, ensuring accurate and concise presentation of numeric data.
The <iomanip> library in C++ also offers the std::boolalpha manipulator. This manipulator affects how boolean values (true or false) are displayed when sent to the output stream.
By default, boolean values are displayed as integral values (0 for false and 1 for true). However, when using std::boolalpha, boolean values are displayed in a more human-readable form as "true" or "false".
With std::setbase, we can easily switch between different number bases like decimal, hexadecimal, and octal, making it valuable for low-level programming and various mathematical operations. The std::setfill lets us specify the character used for padding, enabling us to add visual clarity and aesthetics to the output.
The std::setiosflags and std::resetiosflags manipulators give us the ability to modify and revert specific formatting flags for the output stream, enhancing the overall control of the data display.
Moreover, std::showpoint, std::fixed, std::scientific, std::left, and std::right manipulators add even more versatility. The std::showpoint helps in consistently displaying decimal points and trailing zeros for floating-point numbers. The std::fixed and std::scientific let us switch between fixed-point and scientific notation conveniently.
The alignment manipulators, std::left and std::right, empower us to neatly align data to the left or right within specified field widths, creating visually appealing tables and columns.
The <iomanip> library is a powerful tool in programming, primarily used in languages like C++ and Python. It provides functionality to manipulate input and output formatting, allowing developers to control precision, width, and alignment. Its simplicity and versatility make it a must-have for tasks involving data representation and formatting.
Overall, the <iomanip> library provides an essential toolkit for customizing the appearance of program output, enabling developers to deliver clear, concise, and well-formatted information to users. By leveraging these manipulators, programmers can improve the readability and aesthetics of their programs, making it easier for users to interpret and interact with the data presented.