CSC 306 Introduction to Programming with C++

1) Editing and Running C++ Programs and
2) Creating an Error Log

Chapter 1


Objectives

Important Definitions


Some Background and Terminology

To create a computer program, one needs a text editor to type what is called the source code of the program. The source code contains the sequence of steps the programmer designed for the program (the algorithm) written in a specific language. The computer cannot yet execute this code at this stage because it is written in a high-level language designed for human readability, and it must be translated into a low-level language for the computer to understand it. A program that translates an entire file of source code into executable object code is called a compiler.

The Dev-C++ compiler you will be using for this class allows you to write and compile separate portions of you program separately. This often simplifies a programming task because you can break a large program into smaller more manageable pieces, which you can focus on to make sure that component works. A linker is a program that combines the executable object codes from these modules together to form a single executable program. Dev-C++ is a powerful software-creation package that consists of a C++ editor, debugger, compiler, and linker that works with Microsoft Windows. (Windows does not come with a compiler, so the user is required to purchase one) The Linux operating system comes with a C compiler by default and users can download the Gnu C++ compiler for free, as it is part of the Open-Source initiative.

Most programs written in a high-level language must be compiled, but there are several exceptions. JavaScript is one example you have already encountered, and the two main languages used in artificial intelligence, Prolog and Lisp, are also not compiled. Programs written in these languages use an interpreter that analyzes and executes each line of source code in succession, unlike a compiler that considers the entire program first in its translation. One advantage of interpreters is that they can execute a program immediately, whereas compilers require more time before an executable program emerges. However, programs produced by compilers run much, much faster and can also execute by themselves. The same programs executed by an interpreter are slower because they must wait for the interpreter to analyze each line.

Finally, we must have some way to check what happens when our program fails to work correctly, due to what is called a bug. Some bugs are found by the compiler, but others require a utility program called a debugger to find and correct these mistakes. Debuggers enable programmers to stop running their program at any point and examine the contents of any of the variables to ensure that their values are what they are expected to be.

Getting Started:

  1. Create a new folder somewhere on your computer where you will store all the code you write from this course. Download the file FirstProgram.cpp by right clicking the link into this new folder.
    NOTE: the ".cpp" file extension is used traditionally for C++ source files (files that have source code in them) while ".c" is the extension used for C source code. Although Dev-C++ in particular uses ".cpp", the extension ".cxx" is also sometimes used to indicate a C++ source file, and this is used frequently in our main text.

  2. To start Dev-C++, go to "All Programs" under your "Start Menu", and select "Bloodshed Dev-C++" followed by "Dev-C++".

  3. Once the application opens, read and close the "Tip" window that appears each time you start the application. (These can be interesting and helpful at first, but feel free to close it when needed.)

  4. Either by pressing the "Ctrl" and "O" keys together or by clicking the "Open" option under the "File" menu, open the FirstProgram.cpp source file.

  5. This program needs to write output to the screen, so it needs to be linked to the standard C++ I/O library. I/O stands for Input/Output, and the I/O library has modules already created by someone else that do things like write output to the screen and capture input keystrokes from the keyboard. To link all the pieces together, Dev-C++ requires that the source file is a part of a project, which is basically a collection of all of the pieces of executable code. Open the "Execute" menu and select "Compile" (or press Ctrl-F9). A window will appear asking you if you want to "create a default project workspace?" Click the "Yes" button and a default project will be created in the same folder as the source file.

Examining the source code:

Before we run the program, let's take a line-by-line look at the source code:
  1. The first line '#include <iostream>' is a compiler directive that tells the linker to include the iostream library for I/O.

  2. The second line starts with 'using namespace std;, which is a using directive that directs the compiler to use the std (standard) namespace for the iostream library, where the names such as "cout" are defined.
    NOTE: In general, a C++ namespace is a set of uniquely identified names (i.e. there are no duplicate names in a set.) It is legal in C++ for two different things in different modules to have the same name, so to eliminate ambiguity after linking, name definitions are divided up into namespaces.

    The second part of this line is a comment, where the format is similar to that used in JavaScript.

  3. The third line is a blank line. The C++ compiler generally ignores blank lines and indentation, for details, refer to page 23 of the text.

  4. The fourth line 'int main( )' specifies the beginning of the code module (or function) called "main." The 'int' part indicates that an integer is returned when the main module completes, and the empty parentheses after the word main indicates that this module takes no parameters.

  5. The matching curly brackets on the fifth and eigth lines, { }, indicate where the main function starts and ends, respectively.

  6. The code 'cout << "Hello out there!\n" ;' on line six outputs the text "Hello out there!" and the character '\n' to the standard output device, which in this case is the screen. This '\n' character means "newline" and starts a new line of text on the screen much like what happens when pressing the return key. (Look at page 22 for some details of using the wrong slash mark when doing this.)

  7. The 'return 0;' on line seven indicates that the number zero is returned when the main function finishes. Recall that when the main function was defined on the fourth line, it indicated that an integer will be returned. This practice of returning integers from the main function may seem odd, but it is consistent with how other functions are defined and is also useful in detecting runtime errors.

