CS 1711 Lab 4 - October 19 and 21, 1999
Debugging decision and loop structures

Introduction

The purpose of this lab is to work with if-statements and while-statements, the basic decision and repetition structures of all imperative 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.

Debugger

The Visual C++ debugger is operated by choosing Build | Start Debug. The debugger makes use of some of the files in the Debug folder that is created in each project when you compile your program. Note that the debugger will only work on a program which has compiled successfully. You can use the choices that appear when you select the debugger. They include Step over and Step into selections. The first will execute the program a line at a time, including the execution of any functions called by the action of that line. The second will also follow execution through the steps of function calls. They are also available on a button bar at the top right of the file window after you start debugger. Note that there is a Stop Debugging button as well. If you use Step into for library functions such as cout you will arrive in a rather complicated situation. If you do this inadvertently you may simply use Step Out. A useful feature of the debugger is the Run to cursor choice. Perhaps the most useful feature of the debugger is the objects window that opens at the bottom left. It keeps track of the values of all of the program objects while the debugger is operating. You will be tested on the use of the debugger before leaving the lab! Try it out on the programs you will be modifying in the lab.

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. Create a project on your M: drive called Payroll. Add the file named payroll.cpp (this will be located in the same area as the previous labs).
  2. Read through the program to get an understanding of what it does. This program contains a free function. The function is named `instructpay()'; here is how it works. When the computer executes the instruction instructpay(); in the main() function, it jumps down to the statements of the function instructpay() at the bottom of the program. Take a peek at how this function looks. It turns out that functions are nearly the most important thing learned in C++.
  3. Also notice the location of the two objects TAX_BRACKET and TAX. Both of these are special objects 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. The lab assistant will ask to see your modified version of payroll.cpp 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. Run 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 Visual C++ compiler always produces an executable file (.exe) that can be run without the need for the compiler. The executable is always placed in the Debug folder of your project. 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 where your projects are stored, followed by a colon, and press Enter. For example M:.
  3. Type the command cd \Myprojects\CS1711\Lab4\Payroll (if that is where your Payroll project is stored!) Type the command cd \Debug
  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.