Double Format:

Initial Comments
/* Formatting  and    <iomanip.h>
                 file: 4ex3.cpp  (or used as 4ex3.appended)
   FALL 1998
   ___________________________________
   Jacob Y. Kazakia   jyk0
   September 28, 1998
   Programming example  3 of week 4
   Recitation Instructor: J.Y.Kazakia
   Recitation Section  01
   ___________________________________
 
Purpose: This program reads three numbers a, b, and c through the keyboard and outputs them on the screen in various FIXED  formats.
         DOUBLE PRECISION IS USED  ( 8 bytes per number )
           
Algorithm: The functions: setiosflags, setw, setprecision are
           used to format the output
 
              ios:: fixed
              ios:: scientific
              ios:: showpoint
*/
#include <iostream.h>
#include <iomanip.h>
 
void main()
{
//  variables are typed and defined
 
double a,b,c;     // the three numbers
 
cout<<" \n\n   Please enter 3 numbers \n  a = ";
cin>>a;
cout<<"\n  b = ";
cin>>b;
cout<<"\n  c = ";
cin>>c;
Since the function setiosflags is sent to cout, Data directed to the default output  will be in fixed  form
 // Output everything in fixed point form
 
cout<<setiosflags( ios::fixed );
 
//***********************************************************
Precision is set to 14 digits after the decimal point (this makes sense because the variables are declared double).
 
A string of numbers is sent out so that we can easily examine the location of data in the output.
 
The width is set to 20 for each one of the three numbers a, b, and c.
cout<<"\n\n\n    With precision 14 and width 20  \n";
cout<<setprecision(14);
 
cout<<"\n         1         2         3         4         5 \n";
cout<<"12345678901234567890123456789012345678901234567890 \n" ;
cout<< setw(20)<<a <<setw(20)<< b <<setw(20)<< c ;
 
//***********************************************************
Precision is set to 5 digits after the decimal point.
 
A string of numbers is sent out so that we can easily examine the location of data in the output.
 
The width is set to 10 for each one of the three numbers a, b, and c.
cout<<"\n\n\n    With precision 5  and width 10  \n" ;
cout<<setprecision(5);
 
cout<<"\n         1         2         3         4         5 \n";
cout<<"12345678901234567890123456789012345678901234567890 \n" ;
cout<< setw(10)<<a <<setw(10)<< b <<setw(10)<< c ;
 
//***********************************************************
 
cout<<endl<<endl;
cout<<" enter e (exit) to terminate the program....";
char w;
cin>>w;
}
 
 /*     THESE ARE SOME OF THE OUTPUTS
The first number, when entered as fixed, needs 13 significant digits.  Since it is declared as double, all this detail is maintained and reflected at the output.
                             
Notice that 14 digits after the decimal point are shown even if they are zero.
 
The numbers are placed into the 20 slot openings with right justification.
Please enter 3 numbers
  a = 12.00000034567
 
  b = -13.45678
 
  c = 0.00000000012345678
 
 
 
    With precision 14 and width 20
 
         1         2         3         4         5
12345678901234567890123456789012345678901234567890
   12.00000034567000  -13.45678000000000    0.00000000012346
 
Note that the third number is outputted as zero.
 
How can we see the detail in a short span of spaces?
With precision 5  and width 10
 
         1         2         3         4         5
12345678901234567890123456789012345678901234567890
  12.00000 -13.45678   0.00000
 
 enter e (exit) to terminate the program....
 
 
*************************************************************
Same story here for a different set of numbers.
 
Try to understand why the output looks as it does.
*************************************************************
 
 
   Please enter 3 numbers
  a = 12.000000001234567891234
 
  b = -3.0000000345
 
  c = 0.10101010202022222222345
 
 
 
    With precision 14 and width 20
 
         1         2         3         4         5
12345678901234567890123456789012345678901234567890
   12.00000000123457   -3.00000003450000    0.10101010202022
 
 
    With precision 5  and width 10
 
         1         2         3         4         5
12345678901234567890123456789012345678901234567890
  12.00000  -3.00000   0.10101
 
 enter e (exit) to terminate the program....                  */
 

© 2001 J.Y. Kazakia. All rights reserved