home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch12 / format.cpp < prev    next >
C/C++ Source or Header  |  2005-06-16  |  2KB  |  62 lines

  1. //             format.cpp
  2. //
  3. // Synopsis  - Displays the integer 12 in different 
  4. //             formats. Then displays the floating 
  5. //             point number 12.3456789 in different formats.
  6. //
  7. // Objective - To explore the formatting facilities in C++.
  8. //
  9.  
  10. //Include Files
  11. #include <iostream.h>
  12.  
  13. int main()
  14. {
  15.                                                      // Note 1
  16.     cout << 12 << endl;
  17.     cout << "Default width is " << cout.width() << endl;
  18.     cout.width( 8 );
  19.     cout << 12 << endl;
  20.     cout << 12 << endl << endl;
  21.  
  22.                                                      // Note 2
  23.     cout << "Default fill character = '"
  24.          << cout.fill() << "'" << endl;
  25.     cout.fill( '.' );
  26.     cout << "Now fill character is '" 
  27.          << cout.fill() << "'" << endl;
  28.     cout.width( 8 );
  29.     cout << 12 << endl << endl;
  30.  
  31.                                                      // Note 3
  32.     cout.setf( ios::hex | ios::uppercase );
  33.     cout << "Integer output set to hex and uppercase\n";
  34.     cout.width( 8 );
  35.     cout << 12 << endl << endl;
  36.  
  37.                                                      // Note 4
  38.     int precisn = cout.precision();
  39.     cout << "Default precision is " << precisn << endl;
  40.     cout << 12.3456789 << endl;
  41.     cout.precision(2);
  42.     cout << "Now precision is " << cout.precision() << endl;
  43.     cout << 12.3456789 << endl << endl;
  44.  
  45.                                                      // Note 5
  46.     cout.setf( ios::fixed );
  47.     cout << "Floating point output has been set to fixed "
  48.          << "and precision is " << cout.precision() << ".\n";
  49.     cout << 12.3456789 << endl;
  50.     cout.precision( 6 );
  51.     cout << "Now with precision " << cout.precision() 
  52.          << " and fixed floating point output.\n";
  53.     cout << 12.3456789 << endl << endl;
  54.     cout.unsetf( ios::fixed );
  55.     cout.setf( ios::scientific );
  56.     cout << "With scientific floating point output "
  57.          << "and precision " << cout.precision() << endl;
  58.     cout << 12.3456789 << endl;
  59.  
  60.     return 0;
  61. }
  62.