Sams Teach Yourself C++ in 21 Days

(singke) #1
If you receive an error that there is no prototype for main, add the line int main();just
before line 3 (this is one of those pesky compiler variations). In that case, you need to
add this line before the beginning of the mainfunction in every program in this book.
Most compilers don’t require this, but a few do. If yours does, your finished program
needs to look like this:
#include <iostream>
int main(); // most compilers don’t need this line
int main()
{
std::cout <<”Hello World!\n”;
return 0;
}

18 Day 1


It is difficult to read a program to yourself if you don’t know how to pro-
nounce the special characters and keywords. You read the first line “Pound
include (some say hash-include, others say sharp-include) eye-oh-stream.”
You read the fifth line “ess-tee-dee-see-out Hello World.”

NOTE

On a Windows system, try running HELLO.exe(or whatever the name of an executable is
on your operating system; for instance, on a Unix system, you run HELLO, because exe-
cutable programs do not have extensions in Unix). The program should write
Hello World!
directly to your screen. If so, congratulations! You’ve just entered, compiled, and run
your first C++ program. It might not look like much, but almost every professional C++
programmer started out with this exact program.
Some programmers using IDEs (such as Visual Studio or Borland C++ Builder) will find
that running the program flashes up a window that promptly disappears with no chance
to see what result the program produces. If this happens, add these lines to your source
code just prior to the “return” statement:
char response;
std::cin >> response;
These lines cause the program to pause until you type a character (you might also need
to press the Enter key). They ensure you have a chance to see the results of your test run.
If you need to do this for hello.cpp, you will probably need to do it for most of the pro-
grams in this book.
Free download pdf