|
![]() |
|||
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
Following is the code listing for setf.cpp, from the CD-ROM. The code demonstrates some common uses of the setf() member function of the ostream classes (such as cout and the file streams) and shows some common setf() calls and their effects. // File: setf.cpp // Example for C++ How-To // This program demonstrates using the setf() and unsetf() member functions // with the iostreams classes. // 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 - right align text does not work // Watcom 10.5: Not Tried // Microsoft VC++ 5: Not Tried // GCC 2.7.2/Linux: Yes - right align text does not work // GCC/EGCS/Linux: Yes - right align text does not work #include <iostream.h> #include <iomanip.h> #ifdef __BORLANDC__ #pragma hdrstop #endif #include <string> // string includes code with some libraries // this prevents precomiled headers from // being used, and slower compilations. using namespace std ; int main () { double d1, d2; int i1, i2; string s1, s2; // 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 * 1000 ; i1 = d1 * 100 ; i2 = d2 * 1000 ; // setting text or number alignment s1 = This text is right aligned.; cout.setf( ios::right ); cout << endl << Width will be set to: << (s1.size() + 10) << endl << setw(s1.size() + 10 ) << s1 << setw(0) << . << endl ; // unset the right-alignment flag cout.unsetf( ios::right ) ; s2 = This text is left aligned.; cout << endl << setw(s2.size() + 10) << setfill(x) << s2 << endl << endl << setw(0) << setfill(0) << setw(10) << i2 << setw(0) << endl ; // Showing the base of a number on output. cout.setf( ios::showbase ) ; cout << endl << Using setf( ios::showbase ) for numeric output: << endl << i1 as base10: << i1 << Octal: << oct << i1 << dec << endl << i2 as base10: << i2 << Hex: << hex << i2 << dec << endl; // reset the flag cout.unsetf( ios::showbase ) ; // show positive sign cout.setf( ios::showpos ) ; cout << endl << Using setf( ios::showpos ) for numeric output: << endl << i1 as base10: << i1 << Octal: << oct << i1 << dec << endl << i2 as base10: << i2 << Hex: << hex << i2 << dec << endl; cout.unsetf( ios::showpos ) ; // using scientific notation for floating point output cout.setf( ios::scientific ) ; cout << endl << Using setf( ios::scientific ) to produce scientific << endl << notation floating point output. << endl << d1 in scientific notation: << d1 << endl << d2 in scientific notation: << d2 << endl; cout.unsetf( ios::scientific ) ; // 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. Youll have to experiment. cout << endl << Press enter key to continue ... << endl; int c1; while( cin && (c1 = cin.get()) != \n ) ; return 0; } // end of file Steps In order to use I/O manipulators, include iomanip.h in your source file or common header file. The individual steps depend on the intended format of output. Its important that you leave your output streams in the default state when your code is finished. This might not matter in the simple examples shown here, but it will come back to bite you if you have to work in conjunction with other programmers or use other peoples code. How It Works Stream manipulators take a stream reference and possibly some other arguments, perform processing or output into the stream, and then return the stream reference. This has the effect of allowing stream operations to be chained onto each other. In reality, the compiler sees this as a set of nested function calls, rather than a chain of events. Comments Probably the most important point is to remember to reset any manipulators you change. Code that comes after yours, either in your functions, library functions, or third-party code, might depend on the flags being set to the defaults. Short programs probably make an exception here, if resetting everything could make up a significant percentage of the lines in the program. 14.4 Make my own stream manipulators?Problem Often a specific series of manipulations to the input or output streams is required to be repeated in a program. While this can be accomplished using a regular function (by passing the stream by reference and performing the manipulations there), it is much more convenient to actually write a manipulator. This can be worked into the insertion or extraction operation with a minimum of fuss. Technique The technique is similar to overloading the stream insertion and extraction operatorsall manipulators take a stream reference as the first argument and return a stream reference. All operations normally done on streams are valid, and it is possible to add characters and other data to the stream as well as manipulate the stream properties themselves.
|
![]() |
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.
|