/** * format.cpp * 19-mar-2007/sklar * * demonstrates the use of: * -- formatted output * -- constants * -- the cmath library * */ #include #include using namespace std; int main() { const int A = 5; const double B = 3.4568; double C; 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 << "you have to 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() /* HERE'S THE OUPUT FROM A SAMPLE RUN OF THIS PROGRAM: output using fixed precision, 2 decimal places: B=3.46 output using width=10, left justified: B= 3.46 output using width=10, right justified: B=3.46 you have to repeat the formatting if you want the same thing again: C=-0.31 */