/* testio1.cpp CIS 150 May 2008 David Klick This program demonstrates a number of output formatting techniques used with C++ output streams. */ #include #include using std::cin; // input stream using std::cout; // output stream using std::endl; // outputs newline and flushes output buffer using std::setw; // sets width of next field to be output // setw must be set before every field output using std::setprecision; // sets number of positions after decimal point using std::setfill; // sets the leading fill character using std::left; // left justification using std::right; // right justification using std::hex; // outputs numbers in base 16 using std::oct; // outputs numbers in base 8 using std::dec; // outputs numbers in base 10 using std::fixed; // forces fixed decimal display instead of scientific notation using std::showpoint; // outputs decimal point even if not needed using std::boolalpha; // outputs "true" and "false" for bool values using std::noboolalpha; // outputs 0 for false, and 1 for true using std::showbase; // adds indicator to output to specify number base using std::noshowbase; // omits indicator to output to specify number base using std::uppercase; // outputs hex digits and scientific notation in uppercase using std::nouppercase; // outputs hex digits and scientific notation in lowercase void cont(); int main() { // declare and initialize variables int n = 42; double x = 78.1544472; bool flag = false; cout << n << '\n'; cout << setw(5) << n << '\n'; cout << setfill('*') << setw(5) << n << '\n'; cout << left << setw(5) << n << '\n'; cout << right << setw(5) << n << '\n'; cout << setfill(' ') << hex << setw(5) << n << '\n'; cout << oct << setw(5) << n << '\n'; cout << showbase << hex << setw(5) << n << '\n'; cout << oct << setw(5) << n << '\n'; cout << uppercase; cout << showbase << hex << setw(5) << n << '\n'; cout << nouppercase; cout << showbase << hex << setw(5) << n << '\n'; cout << noshowbase << hex << setw(5) << n << '\n'; cout << oct << setw(5) << n << '\n'; cout << dec << setw(5) << n << "\n\n"; cout << x << '\n'; cout << setw(10) << setprecision(5) << x << '\n'; cout << fixed << showpoint; cout << setw(10) << setprecision(5) << x << "\n\n"; cout << flag << '\n'; cout << boolalpha << flag << '\n'; cout << noboolalpha << flag << "\n\n"; cont(); return 0; } void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); } /* Sample output: 42 42 ***42 42*** ***42 2a 52 0x2a 052 0X2A 0x2a 2a 52 42 78.1544 78.154 78.15445 0 false 0 */