Sams Teach Yourself C++ in 21 Days

(singke) #1
namespace, not some other namespace.” You say that to the compiler by putting the char-
acters stdfollowed by two colons before the cout. You’ll learn more about namespaces
in coming days.
Here’s how coutis used: Type the word cout, followed by the output redirection opera-
tor (<<). Whatever follows the output redirection operator is written to the console. If you
want a string of characters written, be certain to enclose them in double quotes (“), as
shown on line 5.

28 Day 2


You should note that the redirection operator is two “greater-than” signs
with no spaces between them.

NOTE

A text string is a series of printable characters.
The final two characters,\n, tell coutto put a new line after the words Hello World!
This special code is explained in detail when coutis discussed on Day 18, “Creating and
Using Namespaces.”
The main()function ends on line 7 with the closing brace.

A Brief Look at cout............................................................................................


On Day 17, you will see how to use coutto print data to the screen. For now, you can
use coutwithout 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 isJesse Liberty).

LISTING2.2 Using cout
1: // Listing 2.2 using std::cout
2: #include <iostream>
3: int main()
4: {
5: std::cout << “Hello there.\n”;
6: std::cout << “Here is 5: “ << 5 << “\n”;
7: std::cout << “The manipulator std::endl “;
8: std::cout << “writes a new line to the screen.”;
9: std::cout << std::endl;
Free download pdf