![]() |
|
|
Debugging your Code
There are tens of debugging routines that can be used in C++, however, this section will only touch on the most easily accessible and the most useful ones that you might find yourself using. Imagine you've just completed a project that you've been working on for a long time. You happily click on the Start button and run your application, however, the program doesn't run as expected. Calculations give wrong results and other options don't work. What would you do? Who you gonna call? Debug Mode, of course! Debug Mode is essentially a mode that takes you through your code step by step. Therefore, you know what exactly happens to each of your variables and procedures. There are many options that VC++ gives you when debugging your code, however, this lesson will only show you the main features of this awesome and powerful feature in VC++. There are three main types of errors that you can run into as a programmer: Syntax Errors, Run-Time Errors and Logic Errors. Syntax Errors happen when you mistype a command, for example, or write an incomplete structure. VB detects some of these Syntax Errors as you write the code. Run-Time Errors occur when VC++ tries carrying out an operation that is impossible, like dividing by zero or calling a non-existent file. The final type, Logical Errors, is a completely different story. Everything is compiled properly, with no messages from VC++, however, the code doesn't work as expected. This is because of flaws in the "logic" used in coding. The only way to fix this type of error is by testing and analyzing your code. Breakpoints: This is something that you'll want to use a lot. Breakpoints are markers placed on lines in your code that cause your C++ compiler to pause as soon as it reaches lines with breakpoints. This gives you the option to look inside your code and check everything while the application is still active (To check values of variables, hover your mouse cursor over the desired variable). You can then run your program step by step and see exactly how every line executes. To set a breakpoint, make sure you're in code view, click on the line where you want C++ to break and press F9. To remove it press F9 again. You should see a dark red dot on the left of the line. When you run your program and C++ breaks on that line, use the Debug toolbar to step into, out of or over lines of code. Many other options are also available on the Debug toolbar:
The table should answer most of your questions. Basically to enter break mode and debug your code: 1. Set breakpoints. 2. Click on the Go button (F5). 3. When the program enters debug mode, use the buttons Step into, Step over and Step out to work your way through the code. 4. When you're done, click on the Stop Debug button. If the Debug Tool Bar is not visible, right click on any blank space next to a toolbar and choose Debug from the popup menu. |
||||||||||||||||||||||||||||||||||||||||||||||||||||