Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


will cause the value to be shown in a field exactly nine characters wide. The following file, setw.cpp, demonstrates application for the setw manipulator:

// File: setw.cpp
// Example for C++ How-To
// Example file 1 for using io manipulators
// Using setw to set output width
// Copyright 1998, Jan Walter
// NO WARRANTY. If this code breaks you get to keep both pieces.

// Compiler verificiation:
// Borland C++ 5.01A:                Yes
// DJGPP 2.01 w. GCC 2.81 :      Yes
// Watcom 10.5:                         Not Tried
// Microsoft VC++ 5:                    Not Tried
// GCC 2.7.2/Linux:                    Not tried
// GCC/EGCS/Linux:                    Not tried


#include <iostream.h>
#include <iomanip.h>

#ifdef __BORLANDC__
#pragma hdrstop
#endif


int main()
{
   // the names are not the best  -sorry

   double      d1, d2;
   int      i1, i2;


     // note that there are functions to do both of these in the math.h
   // library, but since this is so simple this is easier to do.
     d1 = 22.0 / 7.0 ; // this approximates pi
   d2 = d1 * d1 ;

     i1 = d1 * 100 ;
   i2 = d2 * 1000 ;

// Using setwidth()

   cout      << endl
	 << “Using the setw() example:” << endl
	 << endl
	 << “Numbers before setw():” << endl
	 << “double d1 = “ << d1 << ‘.’ << endl
	 << “double d2 = “ << d2 << ‘.’ << endl
	 << “int i1 =    “ << i1 << ‘.’ << endl
	 // note the spaces align things here.
	 << “int i2 =    “ << i2 << ‘.’ << endl
	 << endl
	 << “Note how the output looks after using setw(9):” << endl

	 // setw does not need to be called every time, it’s just
	 // this way for clarity.

	 << “double d1 = “ << setw(9) <<  d1 << ‘.’ << endl
	 << “double d2 = “ << setw(9) << d2 << ‘.’ << endl
	 << “int i1 =    “ << setw(9) << i1 << ‘.’ << endl
	 << “int i2 =    “ << setw(9) << i2 << ‘.’ << endl
	 << endl
       << “Note how the numbers by default are right-aligned, and text” << endl
       << “is left-aligned.” << endl;

// wait for keypress routine to prevent os windows from closing when the
// program has completed.

// non-dos (or Windows or OS/2) platforms use the ‘\r’ char
// for the enter key. Sorry, no Mac definition here. You’ll have to experiment.
   cout << endl << “Press enter key to continue ...” << endl;
   int c1;
   while( cin && (c1 = cin.get())  != ‘\n’ ) ;


   return 0;
}

// end of file

Setting the Whitespace Character with setfill()

The setfill() manipulator selects what the stream considers to be whitespace. By default, this is the space (“ “) character, and can be changed to suit the application’s needs. Check-printing routines would normally use the * character, for instance. This setting stays in effect until changed again by another call to setfill().

The following code (given that i2 has a value of 10250)

cout << “int i2 = “ << setw(11) << setfill(‘*’)<< i2 << ‘.’ << endl;

will produce output that looks like this:

int i2 =******10250.

Setting the Number of Decimal Points with setprecision()

The setprecision() modifier affects only floating-point numbers such as float and double types. It sets the number of digits of precision used when displaying the number, and stays in effect until changed again. Note that the behavior is as expected—the decimal point is not counted as a digit when the output stream calculates the precision.

For example, if the doubled1 had a value of 44.123456789, the code

cout     << setprecision(9)
	 << “double d1 = “ << setw(11) << setfill(‘*’) << d1 << ‘.’ << endl ;

would print like this on the screen:

double d1 = **44.1234568.

Whether the last number rounds up depends somewhat on your compiler’s iostreams implementation, and could possibly be affected by other precision-related rules because this is a double-precision floating-point number. The following file, precisn.cpp, demonstrates the uses of setfill and setprecision I/O manipulators:

// File: precisn.cpp
// Example for C++ How-To
// This file demonstrates the setprecision and setfill io manipulators.
// Copyright 1998, Jan Walter
// NO WARRANTY. If this code breaks you get to keep both pieces.

// Compiler verificiation:
// Borland C++ 5.01A:                Yes
// DJGPP 2.01 w. GCC 2.81 :      Yes
// Watcom 10.5:                         Not Tried
// Microsoft VC++ 5:                    Not Tried
// GCC 2.7.2/Linux:                    Not tried
// GCC/EGCS/Linux:                    Not tried

#include <iostream.h>
#include <iomanip.h>

#ifdef __BORLANDC__
#pragma hdrstop
#endif



int main()
{

   double      d1, d2;
   int      i1, i2;


     // note that there are functions to do both of these in the math.h
   // library, but since this is so simple this is easier to do.
     d1 = 22.0 / 7.0 ; // this approximates pi
   d2 = d1 * d1 ;

     i1 = d1 * 100 ;
   i2 = d2 * 1000 ;

// Using setprecision()

     cout      << endl
	     << “The setprecision() call affects all floating point” << endl
	 << “output after the call. The default precision is 6.” << endl
	 << “Also note that setprecision does not include the decimal” << endl
	 << “point in the precision.” << endl
	 << setprecision(9)
	 << “double d1 = “ << setw(11) << setfill(‘*’) << d1 << ‘.’ << endl
	 << “double d2 = “ << setw(11) << setfill(‘*’)<< d2 << ‘.’ << endl
	 << “int i1 =    “ << setw(11) << setfill(‘*’)<< i1 << ‘.’ << endl
	 << “int i2 =    “ << setw(11) << setfill(‘*’)<< i2 << ‘.’ << endl;


// wait for keypress routine to prevent os windows from closing when the
// program has completed.

// non-dos (or Windows or OS/2) platforms use the ‘\r’ char
// for the enter key. Sorry, no Mac definition here. You’ll have to experiment.
   cout << endl << “Press enter key to continue ...” << endl;
   int c1;
   while( cin && (c1 = cin.get())  != ‘\n’ ) ;


     return 0;
}
// end of file


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.