Go under "Execute" and select the "Run" (or press Cntl-F10) option. You should see the output window with the text. Edit your code so that instead of typing "Hello out there!" your program outputs "Hello world!" Try executing it again, saying yes to having the files updated. Watch carefully to see Dev-C++ tell you that it is compiling and linking. The "Hello World" program is one that is a standard first program that is taught in programming classes, and it has gone through many incarnations as the language of choice has changed over time. Perhaps you can appreciate the odd fanaticism and humor surrounding this program looking at the websites http://www.gnu.org/fun/jokes/helloworld.html and http://www2.latech.edu/~acm/HelloWorld.shtml.


Running and Debugging a More Complicated Program

We will first open and compile a new program called "Display1_8.cpp".
  1. Close the project workspace for the FirstProgram.cpp program, so that multiple unrelated programs do not get compiled together. Select the "Close All" option under the "File" menu.

  2. Download the file Display1_8.cpp into your course code folder. This source file is in fact the program shown in "Display 1.8" on p.20 of your text, but right now that is not important.

  3. Create a new project and add the file Display1_8.cpp to it, and then compile and run it a few times, entering data when requested.
Note that this program has more interactivity than the FirstProgram.cpp you downloaded and compiled above. You will be making changes and seeing what happens in this assignment.

Fundamentals of Programming Errors (bugs)

According to folklore, the first computer "bug" was an actual moth discovered by Grace Hopper that was trapped between two electrical relays of the Mark II Aiken Relay Calculator and caused the machine to crash. A "bug" currently is a term used to describe a situation where a program, for a variety of reasons does not do what it is expected to do.

There are many kinds of programming errors or bugs.

Creating an Error Log

This assignment is designed for you to become more familiar with the error detection built into the Dev-C++ compiler. In particular, you will create a catalog of typical errors that may arise, and this document may hopefully help you in the future to identify the actual problem based upon the error message the compiler produces.

Before proceeding, execute the Display1_8.cpp program so that you can assure yourself that it runs correctly; you will be making changes to the source code to introduce bugs! Open a new Microsoft Word document or text document (using Notepad or Wordpad) and name your file YourLastName_306A2.doc or YourLastName306_A2.txt, respectively. This will be your error log file.


Directions

This assignment must be completed individually.

For each of the numbered changes below, do the following:

  1. Make the change and compile the program. If possible, try to run the changed program.
  2. From this web page, copy the number and description of the change you were asked to make into your Word error log file.
  3. Some of the listed changes may not generate any errors, but most will. Classify these changes as follows: If an error or warning occurs, classify the FIRST one identified as SYNTAX, RUN_TIME, or LOGIC. Otherwise, make your classification NONE for no errors. Remember that when you double-click the error, an arrow will appear indicating where the compiler first noted the error.
  4. Read the entire FIRST error message if one appears, scrolling over if necessary. Then copy the resultant error and/or warning message you got into your Word error log file and state the number of errors and warnings that were found by the compiler and/or linker.
  5. Very briefly explain the correspondence between the change that you made and the resulting error or warning and flagged message that occurred. In other words, did Dev-C++ identify the actual problem? If not, can you briefly explain why?
  6. Repair each change that you just made to restore the program to its original state. Hint: The undo command is often useful here. Thus, for each of the following, you will beginning with the original working code.

Changes

  1. Omit the ">" in the first line.
  2. Omit the "<" in the first line.
  3. Omit the "#" in the first line.
  4. Replace the "int main( )" line with "void main( )".
  5. Replace the "int main( )" line with "int main( void )".
  6. Replace the "int main( )" line with "int main".
  7. Deliberately misspell "main".
  8. Deliberately misspell "using".
  9. Deliberately misspell "namespace".
  10. Deliberately misspell "cout".
  11. In the variable declaration line, deliberately misspell "total_peas" as "total_pees".
  12. In the variable declaration line, replace "total_peas" by "4total_peas".
  13. Add a new variable by changing the variable declaration line to "int m, number_of_pods, peas_per_pod, total_peas;"
  14. In the assignment statement line, deliberately misspell "total_peas" as "total_pees".
  15. Change the "*" in the assignment statement to a "+".
  16. Delete the entire "cin >> peas_per_pod;" line.
  17. Omit the initial "{".
  18. Omit the final "}".
  19. Omit one of the ";" that end a line.
  20. Omit the initial double quote symbol on some cout line.
  21. Omit the final double quote symbol on some cout line.
  22. Omit one of the "<<".
  23. Replace one of the ">>" with "<<".
  24. Replace one of the "\n" with "/n".
  25. Replace one of the "\n" with "\\".
  26. Replace one of the "\n" with "\a".
  27. Remove the very last "\n" from your program.
  28. Return 1 rather than 0.
  29. Return "q" (including the double quotes) rather than 0.
  30. Return q (with no quotes) rather than 0.
Include the following header at the top of your error log:

  CSC306
  Assignment 2
  YourLastName

When are finished with your error log, submit it into the CSC306_A02 dropbox on the Academic server.


Back to Introduction to Computer Programming with C++ Homepage