CSC 306 Introduction to Programming with C++
1) Editing and Running C++ Programs and
2) Creating an Error Log
Chapter 1
Objectives
- Begin to use the Dev-C++ Package for the modification and use of C++
programs.
- Edit, compile, link, and run C++ programs.
- Learn to interpret error messages from the compiler in order to debug a
computer program.
- Learn the rules for naming identifiers in C++.
- Learn to keep a Dev-C++ error log.
Important Definitions
- On coding: source code, compiler, object code, interpreter, linker,
bug, debugger, I/O, project, directive.
- On errors: syntax error, runtime error, logic error, error message, and
warning.
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:
-
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. |
|
-
To start Dev-C++, go to "All Programs" under your "Start Menu", and select
"Bloodshed Dev-C++" followed by "Dev-C++".
-
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.)
-
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.
-
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:
-
The first line '
#include <iostream>' is a compiler
directive that tells the linker to include the iostream
library for I/O.
-
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.
-
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.
-
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.
-
The matching curly brackets on the fifth and eigth lines, { }, indicate
where the main function starts and ends, respectively.
-
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.)
-
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".
-
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.
-
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.
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.
-
A syntax error is an error in the grammar, structure, or order of the
elements in the source code.
These are generally detected by the compiler and flagged with an error
message that appears in the lower frame of Dev-C++.
Following each detected error, the compiler will generally try to guess
1) where the error is, 2) what was actually intended and 3) then proceeed.
Guesswork by a compiler is very bad, so it is generally a good idea to correct
the first error that the compiler finds; subsequent errors may really
be the result of an incorrect guess.
The Dev-C++ compiler puts both the error message and explanation all on one
line, so you will usually need to scroll over to read the entire error message.
When you double-click on an error message, the compiler will mark where it
thinks (guesswork again) the error is with an arrow
.
Note that sometimes the compiler will generate a warning, which usually
indicates that something in the source code is odd, but not illegal.
For the moment, it is a good idea to treat these exactly like error messages
because they are usually due to an error!
-
Run-time errors are generally not detected by the compiler, but are
only detected when the user attempts to run the program.
One standard example of a run-time error is where the source code has an
instruction to divide the variable
top by bottom.
The syntax may be correct, but the program will terminate early (commonly
called crash) if bottom has the value of zero.
Usually run-time errors will cause the running program to crash, but they
sometimes just generate warnings during the program execution.
-
Logic errors are the most subtle, where the program is coded correctly
and does not crash but do not perform the right operation to produce the
expected results.
Errors of this kind cannot be found automatically by the compiler or easy to
determine by the program's output.
They are usually due to errors in the design or how the algorithm was
translated into the source code (a process commonly called encoding).
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:
- Make the change and compile the program. If possible, try to run the
changed program.
- From this web page, copy the number and description of the change you were
asked to make into your Word error log file.
- 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.
- 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.
-
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?
- 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
- Omit the ">" in the first line.
- Omit the "<" in the first line.
- Omit the "#" in the first line.
- Replace the "int main( )" line with "void main( )".
- Replace the "int main( )" line with "int main( void )".
- Replace the "int main( )" line with "int main".
- Deliberately misspell "main".
- Deliberately misspell "using".
- Deliberately misspell "namespace".
- Deliberately misspell "cout".
- In the variable declaration line, deliberately misspell "total_peas" as
"total_pees".
- In the variable declaration line, replace "total_peas" by
"4total_peas".
- Add a new variable by changing the variable declaration line to "int m,
number_of_pods, peas_per_pod, total_peas;"
- In the assignment statement line, deliberately misspell "total_peas" as
"total_pees".
- Change the "*" in the assignment statement to a "+".
- Delete the entire "cin >> peas_per_pod;" line.
- Omit the initial "{".
- Omit the final "}".
- Omit one of the ";" that end a line.
- Omit the initial double quote symbol on some cout line.
- Omit the final double quote symbol on some cout line.
- Omit one of the "<<".
- Replace one of the ">>" with "<<".
- Replace one of the "\n" with "/n".
- Replace one of the "\n" with "\\".
- Replace one of the "\n" with "\a".
- Remove the very last "\n" from your program.
- Return 1 rather than 0.
- Return "q" (including the double quotes) rather than 0.
- 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