Contents

Instructions

This lab has two parts, one on debugging and one on revamping Lab 4.

Part 1: Debugging

When a program compiles but does not run as expected, it can be really annoying and time consuming to figure out why. Luckily, we have a couple of tools at our disposal to help figure out what's going on.

Printing out checkpoints

One way to debug is to add cout statements to your code. For example, suppose you have a program that is trying to convert Celsius to Fahrenheit, but the calculations are not working out. You can use cout statements to display the different parts of the formula to make sure you are computing it correctly.

If you have a larger program and it crashes or behaves unexpectedly at some point, but you are not sure where, add cout lines at the top of the functions you think might be responsible, saying you've entered that function. That'll let you know where you are. One caveat: make sure to flush cout: cout.flush(). This will make sure the output stream is flushed to the terminal before any code that crashes is reached.

Hand execute your program

Another very useful method of debugging is hand executing your code. To do this, follow these steps:

The most important thing to remember when performing manual code execution is: execute the line exactly as it appears—do not assume it does what you want it to do.

For Part 1 of this lab, your job is to use these two techniques to find the bugs in lab6-part1.cpp. This file can be found on Canvas in "Files" → "labs" → "lab6-part1.cpp". You should use both techniques to find bugs. In your code, add comments for each bug you find and note how you found it. Take a picture of your hand tracing and upload that with your submission.

Part 2: Functions

In the second part of this lab, you will add functions to your Lab 4 Part 1 code. Recall that in Lab 4 Part 1, you wrote code to ask users to guess a random number between 1 and 50 until they guessed correctly. Copy your Lab 4 Part 1 code to a file called lab6-part2.cpp Modify your code to include three new functions (that's four including main). One of your functions should be one called runGuessingGame, which should contain most of the code you had in main before, and invoke that function from mainmain should only include that invocation and its return 0; line. You need to come up with the other two functions. Be sure to pick functions that are either modular or improve reusability (or both!).

Submission

Submit your .cpp files to here. You can view the rubric here.

(Back to top)