This program doesn't really do anything - it just demonstrates the correct way to call three
stream member functions: setf
, unsetf
, and flags
(called two ways).
When used with the setf
function, the combination of the the three flags
ios::dec | ios::oct | ios::hex
is meaningless (it's not possible to simultaneously display data in three bases) and may have unpredictable behavior.
(Nevertheless, the statement compiles and runs without causing either a syntax or a runtime error. Its behavior
depends on which flag the library code checks first - Studio understandably displays the oput in decimal or base 10.)
Alternatively, the OR'ing the three flags together makes sense and is useful when used with unsetf
-
it clears all of the flags.
#include <iostream> using namespace std; int main() { unsigned default_flags = cout.flags(); // gets the default formating flags cout << hex << default_flags << endl; cout.setf(ios::dec | ios::oct | ios::hex); // makes no sense cout << hex << cout.flags() << endl; cout.unsetf(ios::dec | ios::oct | ios::hex); // useful cout << hex << cout.flags() << endl; cout.flags(default_flags); // restores the default flags cout << hex << cout.flags() << endl; return 0; }
Output:
201 e01 1 201