Fixed Format:

This example illustrates the use of  fixed format
Place your mouse in the areas below to see comments about the corresponding sections of code

 

Code
 
/* Formatting  and  <iomanip.h>
            file: 4ex1.cpp(or used as 4ex1.appended)
   FALL 1998
   ___________________________________
   Jacob Y. Kazakia   jyk0
   September 28, 1998
   Programming example  1 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.
 
Algorithm: Uses the functions: setiosflags, setw, setprecision to format output
 
              ios:: fixed
              ios:: scientific
              ios:: showpoint
 
                                                      */
#include <iostream.h>
#include <iomanip.h>
 
void main()
{
//  variables are typed and defined
 
float 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;
 
// Output everything in fixed point form
 
cout<<setiosflags( ios::fixed );
 
//***********************************************************
 
cout<<"\n\n\n    With precision 5 and width 20  \n";
cout<<setprecision(5);
 
cout<<"\n         1         2         3         4         5 \n";
cout<<"12345678901234567890123456789012345678901234567890 \n" ;
cout<< setw(20)<<a <<setw(20)<< b <<setw(20)<< c ;
 
//***********************************************************
 
cout<<"\n\n\n    With precision 3  and width 10  \n" ;
cout<<setprecision(3);
 
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

 
 Please enter 3 numbers
  a = 123
 
  b = 0.67895
 
  c = 123456789.0987
 
 
 
    With precision 5 and width 20
 
         1         2         3         4         5
12345678901234567890123456789012345678901234567890
           123.00000             0.67895     123456792.00000
 
 
************************************************************

 
With precision 3  and width 10
 
         1         2         3         4         5
12345678901234567890123456789012345678901234567890
   123.000     0.679123456792.000
 
 
 
************************************************************
 
 
 
 
   Please enter 3 numbers
  a = 12.789
 
  b = 0.0000023
 
  c = -456.78967
 
 
 
    With precision 5 and width 20
 
         1         2         3         4         5
12345678901234567890123456789012345678901234567890
            12.78900             0.00000          -456.78967
 
 
    With precision 3  and width 10
 
         1         2         3         4         5
12345678901234567890123456789012345678901234567890
    12.789     0.000  -456.790
 
 enter e (exit) to terminate the program....             */

(text file of the code)

(printable file)

© 2001 J.Y. Kazakia. All rights reserved