Table 11.5 Member functions of istream.
The constructor associates a streambuf (or its derivation) with the class
to provide an input stream.
cout < "February"; break;
cout < "March";
cout < "April";
cout < "May";
cout < "June";
cout < "July";
cout < "August";
cout < "Septemb
/ number of words in dictionary
/ letter of alphabet
/ greeting message
/ test if n is even:
n%2 = 0
/ test if c is a digit:
c >= '0' & c <= '9'
/ test if c is a letter:
c >= 'a' & c <= 'z' | c >= 'A' & c <
The preprocessor provides a small set of predefined identifiers which denote
useful information. The standard ones are summarized by Table 12.3. Most
implementations augment this list with many nonstandard predefined
Define plain macros for the following:
An infinite loop structure called forever.
Pascal style if-then-else statements.
Pascal style begin and end keywords.
Pascal style repeat-until loop.
Define parameterized macros for the following
The preprocessor provides three other, less-frequently-used directives. The
#line directive is used to change the current line number and file name. It
has the general form:
#line number file
where file is optional. For example,
The conditional compilation directives allow sections of code to be
selectively included for or excluded from compilation, depending on
programmer-specified conditions being satisfied. It is usually used as a
portability tool for t
enum Bool cfw_false, true;
void Primes (unsigned int n)
for (register num = 2; num <= n; +num) cfw_
isPrime = true;
for (register i = 2; i < num/2; +i)
if (num%i = 0) cfw_
isPrime = false;
Menu:Option *handy, *prev = 0;
int idx = 0;
/ set prev to point to before the insertion position:
for (handy = first; handy != 0 & idx+ != pos; handy = handy>Next()
prev = handy;
if (prev = 0) cfw_
/ empty list
option->Next() = first; / first entry
return Complex( real * c.real - imag * c.imag,
imag * c.real + real * c.imag);
void Complex:Print (void)
cout < real < " + i" < imag < '\n';
const int end = -1;
/ denotes the end of the list
A file can be textually included in another file using the #include directive.
For example, placing
inside a file f causes the contents of contents.h to be included in f in exactly
the position where the directive app
One of the common uses of #if is for temporarily omitting code. This is
often done during testing and debugging when the programmer is
experimenting with suspected areas of code. Although code may also be
omitted by commenting its out (i.e., placing /* an
#defone size 100
integer n = 0;
while (n < 10]
Error 1, Unknown directive: defone
Note 3, Return type of main assumed int
Error 5, unknown type: integer
Error 7, ) expected
When run, the program will pro
iof > name;
Table 11.9 summarizes the member functions of ofstream, istream, and
fstream (in addition to those inherited from their base classes).
Table 11.9 Member functions
A manipulator is an identifier that can be inserted into an output stream or
extracted from an input stream in order to produce a desired effect. For
example, endl is a commonly-used manipulator which inserts a newline into
an output s
long flags (void);
long flags (long);
The first version returns the format flags (this is a sequence of formatting
bits). The second version sets the formatting flags to a given value
(flags(0) restores default formats), and return the previous setting.
Table 11.7 summarizes the member functions of ios.
Table 11.7 Member functions of ios.
The constructor associates a streambuf (or its derivation) with the class.
void init (streambuf*);
Associates the specified st
File IO with fstreams
A program which performs IO with respect to an external file should include
the header file fstream.h. Because the classes defined in this file are derived
from iostream classes, fstream.h also includes iostream.h.
A file can be open
IO operations may result in IO errors, which can be checked for using a
number of ios member functions. For example, good returns nonzero if no
error has occurred:
/ all is ok.
where s is an iostream. Similarly, bad returns nonzero if an inva
cout < 10 < '\n';
will use exactly 5 character to display 10:
An object requiring more than the specified width will not be restricted to it.
Also, the specified width applies only to the next object to be output. By
default, spaces are
Table 11.10 Member functions of ostrstream, istrstream, and strstream.
ostrstream (char *buf, int size, int mode = ios:out);
The first version creates an ostrstream with a dynamically-allocated
buffer. The second version creates an ostr