Debugging a simple program

For our first example, we'll use a simple program designed to read a text file and display it. Along the way, the program counts the number of words and tabulates the lengths of all words found - how many words are one character long, how many are exactly two characters long, and so on. The sample program, TWORD.BAS (\PBWin10\Samples\TWord\TWORD.BAS), contains a number of bugs; you will be using the PowerBASIC integrated debugger to find each of them.

Be sure to make copies of the TWORD.DAT data file; TWORD.BAS reads that file and makes specific errors because of the data. While another data file may work as well, it is possible that one or more of the bugs will not occur if you use a different data file.

Here is a listing of the TWORD.BAS program.

When you have loaded TWORD.BAS into the editor, click on the Debugger button on the toolbar, or select Run from the menu, then Compile and Debug.

At this point, the debugger will have scrolled the program and highlighted the line containing the definition of the variable MaxWordLen, since that will be the first line executed when the program begins to run. The highlight is called the execution bar and marks the line of code at the execution position. In other words, that line will be executed next.

To make the program run, click on the Run button in the toolbar or press F5. The program's output appears in the User screen, which allows you to see how the program would look if you weren't using the debugger. If the User screen is not visible you may have to select it by using the Windows Taskbar, ALT+TAB, or by re-sizing the PowerBASIC IDE to a smaller size and different location until the User screen is visible. TWORD prompts you for the name of the file to read. Enter TWORD.DAT and press ENTER. TWORD displays the first line of the file then locks up because of one of the bugs in the program. To regain control, click on the Stop button. You can choose Program Reset (or press the SHIFT+F5 hot-key) to quit running the flawed program. Clicking the Run button lets you restart the program.

Next See: Setting and using breakpoints

See Also

Debugging PB/Win Programs

How the integrated debugger works

The DEBUG Menu

The Integrated Development Environment

Debugger Settings