|
Page 4 of 6
More about iostreams
So far you have seen only the most
rudimentary aspect of the iostreams class. The output formatting
available with iostreams also includes features such as number
formatting in decimal, octal, and hexadecimal. Here’s another
example of the use of iostreams:
//: C02:Stream2.cpp
// More streams features
#include <iostream>
using namespace std;
int main() {
// Specifying formats with manipulators:
cout << "a number in decimal: "
<< dec << 15 << endl;
cout << "in octal: " << oct << 15 << endl;
cout << "in hex: " << hex << 15 << endl;
cout << "a floating-point number: "
<< 3.14159 << endl;
cout << "non-printing char (escape): "
<< char(27) << endl;
} ///:~
This example shows the iostreams class
printing numbers in decimal, octal, and hexadecimal using iostream
manipulators (which
don’t print anything, but change the state of the output
stream). The formatting of floating-point numbers is determined
automatically by the compiler. In addition, any character can be
sent to a stream object using a cast to a char (a char is a data type that
holds single characters). This cast looks like a function
call: char( ), along with the character’s ASCII
value. In the program above, the char(27) sends an
“escape” to cout.
Character array concatenation
An important feature of the C preprocessor
is character array concatenation. This feature is used in
some of the examples in this book. If two quoted character arrays
are adjacent, and no punctuation is between them, the compiler will
paste the character arrays together into a single character array.
This is particularly useful when code listings have width
restrictions:
//: C02:Concat.cpp
// Character array Concatenation
#include <iostream>
using namespace std;
int main() {
cout << "This is far too long to put on a "
"single line but it can be broken up with "
"no ill effects
as long as there is no "
"punctuation separating adjacent character "
"arrays.
";
} ///:~
At first, the code above can look like an
error because there’s no familiar semicolon at the end of
each line. Remember that C and C++ are free-form languages, and
although you’ll usually see a semicolon at the end of each
line, the actual requirement is for a semicolon at the end of each
statement, and it’s possible for a statement to continue over several
lines.
Reading input
The iostreams classes provide the ability
to read input. The object used for standard
input is cin (for
“console input”). cin normally expects input
from the console, but this input can be redirected from other
sources. An example of redirection is shown later in this
chapter.
The iostreams operator used with cin
is >>. This operator waits for the same kind of input
as its argument. For example, if you give it an integer argument,
it waits for an integer from the console. Here’s an
example:
//: C02:Numconv.cpp
// Converts decimal to octal and hex
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a decimal number: ";
cin >> number;
cout << "value in octal = 0"
<< oct << number << endl;
cout << "value in hex = 0x"
<< hex << number << endl;
} ///:~
This program converts a number typed in by
the user into octal and hexadecimal representations.
Calling other programs
While the typical way to use a program that
reads from standard input and writes to standard output is within a
Unix shell script or DOS batch file, any program can be called from
inside a C or C++ program using the Standard C
system( ) function, which is declared in the header file
<cstdlib>:
//: C02:CallHello.cpp
// Call another program
#include <cstdlib> // Declare "system()"
using namespace std;
int main() {
system("Hello");
} ///:~
To use the system( ) function,
you give it a character array that you would normally type at the
operating system command prompt. This can also include command-line
arguments, and the character array can be one that you fabricate at
run time (instead of just using a static character array as shown
above). The command executes and control returns to the
program.
This program shows you how easy it is to
use plain C library functions in C++; just include the header file
and call the function. This upward compatibility from C to C++ is a big advantage
if you are learning the language starting from a background in
C.
|