Sams Teach Yourself C++ in 21 Days

(singke) #1
Getting Started 17

1


HELLO.cpp—Your First C++ Program ..................................................................


Traditional programming books begin by writing the words “Hello World” to the screen,
or a variation on that statement. This time-honored tradition is carried on here.
Type the source code shown in Listing 1.1 directly into your editor, exactly as shown
(excluding the line numbering). After you are certain you have entered it correctly, save
the file, compile it, link it, and run it. If everything was done correctly, it prints the words
Hello World to your screen. Don’t worry too much about how it works; this is really just
to get you comfortable with the development cycle. Every aspect of this program is cov-
ered over the next couple of days.

The following listing contains line numbers on the left. These numbers are
for reference within the book. They should not be typed into your editor.
For example, on line 1 of Listing 1.1, you should enter:
#include <iostream>

CAUTION


LISTING1.1 HELLO.cpp, the Hello World Program


1: #include <iostream>
2:
3: int main()
4: {
5: std::cout << “Hello World!\n”;
6: return 0;
7: }

Be certain you enter this exactly as shown. Pay careful attention to the punctuation. The
<<on line 5 is the redirection symbol, produced on most keyboards by holding the Shift
key and pressing the comma key twice. Between the letters stdand couton line 5 are
two colons (:). Lines 5 and 6 each end with semicolon (;).
Also check to ensure you are following your compiler directions properly. Most compil-
ers link automatically, but check your documentation to see whether you need to provide
a special option or execute a command to cause the link to occur.
If you receive errors, look over your code carefully and determine how it is different
from the preceding listing. If you see an error on line 1, such as cannot find file
iostream, you might need to check your compiler documentation for directions on set-
ting up your include path or environment variables.
Free download pdf