Sams Teach Yourself C++ in 21 Days

(singke) #1

  1. Click OK. You are taken back to the main editor window.

  2. Choose File, New from the menus.

  3. Choose C++ Source File and give it a name, such as hello. You enter this name
    into the File Name text box.

  4. Click OK. You are taken back to the main editor window.

  5. Enter the code as indicated previously.

  6. Choose Build, Build hello.exefrom the menus.

  7. Check that you have no build errors. You can find this information near the bottom
    of the editor.

  8. Run the program by pressing Ctrl+F5 or by selecting Build, Execute hello from the
    menus.

  9. Press the spacebar to end the program.


20 Day 1


FAQ
I can run the program but it flashes by so quickly I can’t read it. What is wrong?
Answer:Check your compiler documentation; there ought to be a way to cause your pro-
gram to pause after execution. With the Microsoft compilers, the trick is to use Ctrl+F5.
With any compiler, you can also add the following lines immediately before the return
statement (that is, between lines 5 and 6 in Listing 1.1):
char response;
std::cin >> response;
This causes the program to pause, waiting for you to enter a value. To end the program,
type any letter or number (for example, 1) and then press Enter (if necessary).
The meaning of std::cinand std::coutwill be discussed in coming days; for now, just
use it as if it were a magical incantation.

Compile Errors ......................................................................................................


Compile-time errors can occur for any number of reasons. Usually, they are a result of a
typo or other inadvertent minor error. Good compilers not only tell you what you did
wrong, they point you to the exact place in your code where you made the mistake. The
great ones even suggest a remedy!
You can see this by intentionally putting an error into your program. If HELLO.cppran
smoothly, edit it now and remove the closing brace on line 7 of Listing 1.1. Your pro-
gram now looks like Listing 1.2.
Free download pdf