![]() |
VOOZH | about |
In C++, the <iomanip> header file defines the manipulator functions that are used to manipulate the format of the input and output of our program. It is a part of the input/output library in C++. In this article, we will discuss the functions and facilities provided in the <iomanip> header file in C++ with examples.
The following table lists the commonly used <iomain> functions:
| S. No. | Function | Description | Syntax |
|---|---|---|---|
| 1 | resetiosflags | This function is used to reset or clear ios_base flags specified by the parameter mask. | resetiosflags (ios_base :: fmtflags mask); |
| 2 | setiosflags | This function is used to set the ios_base flags specified by the parameter mask. | setiosflags(ios_base :: fmtflags mask); |
| 3 | This function is used to set the base according to the argument specified as a parameter to the function. | setbase(int base); | |
| 4 | This function is used to change the fill character. It is used to set the fill according to the character specified as a parameter to the function. | setfill(char c); | |
| 5 | This function is used to change the floating-point precision based on the precision specified as a parameter to the function. | setprecision(int n); | |
| 6 | This function is used to change the width of the next input/output field based on the width specified as a parameter to the function. | setw(int n); | |
| 7 | get_money | This function is used to parse a monetary value. | get_money(moneyT& mon, bool intl = false); |
| 8 | put_money | This function is used to format and output a monetary value. | put_money(const moneyT& mon, bool intl = false); |
| 9 | get_time | This function is used to parse a date/time value according to the specified format. | get_time(tm* tmb, const charT* fmt); |
| 10 | put_time | This function is used to format and output a date/time value according to the format specified. | put_time(const tm* tmb, const charT* fmt); |
| 11 | This function is used to insert and extract quoted strings with embedded space. | quoted(const CharT* s, CharT delim = CharT(' " '), CharT escape = CharT('\\')); |
Lets discuss each of these functions one by one.
The setiosflag function is used to set the ios_base flags that are specified by its parameter for the stream it is used on.
setiosflags (ios_base::fmtflags mask);
Here,
Setting the showbase flag : 0x3c
The resetiosflags function in C++ is used to reset or clear ios_base flags specified by parameter mask for the stream it is used on.
resetiosflags (ios_base::fmtflags mask);
where,
Before resetting the showbase flag : 0X64 After resetting the showbase flag : 64
The setbase function is used to set the base according to the argument specified as a parameter to the function.
setbase(int base);
Here,
Example :
Number before setting the base : 30 Number after setting the base to octal : 36
The setw function is used to change the width of the next input/output field based on the width specified as a parameter to the function.
setw(int n);
Here,
Original Number : 30 Number after setting width : 30
The setfill function is used to change the fill character that is used when the span of the printed data is less than the span specified using setw. It is used to set the fill according to the character specified as a parameter to the function.
setfill (char c);
Here,
Original number : 30 Number after setting the fill character to $ : $$$$$$$$$$$$$$$$$$30
The setprecision function is used to change the floating-point precision of the float and double data type based on the precision specified as a parameter to the function.
setprecision(int n);
Here,
Original Number : 10.6126 Number after setting precision to 3 : 10.6
The get_money function in C++ is used to parse a monetary value from the input stream.
get_money(moneyT& mon, bool intl = false);
Here,
Output
Enter the price: 22
The price is: 22The put_money function is the counterpart of get_money function is used to format and output a monetary value to the given output stream.
put_money(const moneyT& mon, bool intl = false);
Here,
The amount is : 101
The get_time function in C++ is used to parse a date and time value from the input stream according to the specified format.
get_time(tm* tmb, const charT* fmt);
Here,
Output
Enter the time: 10:15:03
The time entered is:
10 hours 15 minutes 3 seconds The put_time function is used to format and output a date/time value according to the format specified for the given stream.
put_time(const tm* tmb, const charT* fmt);
Here,
Current System Time: 05:45:55 PM
The quoted function in C++ is used to insert and extract quoted strings with embedded space.
quoted(const CharT* s, CharT delim = CharT(' " '), CharT escape = CharT('\\'));
Here,
Original string: Hello World! String after quoted : "Hello World!"