Trending September 2023 # Manupulating Output With Iomanip Library # Suggested October 2023 # Top 9 Popular |

Trending September 2023 # Manupulating Output With Iomanip Library # Suggested October 2023 # Top 9 Popular

You are reading the article Manupulating Output With Iomanip Library updated in September 2023 on the website We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Manupulating Output With Iomanip Library

Introduction to C++ iomanip

The iomanip is a library in C++ which helps us in manipulating the output of any C++ program. There are many functions in this library that help in manipulating the output. To name a few we have functions to reset flags, set fill characters, set precision, get date and time, etc. It is a part of input-output library of the C++ standard library. All these functions can be easily used whenever they should affect the state of the iostream objects. We will take a look at all these functions in detail ahead.

Functions of C++ iomanip Library

We have 10 functions in the iomanip library. Let us check the syntax of each of them.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

resetiosflags: It clears the specified ios_base flags.

setiosflags: It will set the ios_base flags.

setbase: It will change the base which is needed as per the said base.

setfill: It will fill with the character specified.

setprecision: It will change the precision of any floating-point number.

setw: It helps in changing the width of the next input or output field.

get_money: It will segregate money value.

put_money: It will format and generate an output which will be a monetary value.

get_time: It will segregate the date or time value which will be sent in a specified format.

put_time: It will format the date or time which will be providing as per the specified format.

Syntax of C++ iomanip setiosflags (ios_base::fmtflags mask);

The mask is the flag which has to be set.

resetiosflags (ios_base::fmtflags mask);

Here also the mask is the flag which has to be reset.

setbase (int base);

The base can be an integer to which the base is set.

setfill (char_type c);

This is a new fill character which is a character type variable that can be used to fill a string.

setprecision (int n);

The integer here is the new value of the decimal precision.

setw (int n);

Here the integer represents the number of characters that will be used as the width.

get_money (moneyT& mon, bool intl = false);

Here the first parameter will be the object where the monetary value will be stored. The second parameter will be a Boolean value.

put_money (constmoneyT& mon, bool intl = false);

Similarly, here also first parameter is the monetary value and second will be a Boolean value.

get_time (struct tm* tmb, constcharT* fmt);

The first parameter here will be a pointer where the date and time will be stored. The second format will be the format of date and time in which the time is desired.

put_time (conststruct tm* tmb, constcharT* fmt);

Similar to the above syntax, we will have a pointer to the structure and the second parameter will have a format in which the date or time is expected.

How iomanip Works in C++?


int main() { double num = 3.142857142857; cout<< "Precision before using iomanip function: n" <<num<<endl; cout<< "The precision is set using" << " setprecision to 3: n" <<setprecision(3); cout<<num<<endl; }

We have taken just one function for our understanding. We have taken a number where we are setting the precision. The iomanip library enables to change the precision. Initially, the precision is default and then we change it to 3 using the setprecision() function. This changes the precision value and the decimal number will not take only the first 3 decimal places. The output will be manipulated and the output will be displayed accordingly. This is because of the iomanip library.

Examples to Implement C++ iomanip

Below are the examples of C++ iomanip:

Example #1

Checking the examples of setprecision, setw, setfill, setiosflags and resetiosflags.


using namespace std; int main () { doubleflt_val =3.14159; cout<<setprecision(4) <<flt_val<< “when set to 4 n”; cout<<setprecision(5) <<flt_val<< “when set to 9 n”; cout<<setw(10); cout<< 77 <<endl; cout<<setfill (‘#’) <<setw (10); cout<< 15 <<endl; std::cout<<std::hex <<std::setiosflags (std::ios::showbase); std::cout<< 600 <<std::endl; std::cout<<std::resetiosflags(std::ios::showbase) << 100 <<std::endl; return 0; }


Explanation: The above program helps us understand 5 functions of iomanip library. We have imported the library iomanip which has manipulation functions present. We have used 5 functions. The first function is setprecision which helps in setting precision of the number defined. We have first set it to 4 and then to 9. We then set the width of the output field to 10. After this, we have used the setfill function where we are filling the number 15 with the character 15. We have set the width to 10. The first 8 characters are # then followed by 15. After this, we have used the setiosflags where we are setting the flag base. In a similar way, we can use resetiosflags function.

Example #2

Checking the example of get_time, put_time, setbase, get_money and put_money.


using namespace std; int main () { std::cout<<std::setbase(32); std::cout<<156<<std::endl; long double amt; std::cout<< “Enter the amount: “; if ( std::cout<< “Error reading pricen”; else std::cout<< “The amount that you have entered is: ” << amt << ‘n’; std::cout<< “Price:” <<std::put_money(10.50L) << ‘n’; structstd::tm clk; std::cout<< “Enter the time: “; if ( std::cout<< “Error reading timen”; else { std::cout<< “The entered time is: “; std::cout<<clk.tm_hour<< ” hours and ” <<clk.tm_min<< ” minutesn”; } using std::chrono::system_clock; std::time_t t = system_clock::to_time_t (system_clock::now()); structstd::tm * ptm = std::localtime(&t); std::cout<< “The local time is: ” <<std::put_time(ptm,”%c”) << ‘n’; return 0; }


Explanation: In this program, we are using the remaining 5 functions from iomanip library. We have imported the iomanip library as before and set the base using the setbase function. After that we have used the get_money function and put_money function which takes the amount and print it as per the function. The get_time and put_time functions help us in displaying time in a given format. The output will be as below:


The iomanip library like the name suggests is a manipulation library that helps us in manipulating the output we want. We can use the functions in this library and get the desired output as we want.

Recommended Article

This is a guide to the C++ iomanip. Here we discuss the Introduction to C++ iomanip and its different functions along with Examples and Code Implementation. You can also go through our suggested articles to learn more –

You're reading Manupulating Output With Iomanip Library

Update the detailed information about Manupulating Output With Iomanip Library on the website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!