< iomanip > Header in C++

Last Updated : 11 Jul, 2026

The <iomanip> header in C++ provides stream manipulators for customizing the formatting of input and output operations. It helps control how data is displayed or read without modifying the actual values.

  • Formats numeric output using width, precision, fill characters, and bases.
  • Provides manipulators for strings, currency, and date/time formatting.
C++
#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
    double num = 123.456789;

    cout << "Default: " << num << endl;
    cout << "Width: " << setw(12) << num << endl;
    cout << "Precision: " << setprecision(4) << num << endl;
    cout << "Fill Character: "
         << setfill('*') << setw(12) << num << endl;

    return 0;
}

Output
Default: 123.457
Width:      123.457
Precision: 123.5
Fill Character: *******123.5

Explanation: setw() specifies the width of the output field, setprecision() controls floating-point precision and setfill() specifies the character used to fill unused spaces.

Syntax

Include the header before using any of its manipulators.

#include <iomanip>

Common <iomanip> Manipulators

The following table lists the most commonly used manipulators available in the <iomanip> header.

ManipulatorDescription
setw()Sets the width of the next output field.
setfill()Specifies the fill character for padded output.
setprecision()Sets floating-point precision.
setbase()Changes the numeric base (decimal, octal, hexadecimal).
setiosflags()Sets formatting flags for a stream.
resetiosflags()Clears selected formatting flags.
quoted()Reads or writes quoted strings.
get_money()Reads monetary values from an input stream.
put_money()Writes monetary values to an output stream.
get_time()Reads formatted date/time values.
put_time()Writes formatted date/time values.

get_money() and put_money()

The get_money() and put_money() manipulators are used to read and write monetary values. get_money() extracts a monetary value from an input stream, while put_money() formats and displays it.

C++
#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
    long double amount;

    cout << "Enter amount: ";
    cin >> get_money(amount);

    if (cin.fail()) {
        cout << "Invalid input";
    }
    else {
        cout << "Formatted amount: "
             << put_money(amount) << endl;
    }

    return 0;
}

Input

12345

Output

Formatted amount: 12345

Explanation

  • get_money() reads the monetary value from the input stream and stores it in amount.
  • put_money() formats the stored monetary value before printing it.
  • Both manipulators work according to the current locale settings, so the displayed format may vary across systems.

get_time() and put_time()

The get_time() and put_time() manipulators are used to parse and format date and time values using the tm structure. get_time() reads a date or time from an input stream, while put_time() formats and prints it.

C++
#include <ctime>
#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
    tm timeInfo = {};

    cout << "Enter time (HH:MM:SS): ";
    cin >> get_time(&timeInfo, "%H:%M:%S");

    if (cin.fail()) {
        cout << "Invalid time format";
    }
    else {
        cout << "Formatted time: "
             << put_time(&timeInfo, "%I:%M:%S %p");
    }

    return 0;
}

Input

14:30:45

Output

Formatted time: 02:30:45 PM

Explanation

  • get_time() reads the input according to the specified format (%H:%M:%S) and stores it in the tm structure.
  • put_time() formats the stored time using the specified format (%I:%M:%S %p) before displaying it.
  • If the input does not match the specified format, the stream enters the failure state (cin.fail()).

Applications of <iomanip>

The <iomanip> header is commonly used in applications that require formatted input and output.

  • Displaying aligned tables and reports.
  • Formatting numbers in decimal, octal, or hexadecimal.
  • Parsing and displaying currency values.
  • Reading and printing formatted date/time values.
  • Handling quoted strings during input and output.
Comment