putback

precision

#include <iostream>streamsize precision() const;streamsize precision(streamsize p);

The precision( ) function is a member of ios (inherited from ios_base).

By default, six digits of precision are displayed when floating-point values are output. However, using the second form of precision( ), you can set this number to the value specified in p. The original value is returned.

The first version of precision( ) returns the current value.

Related functions are width( ) and fill( ).

Programming Tip 

There are two flavors of manipulators: those without parameters and those with parameters. While the creation of parameterized manipulators is beyond the scope of this book, it is quite easy to create your own parameterless manipulators.

All parameterless output manipulators have this skeleton:

ostream & manip-name(ostream & stream)

{

// your code here

return stream;

}

Here, manip-nameis the name of the manipulator. Notice that a reference to a stream of type ostream is returned. This is necessary if a manipulator is to be used as part of a larger I/O expression. It is important to understand that even though the manipulator has as its single argument a reference to the stream upon which it is operating, no argument is used when the manipulator is inserted in an output operation.

All parameterless input manipulators have this skeleton:

istream & manip-name(istream & stream)

{

// your code here

return stream;

}

An input manipulator receives a reference to the stream for which it was invoked. This stream must be returned by the manipulator. Here is an example of a simple output manipulator called setup( ). It turns on left-justification, sets the field width to 10, and specifies the dollar sign as the fill character.

#include <iostream> #include <iomanip> using namespace std; ostream &setup(ostream &stream) {   stream.setf(ios::left);   stream << setw(10) << setfill('$');   return stream; } int main() {   cout << 10 << " " << setup << 10;   return 0; } 

Remember: Your manipulator must return stream. If it doesn’t, the manipulator cannot be used in a series of input or output operations.




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net