CS 1711 Lab 3 - October 6 and 8, 1998
Debugging decision and loop structures


The purpose of this lab is to work with if-statements and while-statements, the basic decision and repetition structures of many programming languages in addition to C++. To help with the design of decision and looping structures, many integrated development environments (IDEs) include a debugger which allows the programmer to step through their program one line at a time and inspect variables to see if they are changing the way they are expected to.

Note about Multiple Windows

The Turbo C++ IDE (and many others) do not manage multiple independent programs very well. The most reliable approach is to make sure that only one program (i.e. one source window) is open when you try to run the program. If you are having difficulty getting a program to compile and/or run, you might want to check that only one program is active. Use Alt-F3 to close any extra source windows.


The IDE debugger is operated by a collection of key strokes. It may be a good idea to keep a list of the following keys handy for the first few times you debug programs. You will be tested on these before leaving the lab! The `important' keys to remember are:
Each time this button is pressed the current line (shown by the blue cursor) is executed and the cursor is moved to the next line to be executed.
Sort of like F8, but we'll have to wait until later to explain this one. For now, use F8.
This causes the program to run from where ever the trace cursor currently is until the line that the typing cursor is on. Just before the line that the typing cursor is on gets executed, the program stops and you can use F8 to step through.
This resets the program.
Opens the `Evaluation Box'. Here, you may type in the name of any variable and you will see the contents of the variable. Press Esc to close it.
Opens the `Watch Window'. In the watch window, you can place variables that you are interested in watching while stepping through the program.
Toggles a break point on and off (turns the line red). When the program is run, just before any statement with a breakpoint is executed, the program is stopped and you may continue stepping through. This feature is very useful for skipping over sections of programs that you know work correctly.

Designing and Testing a Payroll Program

In this section, you will extend an existing program. The program will include the use of a new if-statement and will require the selection of appropriate test data to ensure the new modification works correctly.
  1. If you have any windows opened in the IDE, close them now by pressing Alt-F3 (or use the mouse to click on the upper left button of the window). Load the file named PAYROLL.CPP (this will be located in the same area as the previous two labs).
  2. Read through the program to get an understanding of what it does. This program contains a new construct that you have not seen yet known as a function. There is one function named `instructpay()' in this program; here is how it works. When the computer executes the instruction instructpay(); in the main function, it then jumps down to the statements of the function instructpay at the bottom of the program. Take a peek at how this function looks, we will be studying functions in the next two chapters of the text. It turns out that functions are nearly the most important thing learned in C++.
  3. Also notice the location of the two variables TAX_BRACKET and TAX. Both of these are special variables called constant (because of the word const in front of them). They are given a value when declared that you can never change. This is where you will usually put constant declarations. You must NEVER, NEVER, NEVER put regular variable declarations here, you will lose marks on your assignments.
  4. Modify the program so that it will also pay 50% overtime for all hours worked over 40 hours. The overtime should be added to the gross salary before taxes are evaluated.
  5. Save the program on your M: drive. The marker will ask to see it before you leave.
  6. Now pick some appropriate test data. What is appropriate? You must pick a collection of input data that tests every branch of the program. Select the appropriate data and run the program to complete the table:
    Test Hours Rate Overtime? Taxes? Gross Net
    1     No No    
    2     Yes No    
    3     No Yes    
    4     Yes Yes    

Testing Loops

In this section we investigate a collection of programs named LABLOOP1.CPP, ..., LABLOOP5.CPP. The program allows the user to enter a list of nonnegative integers then prints out the count (number of elements in the list), the sum, and the average of the integers. Each of the programs contain at least one logic error. Load each of the programs and determine the error. Before looking at the actual program, you should run the program a few times to determine what types of input cause the errors to appear. This will help you determine the type of error to look for. If required, you may use the debugger to step through the program and inspect the variables at each step to see what has gone wrong. Keep a list of the programs you ran and what errors you managed to fix.

Running Stand-Alone Executables

The Turbo C++ compiler always produces an executable file (.EXE) that can be run without the need for the compiler. On the network in the lab, the executable is always placed on the drive you connected to when the compiler was started (A, C, or M) in the directory \TC30. You are to open up a DOS window and run a copy of the payroll program.
  1. Select Start|Programs|Command prompt.
  2. Type the letter of the drive you connected to when Turbo C++ was started followed by a colon and press enter. For example M:.
  3. Type the command cd \TC30.
  4. Type the command dir and see if you can find the file PAYROLL.EXE.
  5. Type the command payroll to run the program.

End of Lab

Make sure you have reported to the lab assistant so that you receive credit for the lab.