Lab5: Debugging/Adding functions to Lab4
Contents
Instructions
This lab has two parts, one on debugging and one on revamping Lab4.
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:
- get a piece of paper blank text file
- draw a big box and label it "Console"—this is where cout data goes
- draw a big box and label it "main" (this is main's memory box)
- starting at the top of
main()
, start executing each line of the program- if the line is a variable declaration, make a small box inside of the current function's memory box and label it the name of the variable
- if the line is a variable assignment/reassignment, put a slash through the old value in the variable's box in the current function's memory box, and write the new value next to it
- if the line involves a call to a custom function, make a new memory box for that function and label it with the function name; add a variable entry to the memory box for each of the function's parameters; go into the function and execute the instructions
- if the line is a return statement put a slash through the function's memory box and return back to where the function was invoked
- if the line uses
cout
, write the output data to the Console box - if the line uses
cin
, write your input to the Console box
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 lab5-part1.cpp. This file can be found on Canvas in "Files" → "labs" → "lab5-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 Lab4 Part 1 code. Recall that in Lab4 Part 1, you wrote
code to ask users to guess a random number between 1 and 50 until they guessed
correctly. Copy your Lab4 Part 1 code to a file called lab5-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
main—main 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)