Code /* * manips.cpp * CIS 250 * David Klick * 2014-01-04 * * Demonstration of some stream manipulators */ #include #include using std::cin; using std::cout; using std::endl; using std::setfill; using std::setw; using std::left; using std::right; using std::showpos; using std::noshowpos; using std::internal; using std::hex; using std::uppercase; using std::showbase; using std::noshowbase; using std::setprecision; using std::dec; using std::showpoint; using std::fixed; using std::boolalpha; using std::setiosflags; using std::ios; using std::scientific; int main(int argc, char* argv[]) { int i = 17; double x = 98.7654321; double y = 72.0; bool truth = true; // set width and fill character cout << setfill('*') << setw(5) << i << endl; // left justification cout << left << setw(5) << i << endl; // show sign even if positive cout << right << setw(5) << showpos << i << endl; // do padding between sign and value cout << setw(5) << internal << i << endl; // do padding between sign and value cout << setw(5) << noshowpos << i << endl; // uppercase hex output with 0 fill cout << setw(5) << hex << uppercase << setfill('0') << i << endl; // prepend base indicator cout << setw(5) << showbase << i << noshowbase << endl; // display fp number in field 8 wide with 2 decimal positions // this doesn't do quite what is expected - the setprecision is // interpreted as significant digits instead of decimal positions // because the display format internally is scientific cout << dec << setw(8) << setprecision(2) << x << endl; // show number in fixed format cout << setw(8) << fixed << x << endl; // display a number with no significant digits after decimal cout << setw(8) << setprecision(0) << y << endl; // force display of decimal point cout << setw(8) << showpoint << y << endl; // display boolean value as both number and as text cout << truth << " " << boolalpha << truth << endl; // demonstrate difference of setprecision for fixed/scientific // format vs. general format cout << fixed << setprecision(5) << x << endl; cout << setiosflags(ios::fixed | ios::scientific) << x << endl; // the base manipulators can also be used for input cout << "Enter a number in hex: "; cin >> hex >> i; cout << i << endl; return 0; } /* Sample output: ***17 17*** **+17 +**17 ***17 00011 0X011 00000099 00098.77 00000072 0000072. 1 true 98.76543 98.765 Enter a number in hex: 1a5 421 */