|

If
every program worked the first time you tried it,
that would be the complete development cycle: Write
the program, compile the source code, link the program,
and run it. Unfortunately, almost every program,
no matter how trivial, can and will have errors,
or bugs, in the program. Some bugs will cause the
compile to fail, some will cause the link to fail,
and some will only show up when you run the program.
Whatever
type of bug you find, you must fix it, and that
involves editing your source code, recompiling and
relinking, and then rerunning the program. This
cycle is represented in Figure 1.1, which diagrams
the steps in the development cycle.

Figure 1.1. The steps in the development
of a C++ program.
HELLO.CPPYour
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 first program directly into your editor, exactly
as shown. Once you are certain it is correct, save
the file, compile it, link it, and run it. It will
print 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 will be covered
over the next couple of chapters.
WARNING: The following listing contains
line numbers on the left. These numbers are for
reference within the book. They should not be typed
in to your editor. For example, in line 1 of Listing
1.1, you should enter:
#include
<iostream.h>
Listing
1.1. HELLO.CPP, the Hello World program.
1:
#include <iostream.h>
2:
3:
int main()
4:
{
5:
cout << "Hello World!\n";
6:
return 0;
7:
}
Make
certain you enter this exactly as shown. Pay careful
attention to the punctuation. The << in line
5 is the redirection symbol, produced on most keyboards
by holding the Shift key and pressing the comma
key twice. Line 5 ends with a semicolon; don’t leave
this off!
Also
check to make sure you are following your compiler
directions properly. Most compilers will link automatically,
but check your documentation. If you get errors,
look over your code carefully and determine how
it is different from the above. If you see an error
on line 1, such as cannot find file iostream.h,
check your compiler documentation for directions
on setting up your include path or environment variables.
If you receive an error that there is no prototype
for main, add the line int main(); just before line
3. You will need to add this line before the beginning
of the main function in every program in this book.
Most compilers don’t require this, but a few do.
Your
finished program will look like this:
1:
#include <iostream.h>
2:int
main();
3:
4:
int main()
5:
{
6:
cout <<"Hello World!\n";
7:
return 0;
8:
}
Try
running HELLO.EXE; it should write
Hello
World!
directly
to your screen. If so, congratulations! You’ve just
entered, compiled, and run your first C++ program.
It may not look like much, but almost every professional
C++ programmer started out with this exact program.
|