#include #include using namespace std; void show_fmtflags(ios_base& stream); int main() { // save the initial cout flags settings ios_base::fmtflags cout_fmtflags = cout.flags(); // Display the cout flags show_fmtflags(cin); show_fmtflags(cout); show_fmtflags(cerr); show_fmtflags(clog); cout << endl; int x = 123; // hex, oct, & dec manipulators cout << "dec: x = " << dec << x << endl; cout << "hex: x = " << hex << x << endl; cout << "oct: x = " << oct << x << endl; show_fmtflags(cout); cout << endl; // Turn on showpos, uppercase, showpoint, left, hex cout << setiosflags(ios::showpos|ios::uppercase|ios::showpoint| ios::showbase|ios::left|ios::hex); show_fmtflags(cout); cout << "x = " << x << endl << endl; // Clear the oct flag cout << resetiosflags(ios::oct) << "x = " << x << endl; show_fmtflags(cout); cout << endl; // Demonstrate the setfill and setw manipulators cout << setfill('$') << setw(10) << "x = " << x << endl; cout << "x = " << x << endl << endl; // Reset cout's flags back to the original settings cout.flags(cout_fmtflags); // Turn on hex cout << hex << "x = " << x << endl; show_fmtflags(cout); cout << endl; // Turn on octal cout << oct << "x = " << x << endl; show_fmtflags(cout); cout << endl; // Demonstrate setprecision cout << setprecision(3) << 1.2 << ' ' << 3.14 << ' ' << 35 << ' ' << 3.14159 << endl; // Demonstrate setprecision with showpoint cout << showpoint << 1.2 << ' ' << 3.14 << ' ' << 35 << ' ' << 3.14159 << endl; // Demonstrate showpos cout << showpos << 1.2 << ' ' << 3.14 << ' ' << 35 << ' ' << 3.14159 << endl; show_fmtflags(cout); cout << endl; // Back to decimal cout << dec << 1.2 << ' ' << 3.14 << ' ' << 35 << ' ' << 3.14159 << endl; show_fmtflags(cout); cout << endl; // What is truth? cout << true << ' ' << boolalpha << true << endl; show_fmtflags(cout); } void show_fmtflags(ios_base& stream) { cout << (&stream == &cout ? "cout " : ""); cout << (&stream == &cerr ? "cerr " : ""); cout << (&stream == &clog ? "clog " : ""); cout << (&stream == &cin ? "cin " : ""); cout << "fmtflags set: "; cout << (stream.flags() & ios::boolalpha ? "boolalpha " : ""); cout << (stream.flags() & ios::dec ? "dec " : ""); cout << (stream.flags() & ios::fixed ? "fixed " : ""); cout << (stream.flags() & ios::hex ? "hex " : ""); cout << (stream.flags() & ios::internal ? "internal " : ""); cout << (stream.flags() & ios::left ? "left " : ""); cout << (stream.flags() & ios::oct ? "oct " : ""); cout << (stream.flags() & ios::right ? "right " : ""); cout << (stream.flags() & ios::scientific ? "scientific " : ""); cout << (stream.flags() & ios::showbase ? "showbase " : ""); cout << (stream.flags() & ios::showpoint ? "showpoint " : ""); cout << (stream.flags() & ios::showpos ? "showpos " : ""); cout << (stream.flags() & ios::skipws ? "skipws " : ""); cout << (stream.flags() & ios::unitbuf ? "unitbuf " : ""); cout << (stream.flags() & ios::uppercase ? "uppercase " : ""); cout << endl; }