Formatting Output

#include <iostream.h>
/************************************************
 File: format1.C
 An example illustrating the use of
 the setf() function to format output
************************************************/

int main()
{
     float y = 16.0011;
     // display using default settings
     cout << y << " hello" << "\n";

     // change format to scientific
     cout.setf(ios::scientific);
     cout << y << " hello" << "\n";

     // change format to decimal, show positive sign, show
     // fixed format
     cout.unsetf(ios::scientific);
     cout.setf(ios::showpos | ios::fixed);
     cout << y << " hello" << "\n";
     
     // reset format to eliminate the positive sign
     cout.unsetf(ios::showpos);
     cout << y << " hello" << "\n";

     // show the status of format flags
     long z = cout.flags();
     cout << "format flags: " << z << "\n";

     exit(0);
}

Output:

16.0011 hello
1.600110e+01 hello
+16.001101 hello
16.001101 hello
format flags: 36865

#include <iostream.h> /********************************************** File: format2.C An example illustrating the use of width() precision(), and fill() functions for formatting output **********************************************/ int main() { cout.width(10); // set minimum field width cout << "hello" << "\n"; cout.fill('*'); // set fill character cout.width(10); // set minimum field width cout << "hello" << "\n"; // right justify by default cout.setf(ios::left); // set left justification cout.width(10); // set minimum field width cout << "hello" << "\n"; // left justified output double d = 123.234567; cout.setf(ios::fixed); // set fixed numeric format cout.width(12); // set minimum field width cout << d << "\n"; // default precision of 6 digits after the decimal cout.width(12); // set minimum field width cout.precision(5); // set precision to 5 digits cout << d << "\n"; // 5 digits precision after the decimal exit(0); } Output: hello *****hello hello***** 123.234567** 123.23457***
#include <iostream.h> /****************************************************** File: format3.C Create a table of float values and and their squares using the I/O format flags ******************************************************/ int main() { double x; cout.setf(ios::fixed); cout.precision(3); cout << " x x^2\n\n"; for (x = 2.2 ; x <= 20.2 ; x++) { cout.width(7); cout << x << " "; cout.width(7); cout << x*x << '\n'; } exit(0); } Output: x x^2 2.200 4.840 3.200 10.240 4.200 17.640 5.200 27.040 6.200 38.440 7.200 51.840 8.200 67.240 9.200 84.640 10.200 104.040 11.200 125.440 12.200 148.840 13.200 174.240 14.200 201.640 15.200 231.040 16.200 262.440 17.200 295.840 18.200 331.240 19.200 368.640 20.200 408.040
#include <iostream.h> #include <iomanip.h> /*************************************************** File: format4.C Create a table of float values and their squares using the I/O manipulators ***************************************************/ int main() { double x; cout.setf(ios::fixed); cout << setprecision(3); cout << " x x^2\n\n"; for (x = 2.2 ; x <= 20.2 ; x++) { cout << setw(7) << x << " "; cout << setw(7) << x*x << endl; } exit(0); } Output: x x^2 2.200 4.840 3.200 10.240 4.200 17.640 5.200 27.040 6.200 38.440 7.200 51.840 8.200 67.240 9.200 84.640 10.200 104.040 11.200 125.440 12.200 148.840 13.200 174.240 14.200 201.640 15.200 231.040 16.200 262.440 17.200 295.840 18.200 331.240 19.200 368.640 20.200 408.040



Back to Previous Page

Document:
Local Date:
Last Modified On: