< iomanip > Header in C++
Last Updated :
17 Dec, 2023
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.
C++ <iomanip> Functions
The following table lists the commonly used <iomain> functions:
This function is used to reset or clear ios_base flags specified by the parameter mask. |
resetiosflags (ios_base :: fmtflags mask); |
This function is used to set the ios_base flags specified by the parameter mask. |
setiosflags(ios_base :: fmtflags mask); |
This function is used to set the base according to the argument specified as a parameter to the function. |
setbase(int base); |
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); |
This function is used to change the floating-point precision based on the precision specified as a parameter to the function. |
setprecision(int n); |
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); |
This function is used to parse a monetary value. |
get_money(moneyT& mon, bool intl = false); |
This function is used to format and output a monetary value. |
put_money(const moneyT& mon, bool intl = false); |
This function is used to parse a date/time value according to the specified format. |
get_time(tm* tmb, const charT* fmt); |
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); |
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.
1. setiosflags in C++
The setiosflag function is used to set the ios_base flags that are specified by its parameter for the stream it is used on.
Syntax of setiosflags
setiosflags (ios_base::fmtflags mask);
Here,
- mask: It is actually the flag which we have to set.
Example of setiosflags
C++
#include <iomanip>
#include <ios>
#include <iostream>
using namespace std;
int main()
{
int num = 60;
cout << "Setting the showbase flag : " << hex
<< setiosflags(ios::showbase) << num << endl;
return 0;
}
|
Output
Setting the showbase flag : 0x3c
2. resetiosflags in C++
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.
Syntax of resetiosflags
resetiosflags (ios_base::fmtflags mask);
where,
- mask: It is the actual flag to be reset.
Example of reseriosflags
C++
#include <iomanip>
#include <ios>
#include <iostream>
using namespace std;
int main()
{
int num = 100;
cout << "Before resetting the showbase flag : " << hex
<< setiosflags(ios::showbase) << uppercase << num
<< endl;
cout << "After resetting the showbase flag : " << hex
<< resetiosflags(ios::showbase) << num << endl;
return 0;
}
|
Output
Before resetting the showbase flag : 0X64
After resetting the showbase flag : 64
3. setbase in C++
The setbase function is used to set the base according to the argument specified as a parameter to the function.
Syntax of setbase
setbase(int base);
Here,
- base: It is the base of the numerical values.
Example :
C++
#include <iomanip>
#include <ios>
#include <iostream>
using namespace std;
int main()
{
int num = 30;
cout << "Number before setting the base : " << num
<< endl;
cout << "Number after setting the base to octal : "
<< setbase(8) << num << endl;
return 0;
}
|
Output
Number before setting the base : 30
Number after setting the base to octal : 36
4. setw in C++
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.
Syntax of setw
setw(int n);
Here,
- n: It is an integer argument specifying the number of characters to be used as width.
Example of setw
C++
#include <iostream>
#include<ios>
#include<iomanip>
using namespace std;
int main() {
double num = 30;
cout<< "Original Number : " <<num<<endl;
cout<< "Number after setting width : " <<setw(6)<<num<<endl;
return 0;
}
|
Output
Original Number : 30
Number after setting width : 30
5. setfill in C++
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.
Syntax of setfill
setfill (char c);
Here,
- c: It is a variable of character type corresponding to which the fill is set.
Example of setfill
C++
#include <iomanip>
#include <ios>
#include <iostream>
using namespace std;
int main()
{
int num = 30;
cout << "Original number : " << setw(20) << num << endl;
cout
<< "Number after setting the fill character to $ : "
<< setfill( '$' ) << setw(20) << num << endl;
return 0;
}
|
Output
Original number : 30
Number after setting the fill character to $ : $$$$$$$$$$$$$$$$$$30
6. setprecision in C++
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.
Syntax of the setprecision
setprecision(int n);
Here,
- n: It is an integer argument specifying the new precision.
Example of setprecision
C++
#include <iostream>
#include<ios>
#include<iomanip>
using namespace std;
int main() {
double num = 10.61256589;
cout<< "Original Number : " <<num<<endl;
cout<< "Number after setting precision to 3 : " <<setprecision(3)<<num<<endl;
return 0;
}
|
Output
Original Number : 10.6126
Number after setting precision to 3 : 10.6
7. get_money in C++
The get_money function in C++ is used to parse a monetary value from the input stream.
Syntax of get_money
get_money(moneyT& mon, bool intl = false);
Here,
- mon: represents the object where the monetary value will be stored
- intl: Boolean value which is true of international representation and false otherwise.
Example of get_money
C++
#include <iomanip>
#include <ios>
#include <iostream>
using namespace std;
int main()
{
long double price;
cout << "Enter the price: " ;
cin >> get_money(price);
if (cin.fail()) {
cout << "Sorry! We had an error reading the price"
<< endl;
}
else {
cout << "The price is: " << price << endl;
}
return 0;
}
|
Output
Enter the price: 22
The price is: 22
8. put_money in C++
The put_money function is the counterpart of get_money function is used to format and output a monetary value to the given output stream.
Syntax of put_money
put_money(const moneyT& mon, bool intl = false);
Here,
- mon: represents the object where the monetary value will be stored
- intl: Boolean value which is true of international representation and false otherwise.
Example of put_money
C++
#include <iostream>
#include<ios>
#include<iomanip>
using namespace std;
int main() {
cout<< "The amount is : " <<put_money(100.51L)<<endl;
return 0;
}
|
Output
The amount is : 101
9. get_time in C++
The get_time function in C++ is used to parse a date and time value from the input stream according to the specified format.
Syntax get_time
get_time(tm* tmb, const charT* fmt);
Here,
- tmb: It is a pointer to the structure where the date and time information will be stored
- fmt: It represents the desired format of date and time.
Example of get_time
C++
#include <ctime>
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
struct tm time ;
cout << "Enter the time: " ;
cin >> get_time(& time , "%T" );
if (cin.fail()) {
cout << "Sorry! We had an error reading the time."
<< endl;
}
else {
cout << "The time entered is: " << endl;
cout << time .tm_hour << " hours " << time .tm_min
<< " minutes " << time .tm_sec << " seconds "
<< endl;
}
return 0;
}
|
Output
Enter the time: 10:15:03
The time entered is:
10 hours 15 minutes 3 seconds
10. put_time in C++
The put_time function is used to format and output a date/time value according to the format specified for the given stream.
Syntax of put_time
put_time(const tm* tmb, const charT* fmt);
Here,
- tmb: It is a pointer to the structure where the date and time information will be stored
- fmt: It represents the desired format of date and time.
Example of put_time
C++
#include <chrono>
#include <iomanip>
#include <iostream>
using namespace std;
using chrono::system_clock;
int main()
{
time_t t = system_clock::to_time_t(system_clock::now());
struct tm * ptm = localtime (&t);
cout << "Current System Time: " << put_time(ptm, "%r" )
<< endl;
return 0;
}
|
Output
Current System Time: 05:45:55 PM
11. quoted in C++
The quoted function in C++ is used to insert and extract quoted strings with embedded space.
Syntax of quoted
quoted(const CharT* s, CharT delim = CharT(' " '), CharT escape = CharT('\\'));
Here,
- s: String to be printed.
- delim: Character to be used as delimiter.
- escape: Character to be used as escape sequence.
Example of quoted
C++
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Hello World!" ;
cout << "Original string: " << str << endl;
cout << "String after quoted : " << quoted(str) << endl;
return 0;
}
|
Output
Original string: Hello World!
String after quoted : "Hello World!"
Share your thoughts in the comments
Please Login to comment...