|
ex8-5.cpp - Example 8-5 Input/Output manipulators |
// File: ex8-5.cpp - I/O Mamipulators
#include <iomanip>
using namespace std;
#include "fmtflags.h"
int main() {
// save the initial cout flags settings
ios_base::fmtflags cout_fmtflags = cout.flags();
// Display the cout flags
show_fmtflags(cout);
// hex, oct, & dec manipulators
cout << hex << 123 << ' ' << oct << 123 << ' ' << dec << 123 << endl;
show_fmtflags(cout);
// Turn on showpos, uppercase, showpoint, left, hex, (Note: dec on)
cout << setiosflags(ios::showpos|ios::uppercase|ios::showpoint|
ios::showbase|ios::left|ios::hex)
<< 123 << endl;
show_fmtflags(cout);
// Clear the dec flag
cout << resetiosflags(ios::dec) << 123 << endl;
show_fmtflags(cout);
// Demonstrate the setfill and setw manipulators
cout << setfill('$') << setw(10) << 123 << endl;
cout << 123 << endl;
// Reset cout's flags back to the original settings
cout.flags(cout_fmtflags);
// Turn on hex
cout << hex << 123 << endl;
show_fmtflags(cout);
// Turn on octal
cout << oct << 123 << endl;
show_fmtflags(cout);
// 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);
// Back to decimal
cout << dec << 1.2 << ' ' << 3.14 << ' ' << 35 << ' ' << 3.14159 << endl;
show_fmtflags(cout);
// What is truth?
cout << true << ' ' << boolalpha << true << endl;
show_fmtflags(cout);
return 0;
}
CIS27: Programming in C++ Instructor: Joe Bentley