Debugging is easily done by placing cerr statements into your code and doing a bit of a binary search for the problem.
But, if you want to go beyond that, you'll need help from a debugging tool called a debugger. Debuggers all have a couple of common features that I'll describe first.
First, you'll have to compile specially to prep for a debugging session. You just have to compile like normal but add the -g flag to your typical CPP command:
$ CPP -g mycode
Next comes the actual debugging session. There are three commands you'll need to find in your environment and master to debug at the most basic level:
The go-getters among you will also want to master setting breakpoints.
Step over will execute the next statement in its entirety and set you up to do the next statement.
Step into will execute the next statement, but if it involves a function call, it will dive into that function and set you up to do the first statement in there. This can be very disconcerting when you are thrown into a standard library function like operator>> or the like. Be careful with this one. But it is very useful to execute your own functions and check their code, too.
Finally, inspect will allow you to view the contents of a variable at that moment in the execution. This is very helpful to see what changes a variable has undergone and realize that the wrong thing has happened!
Setting a breakpoint on a line of your program will allow you to do the "run" command and still regain control when execution reaches the line you think has a problem. Then you can step over/into as necessary to track down the actual problem from there — having skipped over lots of your program you are sure isn't to blame.
There are two debuggers installed on our ares box:
These are described in more detail below.
DDD is the Data Display Debugger. It is a graphical front-end to the primary GNU debugger, gdb. It will give you a nice area to inspect your variables and a second area with the code displayed nicely and a third area to type raw gdb commands. But all of these commands can be entered also by menus or pop-up tool boxes.
Here's a fairly detailed tutorial from Dr. Dobb's Journal.
I also found a couple of videos on YouTube with the Google search "ddd tutorial".
gdb is the GNU DeBugger. It is a console-based debugger.
I found many fine tutorials with the Google search "gdb tutorial". Try to use ones from reputable .edu sites for best results.