![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
Conversion Bases Conversion bases cause numerical output to be given in something other than decimal. Octal and hexadecimal output are most commonly used for debug messages and other computer- or system-related output. For incoming data, the manipulator tells its stream what type of numerical data to expect by default. Base prefixes, such as the 0x prefix, are normally used to determine the base of the number. On some platforms, such as some older UNIX compilers, prefixing the input number with something like a 0x will cause the stream to read only the first number (thats the 0), and leave the rest in the stream. For instance, the following code will read an octal number from the user and write it back to the screen: cout << endl << Enter an octal number: ; cin >> oct >> i2; cout << endl << The number was: << oct << i2 << Base 10: << dec << i2 << endl; Note that unlike the modifiers in the previous examples, these do not take any arguments. The following file, setbase.cpp, shows how to use some of the manipulators used to set the base of the number read: // File: setbase.cpp // Example for C++ How-To // This program demonstrates IO using base 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 ; // Setting the base for reading and writing numbers cout << endl << Using the base modifiers affects all integers inserted << endl << into the stream from that point onwards, like << endl << setprecision() does for floating point numbers. << endl << double d1 = << setw(11) << setfill(*) << d1 << . << endl << double d2 = << setw(11) << setfill(*)<< d2 << . << endl << int i1 = << setw(11) << setfill(*) << i1 << . << endl << int i1 as hexadecimal = << setw(11) << hex << i1 << . << endl << int i2 = << setw(11) << dec << i2 << . << endl; // demonstrate input - different compiler systems iostreams // classes handle this somewhat differently cout << endl << Enter an octal number: ; cin >> oct >> i2; cout << endl << The number was: << oct << i2 << Base 10: << dec << i2 << endl; cin.ignore(3, \n); // gets rid of any left over newlines in the input // stream // 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 Manipulations Using setf() Although the other manipulators are inline, that is, placed between << operators, the modifications done using setf() and unsetf() are performed using these member functions to the stream object. Good programming practice dictates that all flags should be unset after the output is completed because other routines, possibly in other peoples code, might assume the flags to be left as defaults. Alignment Although on some systems it is possible to set the alignment of text in a field defined with setw(), this is not guaranteed to work on all compilers. It is confirmed not to work with the GNU and DJGPP iostreams libraries, which are essentially the same. Alignment of numbers is what the alignment setting was intended for, and it is likely that most library authors will only support this. Borlands implementation is the notable exception to this case. The call to set right alignment (which is the default) is shown for the cout class: cout.setf( ios::right ) and cout.setf( ios::left ) to set numbers to left align in their fields, just as text is by default. Showing Positive Numbers with a + Sign By default, only negative numbers are prefixed with a on output to indicate they are negative numbers. If desired, this flag can be set to cause the system to mark positive numbers with a + also. To get the system to denote positive numbers, use a call like this: cout.setf( ios::showpos ) Showing Floating-Point Numbers in Scientific Notation Scientific notation as displayed on a computer screen is not exactly the way humans would write it on papermost systems lack superscript, for example. Instead, the significant digits are shown, and then usually a lowercase e, and a positive or negative exponent sign. This is particularly useful for showing numbers with most of their significant digits more than six places away from the decimal point by reducing the amount of space used when printing the numbers on the console. To set the output class to use scientific notation to output floating-point numbers, use the call cout.setf( ios::scientific ) ;
|
![]() |
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.
|