I've added some advice.
The #include library are
These all place their declarations into namesapce std and so you will need
using namespace std;or
using std::whatever(whatever is an object/function defined below) to use them as shown below.
The following sections give a rough picture of what is
defined in these above libraries.
iostream
For NUMERIC=long|int|unsigned|long unsigned,
istream
Class of input streams
Read ahead
In C++ you only find out that data does not exist by attempting to read it
and failing. So you need to (1) get same data, and (2) test it. But the
following
while( ! cin.eof() ) { cin>>data; consume the data }
does not work because it (1) tests the stream and then (2) gets the data.
At the end of the stream the loop attempts to get the nonexistent data, fails,
processes the previous data (!), and then tries again. Not what we want!
The easiest way to read to EOF is to attempt to input characters and test for success at the same time:
while( cin >> character ){ consume the character }
This works because when the input fails,
the stream returns a fals bool value(it is (void*)0).
More complex input usually needs a technique know as "reading ahead":
stream >> data; // read ahead
while(stream) // repeat until end or failure
{
...//consume data
stream >> data; // replace data by next item
}
. . . . . . . . . ( end of section iostream) <<Contents | End>>
ios
This is a class that is full of special constants used to
control formatting. See the next section.
Functions that change formatting
Note: Some functions are also manipulators with different names. For example
is like
and
is like
Flags Meanings
-----------------------------------------------------------
ios::showpoint show point in whole numbers
ios::showbase Indicate Base(0,0x,...)
ios::showpos Show +sign on positive
ios::uppercase EXABCDEF not exabcdef
-----------------------------------------------------------
Setting one bit in a group of bits actually means turning off the group and then turning one the correct bit on. There is a special version of the 'setf' function that does this:
selection ios::ingroup Meaning
-----------------------------------------------------------
ios::left ios::adjustfield left alignment
ios::right ios::adjustfield right alignment
ios::internal ios::adjustfield sign on left, digits right
ios::dec ios::basefield decimals
ios::hex ios::basefield hexadecimal
ios::oct ios::basefield Octal
ios::fixed ios::floatfield fixed form of floating point
ios::scientific ios::floatfield force exponential format
-----------------------------------------------------------
However this 'setf' has no matching 'setiosflags' manipulator.
[ formats.cpp ]
List of Format Flags in Library
ios::dec // use decimals
ios::fixed // no exponentials
ios::hex // use hexadecimals
ios::internal // sign on left, digits on right
ios::left // left justified
ios::oct // use octal
ios::right // right justified
ios::scientific // scientific notation
ios::showbase // Show base (hex,octal,...)
ios::showpoint // Always show a point
ios::showpos // If positive still show sign
ios::skipws // Skip whitespace on input
ios::stdio // ??
ios::unitbuf // ??
ios::uppercase // Letters in hex are UPPERCASE
ios::adjustfield = ios::left | ios::right | ios::internal.
ios::floatfield = ios::fixed | ios::scientific
ios::basefield = ios::dec | ios::hex | ios::octal
To turn on one bit in s group you can use ostream . setf(ios::bit, ios::ingroup); or ostream << resetiosflags(ios::ingroup)<<setiosflags(ios::bit)...
They duplicate the member functions of stream like setf and fill.
Note: formatflags is part of namespace ios
ios::formatflags
To turn on one bit in s group you can use ostream << resetiosflags(ios::ingroup)<<setiosflags(ios::bit)...
The setw(W) manipulator is unusual in two ways. Firstly it is the only one above that effects the next transfer only. Secondly, it is used for both istreams and ostreams.
[ giomanip.cpp ]
. . . . . . . . . ( end of section iomanip) <<Contents | End>>
Do-it-yourself Manipulators
Simple manipulators without arguments are very easy to define
and can make many programs easier to both read and write.
A function that takes a reference to an istream and returns
one as well can be used as an input manipulator. Similarly the
name of a function that is given a ref to an ostream and returns
a ref to an ostream is treated as an ostream manipulator.
// Output manipulator example - Part 2
ostream& dollars(ostream& out) //set check protected monetary formatting
{
return out << '$' << setfill('*') <<setw(10)
<< setprecision(2) << setiosflags(ios::fixed | ios:: showpoint);
}
...
cout << dollars << example << endl;
cout << left << dollars << example << endl;
cout << right << dollars << example << endl;
Setting up manipulators that have arguments is more complicated because functions that have arbitrary arguments are not recognised as iomanipulators by '<<' and '>>'. See see << and see >> above.
The technique is to define a class of objects with a constructor that has the arguments that you want that returns a function with the correct argument that has access to values of the arguments as data members of the class. Believe it or not!
The following is straight from the library....
fstreambase is used as a base for ifstream, ofstream and fstream. These are effectively the result of combining the the possibilities of handling files (fstreambase) with the matching stream class(istream, ostream, iostream respectively).
Constructors
Constructors
void open(const char *name, int mode=ios::in, int prot=0664);
Constructors
void open(const char *name, int mode=ios::out, int prot=0664) ;
Constructors