C++

C++ Iomanip

Any C++ program’s output can be altered with the use of the iomanip package. This library contains a wide variety of functions that aid in modifying the output. Here are only a few examples of methods to reset the flags, adjust fill the characters, set accuracy, and retrieve the dates and times. It is a component of the input-output library found in the C++ standard library. Every time these functions need to change the state of iostream objects, they are all simple to use.

Example 1: Iomanip Setbase() Method in C++

The basefield flag ios library is set by the setbase() function based on the parameter provided as the method’s input. It accepts the integer argument base which corresponds to the base that is to be set as a parameter.  The setbase() function allows us to change the basefield of a numeric value to a different base.

We add the “iomanip” module for the usage of the setbase() method inside our code. We use the setbase() method in the main method. We set the decimal base by calling “10” as input and print the decimal base value of the number “100”. Just like this, we implement the base Hexa and octal inside the setbase() method and display the specified value in the given bases.

There, we have three base values generated from the specified base fields.

Example 2: Iomanip Setiosflags() Method in C++

The format flags of the ios library provided as the method’s parameter are set using the setiosflags() function of the iomanip library in C++. The “format_flag” is a parameter that this method accepts. There is nothing returned by this procedure. Only the stream manipulators are used by it.

#include <iostream>
#include <iomanip>
int main () {
  int MyNum= 80;
   std::cout << std::hex;
   std::cout << std::setiosflags
   (std::ios::showbase | std::ios::uppercase);
   std::cout << "showbase and upper case flag ="<<MyNum << std::endl;
   return 0;
}

After including the iomanip package, we employ the main method which we call the setiosflags method with the std command. Before that, we initialize the numerical value “80” in the variable “MyNum”. Then, we invoke the setiosflags method which triggers the showbase and uppercase flags for the specified number.

Here, the output prints the showbase and uppercase flags value in hex format.

Example 3: Iomanip Setw() Method in C++

The width that is passed in as a parameter to the setw() method of the C++ iomanip library is used to adjust the ios library field width. Now, with the program implemented, this method will be clearer.

#include <iomanip>
#include <ios>
#include <iostream>

using namespace std;

int main()
{
    int val = 40;

    cout << "Breadth,before using the setw : \n"
        << val << endl;
    cout << "Breadth,after "
        << " using setw to 4: \n"
        << setw(4);
    cout << val << endl;

    return 0;
}

We utilize the iomanip library for the setw() method here. Then, within the program’s main method, we declare and initialize the integer value “val” with the value “40”. We print the variable “val” before deploying the setw() method. After this, we use the setw() method and set the parameter value “4” which is the breadth of the value “40”.

The output shows the functionality of the setw() method in the following: 

Example 4: Iomanip Setfill() Method in C++

Using the character provided as an argument, the setfill() function of the C++ iomanip library sets the fill character for the iOS library. Consider the following program:

#include <iomanip>
#include <ios>
#include <iostream>

using namespace std;

int main()
{
    int number = 90;

    cout << "Before setfill(), char: "
        << setw(5);
    cout << number << endl;
    cout << "After setting the fill symbol"
        << " setfill to *: \n"
        << setfill('*')
        << setw(5);
    cout << number << endl;
    return 0;
}

We insert the library “iomanip” into the header section. Then, we enter the program’s main. We define the variable “number” and initialize it with the value “90”. First, we utilize the setw() method to set the width of the value “90”. Then, we invoke the setfill() method which occupies the width space with the symbol “*”.

The output shows the width of the value “90” and displays the filled symbol “*” on the width.

Example 5: Iomanip Setprecision() Method in C++

The floating point precision of the ios library is set using the setprecision() procedure of the C++ iomanip library based on the precision supplied as the method’s parameter.

#include <iomanip>
#include <ios>
#include <iostream>

using namespace std;

int main()
{
    double decimal_num = 2.718281828459045;

    cout << "Before Precision method: \n"
        << decimal_num<< endl;
    cout << "Precision using"
        << " setprecision to 3: \n"
        << setprecision(3);
    cout << decimal_num << endl;
    cout << "Precision using"
        << " setprecision to 7 : \n "
        << setprecision(7);
    cout << decimal_num << endl;
    return 0;
}

After including the iomanip in the header section, we construct the program’s main. Here, we declare a variable “decimal_num” of the data type double and set the floating-point value to it. Then, we first print the value of the float number. After this, we reduce the setprecision() method. Inside the setprecision() method, we assign the value “3”. In the next setprecision() method, we assign the parameter value “7”.

In the output, the floating value is generated first. Next, a value is generated which has a precision set to “3” and only displays three values. Lastly, we set the precision value “7” so the floating value generated has only seven digits.

Example 6: Iomanip Get_Time() Method in C++

The specified format is used to extract a timestamp from a stream. A time structure containing the value of the parameter is returned. The parameter includes the time pointer and the format pointer.

#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;
int main ()

{
    struct tm when;
    cout <> get_time(&when,"%R");
    if (cin.fail())
            {
             cout << "Error in the time you have entered\n";
            }
            else
 {
    cout << "The time you entered: ";
    cout<< when.tm_hour << " hrs " << when.tm_min<< " min\n";
             }
             return 0;
}

With the addition of the ctime module along with the iomanip module, we implement the code for the get_time() method. Here, inside our main method, we construct the structure as “tm” which uses the “when” keyword. The time is provided by the user as get_time() and is set in the “cin” command. The get_time takes the “when” keyword reference and the “%R” as input. The fail() function is employed inside the if statement to throw errors upon failure in time or it executes the time in the specified format given in the cout command.

The time is inputted as “16:23” which is then converted into the format utilized in the code.

Example 7: Iomanip Get_Money() Method in C++

The get_money() method is employed to take the characters from the data stream being processed and translate them into a monetary statement, which is then saved as the value of money_amount.

#include <iostream>
#include <iomanip>

int main () {
   long double price_amount;
   std::cout <> std::get_money(price_amount);

   if (std::cin.fail()) std::cout << "Error getting amount readable \n";
   else std::cout << "The amount entered is: " << price_amount << '\n';
return 0;
}

We take the amount of the money from the user. When the user enters the money amount, it is stored in the get_money() method as we pass the variable “price_amount” inside it. After this, we utilize the if-else to generate two possibilities. If the amount is entered correctly, the if statement is executed. Otherwise, the else statement executes.

The program we just built is now run, and the outcome is as follows:

Conclusion

The C++ iomanip user manual is presented here. As its name implies, the iomanip library is a manipulation library that aids us in modifying the desired output. Using the methods in this library, we can acquire the result that we want. The introduction of C++’s iomanip and its various functions along with samples and code implementation, are covered in this article.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.