Here are three example programs to debug;
Example 1 should be very easy to fix. Examples 2 and 3 contain compile time errors and runtime errors. Runtime errors are very difficult to track down so we shall show how to use the debugging capabilities of your IDE to help you out.
First, create a new project, copy and paste example code 2 as the main file. If you press to run the main project you shall see errors pop up in the output window at the bottom, you may need to enable the error list window from the menu to see them more clearly. Unfortunately error messages can be criptic, compiler specific and often misleading, if they occur at the top of your program can cause errors further down. To combat this it is best to try to tackle each error one by one recompiling at each stage until they have all gone.
Errors for this program are shown above. We shall look at the first error which is at line 21 (turn on line numbers in the view menu if they are not on by default). It says that `i' was not declared in this scope. So it should be a simple error to fix, just declare as an integer either inside the for loop or before. Run the program again, the output is now shown below:
We still have errors at line 21. The error message tells us that it is something to do with the
;) at the end of the
for declaration. If we check against the syntax from our notes we can see that we don't need the extra
;. Recompile and run again shows the following:
Now we have some cryptic messages. The first alludes to an operator not having the correct inputs. This actually refers to the fact we have written
<rather than a
<<. You may also notice that we have forgotten to close the brace on the
forloop, and not put
;at the end of line 25. This is changed in the next picture:
If we run now we should not get any compile errors but the answer is still not what we would expect. There is still one more change to make...
Create a new project, copy and paste example code 3 as the main file. If you press to run the main project you shall see that the program compiles but the result is not the correct one (the answer is pi!). We shall use the debugging tools of the IDE to try to track down the errors.
The main aim of the debugging environment in an IDE is the ability to:
From our point of view the first on the list is the most important. There is clearly a problem with values updating and behaving as we would expect. Often the compiler will assume hidden meanings from the code that we would not expect.
Now the first thing we need to do to debug programs is to insert break points. To do this all we need to do is make sure we are in debug configuration, and that we have line numbers showing, now click on the line number (as in on the actual number in the margin) we wish the code to stop at. I have chosen to stop at lines 25 and 33. Now click on the icon next to the run icon, with a small green triangle and a breakpoint. The program should stop at the first breakpoint. We can see below that the code has stopped at line 25, the green arrow shows line 26 is next.
I have highlighted the fact that the variable
dx=0, when we should expect that the value would be
In fact, on line 22, by dividing the integer 1 by the integer
n, the compiler has assumed integer division
and return an integer from the calculation. Since
0.04 is not an integer the compiler knock off the decimal places and returns 0.
This is called implicit casting, and it can be countered by explicitly stating in the code that 1 should be a double (by putting a point after it) and also that
n should be a double by casting it as one with the command
After pressing the green pause button, we will continue onto the next break point at line 33. Here we see that
Now stop the program and fix the problem using explicit casting. Take out the break points and we shall see that the program is still not
yet working, even though
dx is showing up with the correct value (below).
So now put the break point back in at line 33, and keep pressing the pause button, we need to find out why the value of
sum is still 0.
Try to keep track of what is happening to the value of sum. Why is the value not updating in the way it should? Try to test it against values that you can calculate by hand.
Once you have worked out what is happening you should start to see the value of sum increasing...
There are still a couple of subtle errors to find in the code. In order to test whether you have a working code see if you can calculate the value of pi to within 5 decimal places by increasing
n. Using 25 steps you should see something like: