Lab 6: C++ Debugging Lab: Working with the VSCode Debugger
Instructions
-
First read this page then start working on lab with the GitHub classroom link below.
-
Put your code in the GitHub repository for this lab.
-
After you complete the lab, answer the questions in the README.md file. Enter your answers directly in the README.md file.
-
Github Classroom Link: https://classroom.github.com/a/xylMar4l
Objective
Familiarize yourself with the essential features of the VSCode Debugger while working with a C++ programs.
Debugging - Part 1
- Setup:
- Open the file
ecommerce.cppin thePart1directory in the repository.
- Open the file
- Setting Breakpoints:
- Set a breakpoint at the start of the
mainfunction. - Set another breakpoint inside the
processOrderfunction. - Add a third breakpoint in the
applyDiscountfunction.
- Set a breakpoint at the start of the
- Start Debugging:
- Launch the debugger. First click the debug icon in the activity bar on the left. Then click the “Run and Debug” button.
- Stepping Through the Code:
- When the debugger pauses at main, press “Step Over” until you get to the
processOrderfunction call. - Now, press “Step Into” to dive into the
processOrderfunction. - Inside the
processOrderfunction, you’ll soon see theapplyDiscountfunction call. First, press “Step Over” to notice how you skip the internals ofapplyDiscountand directly get its return value. - Run the
processOrderfunction again. This time, when you’re atapplyDiscount, press “Step Into” to go inside it and inspect its inner workings.
- When the debugger pauses at main, press “Step Over” until you get to the
- Observing the Call Stack:
- Inside the
applyDiscountfunction, take a moment to observe the CALL STACK pane. You should see the function hierarchy:applyDiscountcalled fromprocessOrderwhich was called frommain.
- Inside the
- Modifying Variables & Stepping Out:
- In the
applyDiscountfunction, under the VARIABLES section, change the value of user to “Jane Doe”. - After making this change, press “Step Out” to move back to the
processOrderfunction and observe how the change impacts the total cost.
- In the
- Completing Execution:
- Press “Continue” to complete the program.
- Expected Output:
Total Cost for John Doe: $50.99 - Note: If the user was changed to “Jane Doe” during debugging, the discount wouldn’t be applied, and the total cost would be $55.99.
Debugging - Part 2
- Setup:
- Open the file
Student.cppin thePart2directory in the repository.
- Open the file
- Setting Breakpoints:
- Set breakpoints inside the
read_students_from_filefunction and inside thecatchblock in themainfunction.
- Set breakpoints inside the
- Start Debugging:
- Start debugging.
- As the debugger stops at the breakpoint, inspect:
- The state of the file object to ensure the file has been read correctly.
- The values of the
nameandagevariables. - The contents of the students vector after reading from the file.
- Debugging with Error Handling:
- Intentionally introduce an error by renaming the
students.txtfile tostudents_renamed.txt. - Debug the program again. This time, the debugger should stop inside the
catchblock. - Inspect the thrown exception’s message by examining the
exvariable. - Fix the error by renaming the file back to
students.txtor updating the filename in the code.
- Intentionally introduce an error by renaming the
- Enhance the
Studentclass:- Add more attributes, such as
gradeormajor. - Modify the file I/O functions accordingly and use the debugger to ensure everything works as expected.
- Introduce a few more intentional errors or exceptions and practice using the debugger to catch and understand them.
- Add more attributes, such as
Debugging - Part 3
- Setup:
- Open the file
factorial.cppin thePart3directory in the repository.
- Open the file
- Setting Breakpoints:
- Set a breakpoint inside the factorial function.
- Start Debugging:
- Launch the debugger.
- As the debugger stops at the breakpoint:
- Inspect Variables: Hover over the variable n to observe its value during each recursive call.
- Step Through the Code: Use the step-over and step-into buttons to navigate through the recursive calls.
- Try to identify the reason why the recursion never terminates.
- Fixing the Recursive Function:
- Once you’ve identified the missing base case, modify the factorial function to include a termination condition for the recursion.
- Debug the program again to ensure the recursive function now works correctly.
- Expected Output:
Factorial of 10 is: 3628800
- Now try:
- Modifying the input value (number) to larger values and see how high you can go before running into potential issues.
- Observe how the call stack grows with larger input values.
- Introduce other recursive functions, like the Fibonacci sequence, and practice debugging them.
After you complete the lab, answer the questions in the README.md file. Enter your answers directly in the README.md file.