//------------------------------------------------------------------------- // // io.cpp // // Written by: Elizabeth Sklar, Simon Parsons // (based on an example from Ira Pohl) // // Last modified: 10th October 2007 // // A program to illustrate the use of formatted output // //------------------------------------------------------------------------- // We need iostream for the basic i/o and cmath for the sin we use near // the end. #include #include using namespace std; int main() { const int A = 5; const double B = 3.4568; double C; // The output strings explain what is going on: cout << "Output using fixed precision, 2 decimal places:\n"; cout.setf( ios::fixed, ios::floatfield ); cout.precision( 2 ); cout << "B=" << B << endl; cout << "Output using width=10, left justified:\n"; cout.setf( ios::left ); cout.width( 10 ); cout << "B=" << B << endl; cout << "Output using width=10, right justified:\n"; cout.setf( ios::right ); cout.width( 10 ); cout << "B=" << B << endl; cout << "Repeat the formatting if you want the same thing again:\n"; C = sin( B ); cout.setf( ios::right ); cout.width( 10 ); cout << "C=" << C << endl; } // End of main()