Programming Errors

Programming errors can be categorized into three types: syntax errors, runtime errors, and logic errors.

Programming errors are unavoidable, even for experienced programmers. Errors can be cat egorized into three types: syntax errors, runtime errors, and logic errors.

1. Syntax Errors

Errors that are detected by the compiler are called syntax errors or compile errors. Syntax errors result from errors in code construction, such as mistyping a keyword, omitting neces-sary punctuation, or using an opening brace without a corresponding closing brace. These errors are usually easy to detect, because the compiler tells you where they are and what caused them. For example, the following program in Listing 1.4 has a syntax error.

Listing 1.4 ShowSyntaxErrors.cpp

1 #include <iostream>

2 using namespace std

3

4 int main()

5 {

6     cout << “Programming is fun << endl;

7

8     return 0;

9 }

When you compile this program using Visual C++, it displays the following errors:

Three errors are reported, but the program actually has two errors. First, the semicolon (;) is missing at the end of line 2. Second, the string Programming is fun should be closed with a closing quotation mark in line 6.

Since a single error will often display many lines of compile errors, it is a good practice to fix errors from the top line and work downward. Fixing errors that occur earlier in the program may also fix additional errors that occur later.

2. Runtime Errors

Runtime errors cause a program to terminate abnormally. They occur while an application is running if the environment detects an operation that is impossible to carry out. Input mistakes typically cause runtime errors. An input error occurs when the program is waiting for the user to enter a value, but the user enters a value that the program cannot handle. For instance, if the program expects to read in a number, but instead the user enters a string, this causes data-type errors to occur.

Another common source of runtime errors is division by zero. This happens when the divisor is zero for integer divisions. For instance, the following program in Listing 1.5 would cause a runtime error.

Listing 1.5  ShowRuntimeErrors.cpp

1 #include <iostream>

2 using namespace std;

3

4 int main()

5 {

6    int i = 4;

7    int j = 0;

8    cout << i / j << endl;

9

10   return 0;

11 }

Here, i and j are called variables. We will introduce variables in Chapter 2. i has a value of 4 and j has a value of 0. i / j in line 8 causes a runtime error of division by zero.

3. Logic Errors

Logic errors occur when a program does not perform the way it was intended. Errors of this kind occur for many different reasons. For example, suppose you wrote the following pro­gram in Listing 1.6 to convert a Celsius 35 degree to a Fahrenheit degree:

Listing 1.6  ShowLogicErrors.cpp

1 #include <iostream>

2 using namespace std;

3

4 int main()

5 {

6    cout << “Celsius 35 is Fahrenheit degree ” << endl;

7    cout << (9 / 5) * 35 + 32 << endl;

8

9    return 0;

10 }

You will get Fahrenheit 67 degree, which is wrong. It should be 95. In C++, the division for integers is the quotient. The fractional part is truncated. So 9 / 5 is 1. To get the correct result, you need to use 9.0 / 5, which results in 1.8.

In general, syntax errors are easy to find and easy to correct, because the compiler indicates where the errors were introduced and why they are wrong. Runtime errors are not difficult to find, either, because the reasons and locations for the errors are displayed on the console when the program aborts. Finding logic errors, on the other hand, can be very challenging. In the upcoming chapters, you will learn the techniques of tracing programs and finding logic errors.

4. Common Errors

Missing a closing brace, missing a semicolon, missing quotation marks for strings, and mis­spelling names are common errors made by new programmers.

Common Error 1: Missing Braces

Braces are used to denote a block in the program. Each opening brace must be matched by a closing brace. A common error is missing the closing brace. To avoid this error, type a closing brace whenever an opening brace is typed, as shown in the following example.

int main()

{

} <—— Type this closing brace right away to match the opening brace

 

Common Error 2: Missing Semicolons

Each statement ends with a statement terminator (;). Often, a new programmer forgets to place a statement terminator for the last statement in a block, as shown in the following example:

int main()

{

cout << “Programming is fun!” << endl;

cout << “Fundamentals First” << endl;

cout << “Problem Driven” << endl

}                                 

Missing a semicolon

Common Error 3: Missing Quotation Marks

A string must be placed inside the quotation marks. Often, a new programmer forgets to place a quotation mark at the end of a string, as shown in the following example:

cout << “Problem Driven;

Missing a quotation mark

Common Error 4: Misspelling Names

C++ is case-sensitive. Misspelling names is a common error made by new programmers. For example, the word main is misspelled as Main in the following code:

1 int Main()

2 {

3    cout << (10.5 + 2 * 3) / (45 3.5) << endl;

4    return 0;

5 }

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *