/* File: setwidth.cpp

   Programmer: S. Imberman

   Description: Shows setwidth format for float numbers

   Date: 09/21/2008

*/

 

#include <iostream>

#include <iomanip>

using namespace std;

 

int

main()

{

 

     

      int num2;

 

 

     

      num2 = 1234;

 

      //demonstration of setw

      cout << "A width of 7" << '|'  << setw(7)

            << num2 << '|' << endl;

      cout << "A width of 2" << '|' << setw(2)

            << num2 << '|' << endl;

      cout << "A width of 5" << '|' << setw(5)

            << num2 << '|' << endl;

      cout << "No width specified " << '|' << num2 << '|' << endl;

      return 0;

 

}    

     

A width of 7|   1234|

A width of 2|1234|

A width of 5| 1234|

No width specified |1234|

Press any key to continue

 

-------------------------------------------------------------------------


/* File: leftrightformat.cpp

   Programmer: S. Imberman

   Description: Shows left and right justification for float numbers

   Date: 09/21/2008

*/

 

#include <iostream>

#include <iomanip>

using namespace std;

 

int

main()

{

      int num2;

      num2 = 1234;

 

      //demonstration of setw

      cout << "A width of 7" << '|'  << setw(7)

            << num2 << '|' << endl;

 

      cout << left << "A width of 7" << '|'  << setw(7)

            << num2 << '|' << endl;

 

      cout << right << "A width of 7" << '|'  << setw(7)

            << num2 << '|' << endl;

 

      return 0;

}    

 

A width of 7|   1234|

A width of 7|1234   |

A width of 7|   1234|

Press any key to continue

 

-------------------------------------------------------------------------

 
/* File: precision.cpp

   Programmer: S. Imberman

   Description: Shows precision format for float numbers

   Date: 09/21/2008

*/

 

#include <iostream>

#include <iomanip>

using namespace std;

 

 

int

main()

{

 

      float num1;

     

      //default printout of float numbers.

      num1 = 123456;

      cout << "The value of num1 is: " << num1 << endl;

 

      num1 = 1234567;

     

      cout << "The value of num1 is: " << num1 << endl;

     

 

      //demonstration of setprecision

      cout <<"A precision of 3 " <<  setprecision(3) << num1 << endl;

      cout << "A precision of 2 " <<setprecision(2) << num1 << endl;

      cout << "No precision specified after precision 2 specified "

     << num1 << endl;

 

      return 0;

}

 

     

The value of num1 is: 123456

The value of num1 is: 1.23457e+006

A precision of 3 1.23e+006

A precision of 2 1.2e+006

No precision specified after precision 2 specified 1.2e+006

Press any key to continue

 

---------------------------------------------------------------------


/* File: showpoint.cpp

   Programmer: S. Imberman

   Description: Shows showpoint format for float numbers

   Date: 09/21/2008

*/

 

#include <iostream>

#include <iomanip>

using namespace std;

 

 

int

main()

{

      float num1;

     

      //default printout of float numbers.

      num1 = 123456;

      cout << "The value of num1 is: " << num1 << endl;

 

      num1 = 1234567;

     

      cout << "The value of num1 is: " << num1 << endl;

     

 

      //demonstration of format flag showpoint

 

      cout << "demo of showpoint" << endl;

      num1 = 6;

      cout << "Before showpoint"<< endl;

      cout  << num1 << endl;

      cout << showpoint;

      cout << "After showpoint" << endl;

      cout << num1 << endl;

      num1 = 24.5;

      cout << num1 << endl;

      num1 = 1234567;

      cout << num1 << endl;

 

      return 0;

}

 

 

The value of num1 is: 123456

The value of num1 is: 1.23457e+006

demo of showpoint

Before showpoint

6

After showpoint

6.00000

24.5000

1.23457e+006

Press any key to continue

 

------------------------------------------------------------------


/* File: fixed.cpp

   Programmer: S. Imberman

   Description: Shows fixed format for float numbers

   Date: 09/21/2008

*/

 

#include <iostream>

#include <iomanip>

using namespace std;

 

int

main()

{

      float num1;

     

      //default printout of float numbers.

      num1 = 123456;

      cout << "The value of num1 is: " << num1 << endl;

 

      num1 = 1234567;

      cout << "The value of num1 is: " << num1 << endl;

     

      cout << "demo of fixed" << endl;

 

      cout << fixed;

      cout << num1 << endl;

      num1 = 24.5;

      cout << num1 << endl;

      num1 = 6;

      cout << num1 << endl;

      num1 = 123456;

      cout << num1 << endl << endl;;

 

      cout << "Demo of fixed and setprecision" << endl;

     

      num1 = 23.123456;

      cout << "Before setprecision:  " << num1 << endl;

 

      cout << setprecision(3);

      cout << "After setprecision(3):  " << num1<< endl;

      return 0;

}

 

 

The value of num1 is: 123456

The value of num1 is: 1.23457e+006

demo of fixed

1234567.000000

24.500000

6.000000

123456.000000

 

Demo of fixed and setprecision

Before setprecision:  23.123455

After setprecision(3):  23.123

Press any key to continue