The C++ bit format flags changes the characteristics of IO stream.The flags hold the current setting for the IO stream. We can turn the flags on or off using builtin function such as setf()
or unsetf
(). Changing the behavior of the cout
object is useful in displaying output only after bit level manipulation.
Here is the list of flags available in C++ .
Flag Name | Description |
skipws | skip whitespace while input |
right | left justify the output |
internal | pad after sign or base |
dec | decimal base |
hex | hexadecimal base |
oct | octal base |
showbase | display base for hex and oct |
showpoint | shows all decimal point for float |
uppercase | shows hex in uppercase |
showpos | display a + sign for positive numbers |
scientific | display float numbers with E |
fixed | floating point notation |
unitbuf | flush all streams after cin |
stdio | flush stdout,stderr after cin |
setf() function
The setf() function sets the bit format flags. It takes the flag name as the argument. The syntax to use this fucntion is given below.
setf(ios::<flag_name>);
You can use bitwise or operator to set more than one field.
setf(ios::<field1>| ios::<field2> | ios::<field3>);
unsetf() function
The unsetf() function unsets the bit format flag for cout object. The general syntax to use this function is given below.
unsetf(ios::<flag_name>);
Example #1 : skipws
//using skipws function
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Variable declaration
char animal_name[100];
//Reading values
cout << "Enter an animal name:";
cin >> skipws;
cin >> animal_name;
//printing output
cout << "Number = " << animal_name << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Output:
Enter an animal name:bengal tiger
Number = bengal
Example #2 : right,left, internal and adjustfield
/* Demo of adjustfield, left,right and internal bit format */
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//variable declaration
int number1, number2, number3;
//variable initialization
number1 = 100;
number2 = 230;
number3 = 436;
//using adjustfield and other bit formats
cout.setf(ios::showpos);
cout.setf(ios::left,ios::adjustfield);
cout.width(10);
cout << number1 << endl;
cout.setf(ios::right,ios::adjustfield);
cout.width(10);
cout << number2 << endl;
cout.setf(ios::internal,ios::adjustfield);
cout.width(10);
cout << number3 << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Output:
+100
+230
+ 436
Example #3 : dec, hex, oct, and showbase
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
//Variable declaration
int amount;
//Variable initialization
amount = 564;
//Converting base using showbase, hex, oct and dec
cout.setf(ios::showbase);
cout.setf(ios::hex,ios::basefield);
cout << "Hexadecimal =" << amount << endl;
cout.setf(ios::oct,ios::basefield);
cout << "Octal =" << amount << endl;
cout.setf(ios::dec,ios::basefield);
cout << "Decimal =" << amount << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Output:
Hexadecimal =0x234
Octal =01064
Decimal =564
Example #4 : scientific, fixed, showpoint and floatfield
/* Demo of all floating point format flags -
floatpoint, showpoint, scientific, fixed
*/
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//variable declaration
float pi;
float radius;
float area;
//variable initializtiom
pi = 3.14159;
radius = 2.23;
area = 0.0;
area = pi * (radius * radius);
// printing result using bit format flags
cout.setf(ios::showpoint);
cout.precision(3);
cout.setf(ios::scientific,ios::floatfield);
cout << "Pi=" << pi << endl;
cout << "Radius=" << radius << endl;
cout << "Area=" << area << endl;
cout << endl;
cout << endl;
cout.setf(ios::fixed,ios::floatfield);
cout << "Pi(fixed)=" << pi << endl;
cout << "Radius(fixed)=" << radius << endl;
cout << "Area(fixed)=" << area << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Output:
Pi=3.142e+000
Radius=2.230e+000
Area=1.562e+001
Pi(fixed)=3.142
Radius(fixed)=2.230
Area(fixed)=15.623