Sams Teach Yourself C++ in 21 Days

(singke) #1
The Anatomy of a C++ Program 29

2


10: std::cout << “Here is a very big number:\t” << 70000;
11: std::cout << std::endl;
12: std::cout << “Here is the sum of 8 and 5:\t”;
13: std::cout << 8+5 << std::endl;
14: std::cout << “Here’s a fraction:\t\t”;
15: std::cout << (float) 5/8 << std::endl;
16: std::cout << “And a very very big number:\t”;
17: std::cout << (double) 7000 * 7000 << std::endl;
18: std::cout << “Don’t forget to replace Jesse Liberty “;
19: std::cout << “with your name...\n”;
20: std::cout << “Jesse Liberty is a C++ programmer!\n”;
21: return 0;
22: }

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+007
Don’t forget to replace Jesse Liberty with your name...
Jesse Liberty is a C++ programmer!

OUTPUT


LISTING2.2 continued


Some compilers have a bug that requires that you put parentheses around
the addition before passing it to cout. Thus, line 13 would change to
13: cout << (8+5) << std::endl;

CAUTION


On line 2, the statement #include <iostream>causes the iostreamfile to be
added to your source code. This is required if you use coutand its related
functions.
On line 5 is the simplest use of cout, printing a string or series of characters. The symbol
\nis a special formatting character. It tells coutto print a newline character to the
screen; it is pronounced “slash-n” or “new line.”
Three values are passed to couton line 6, 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 then
the newline character (always in double quotes or single quotes) is passed. This causes
the line
Here is 5: 5

ANALYSIS
Free download pdf