|

In
unit 16, "Streams," you will see how to use cout
to print data to the screen. For now, you can use
cout without fully understanding how it works. To
print a value to the screen, write the word cout,
followed by the insertion operator (<<), which
you create by typing the less-than character (<)
twice. Even though this is two characters, C++ treats
it as one.
Follow
the insertion character with your data. Listing
2.2 illustrates how this is used. Type in the example
exactly as written, except substitute your own name
where you see Jesse Liberty (unless your name is
Jesse Liberty, in which case leave it just the way
it is; it’s perfect— but I’m still not splitting
royalties!).
Listing
2.2.Using cout.
1:
// Listing 2.2 using cout
2:
3:
#include <iostream.h>
4:
int main()
5:
{
6:
cout << "Hello there.\n";
7:
cout << "Here is 5: " << 5 <<
"\n";
8:
cout << "The manipulator endl writes a new
line to the screen." << endl;
9:
cout << "Here is a very big number:\t" <<
70000 << endl;
10:
cout << "Here is the sum of 8 and 5:\t" <<
8+5 << endl;
11:
cout << "Here’s a fraction:\t\t" <<
(float) 5/8 << endl;
12:
cout << "And a very very big number:\t" <<
(double) 7000 * 7000 << endl;
13:
cout << "Don’t forget to replace Jesse Liberty
with your name...\n";
14:
cout << "Jesse Liberty is a C++ programmer!\n";
15:
return 0;
16:
}
OUTPUT:
Hello
there.
Here
is 5: 5
The
manipulator endl writes a new line to the screen.
Here
is a very big number: 70000
Here
is the sum of 8 and 5: 13
Here’s
a fraction: 0.625
And
a very very big number: 4.9e+07
Don’t
forget to replace Jesse Liberty with your name...
Jesse
Liberty is a C++ programmer!
On
line 3, the statement #include
<iostream.h> causes the iostream.h
file to be added to your source code. This is required
if you use cout and its related functions.
On
line 6 is the simplest use of cout, printing a string
or series of characters. The symbol \n is a special
formatting character. It tells cout to print a newline
character to the screen.
Three
values are passed to cout on line 7, and each value
is separated by the insertion operator. The first
value is the string "Here is 5: ". Note the space
after the colon. The space is part of the string.
Next, the value 5 is passed to the insertion operator
and the newline character (always in double quotes
or single quotes). This causes the line
Here
is 5: 5
to
be printed to the screen. Because there is no newline
character after the first string, the next value
is printed immediately afterwards. This is called
concatenating the two values.
On
line 8, an informative message is printed, and then
the manipulator endl is used. The purpose of endl
is to write a new line to the screen.
On
line 9, a new formatting character, \t, is introduced.
This inserts a tab character and is used on lines
8-12 to line up the output. Line 9 shows that not
only integers, but long integers as well can be
printed. Line 10 demonstrates that cout will do
simple addition. The value of 8+5 is passed to cout,
but 13 is printed.
On
line 11, the value 5/8 is inserted into cout. The
term (float) tells cout that you want this value
evaluated as a decimal equivalent, and so a fraction
is printed. On line 12 the value 7000 * 7000 is
given to cout, and the term (double) is used to
tell cout that you want this to be printed using
scientific notation. All of this will be explained
in Unit 3, "Variables and Constants," when data
types are discussed.
On
line 14, you substituted your name, and the output
confirmed that you are indeed a C++ programmer.
It must be true, because the computer said so!
|