7. Types of Problems
It is actually fairly rare that a program compiles and runs perfectly the first time. During the process of implementing a design, a number of things can go wrong. These problems generally fall into the categories discussed below.
Syntax Errors and Warnings
The rules of a programming language, its syntax, must be followed when a program is written. If a rule is broken, the compiler will catch the problem when it attempts to translate the program to machine language. If the problem is severe enough, the translation will fail and no object file, and therefore no executable file, will be created. This type of problem is called a syntax error. Common causes of syntax errors include forgetting a semicolon or missing one of a pair of quotes or braces.
Some problems identified by the compiler are not severe enough to cause translation to fail. In these cases, the problem is considered a syntax warning and both object and executable files will be created. An example of a statement which would generate a warning follows:
int number = 4.5;
A floating-point value cannot be assigned to an integer, so only the 4 will be assigned to the variable and a warning generated because of the loss of data.
It is important to realize that a problem resulting in a warning may be severe enough to cause the program to fail when it is executed. The source of a warning should always be fixed so that the program compiles cleanly (i.e., without any problems).
When a syntax error or warning is identified, the compiler will generate a message describing the identified problem and the line number on which it was located.
Linker Errors
After a source code file is successfully translated to machine language and an object file created, the linker will combine the object file with the items from the C++ Standard Library used by the program (review the What Does “Implement a Program” Mean? section). As part of this process, the functions written by the programmer are also checked. For example, if a function is named read_data(), but a function call uses readdata(), an error called a linker error will be generated (recall that C++ is case sensitive). When a linker error occurs, the executable file is not created.
Run-Time Errors
Once an executable file is created, the program can be executed, but problems can still appear. Sometimes a program will fail (i.e., end immediately) when it reaches a particular point during execution. This type of error is called a run-time error because it occurs when a program is running. There are a variety of reasons why a program will fail during execution. Attempting to divide by zero is one such reason. Warnings which were not addressed can also be the source of run-time errors.
Logic Errors
Sometimes a program can compile and run without failure and still work incorrectly. For example, perhaps a program should add two numbers but instead multiplies them. When the result of the calculation is displayed, the program’s output will be incorrect. Another example is a cin statement to read a number placed prior to the cout statement prompting the user to enter a value. The program will compile and run, but act oddly from the user’s perspective by reading a number before it even asks for one to be entered. These types of problems are called logic errors because the program is not logically correct. Errors of this type cannot be identified by the compiler or linker and are not reasons for the program to fail when it executes. To the computer, the program works fine. It is the humans that are confused.