Struggling with visual studio debugging? Here are some debugging code tips in Visual Studio.

Three key questions to ask yourself while visual studio debugging

  1. Where is your error? – Find the line of code which has the first error
  2. What is your error? – Read the error message carefully, copy and paste the error message into google if needed
  3. What is causing the error? – Go to the line of code, find and fix the mistake in the code

If you have multiple errors in your program, fix the first few errors and work through the list from the start to the bottom. Keep re-compiling the project as generally the number of errors will be reduced substantially once the first few errors are fixed. The advantage of visual studio debugging is that you can look for a red squiggle in your code which means that Visual Studio has detected a syntax error and won’t compile the code until you correct it.

 

Error types you will encounter

Syntax Errors

What happens – The code does not compile because the rules of the C++ programming language have not been followed. Work through compiler errors from the first few to the bottom.

How to avoid these errors – Don’t write code really fast as you will save a lot of time by not having to debug a long list of compiler errors

Logic errors 

What happens – The code compiles but does not work as expected. These errors are the hardest to find.

How to avoid these errors – The way to combat this is to test, test, and test your code. Try to brainstorm and think of all possible scenarios for your code and test them.

Runtime errors

What happens – The code compiles, but the program performs an illegal operation whilst running. Examples include dividing by zero and going out of bounds for an array.

How to avoid these errors – Use try/catch statements to handle any errors that exceptions. Carefully read any runtime error messages that appear. These errors can be confusing so researching the error on Google and Stackoverflow can give an indication of the problem.

 

How to use the debugger in Visual Studio 

Start debugging [F5] – Complies the code and runs till the breakpoint

Set breakpoint[F9] – Toggles breakpoint on the line in the CPP file

Step into [F11] – Executes one line in the program. If that line is a function call, it jumps to the first line of the function

Step out[Shift + F11] – Continue execution until the current function returns or until a breakpoint in the function is reached

Step over[F10] – Executes one line in the program. If that line is a function call, the entire function is executed.

Share this post