In this video, I'll show you how to set up and use the Integrated Debugger in Visual Studio code.
So for this example, I'm going to use Project two and I'm just going to make one little change right
here.
I'm going to initialize that number to 100 just so we can see it in the debugger in a minute.
So let's create this.
What we're going to do is we're going to create a launch JSON configuration file.
First order of business is click on one of the main CP files.
Since I'm using Project two, that's the one I'm going to select.
So you can click on that file and then you're going to come up to run add configuration.
And it's going to ask you again, what's your environment?
We're going to always be using C++, GDB, WL, DB, so that's what I'm going to select.
And again, I'm using GW 64 G plus plus.
I'm going to select this guy right here.
And we're going to come over here.
Don't worry about this.
You can just click that little red square right here and get out of here.
The important thing is and we can come back over here as well to explore, just to clean this up a little
bit.
So the important thing is it has created a launch JSON file for us, and this is the one that's right
here in the editor and this is the one that we're going to configure.
And the configuration here is pretty easy.
There's just one entry we need to change this one here in line 14, which is CW.
That's our current working directory.
What we're going to do is we're going to replace this with file name, and this time we're going to
grab the dollar sign of the curlies as well.
So we're going to put that right in there like that.
That's it.
So we're done with that.
We can close up our launch JSON.
Now what we're going to do is we want to debug this program.
So in order to debug the program, we need some breakpoints.
We need to tell the C++ debugger where to stop because otherwise it's just going to run the program.
So it's got to stop so that we can examine variables, we can look at our logic, and we can step through
the code one line at a time so that we can follow our logic and find mistakes or verify that what we
did was correct.
So let's do that.
We can do this a couple of different ways.
First of all, make sure that we've got this selected main step in whatever project we want to debug.
In this case, we're debugging project two, so make sure your default is selected main DHCP.
Then we can come up here to run.
Start debugging.
Or we can come over here on the left side here again, select main app and click that bug right there.
Run and debug.
That's what I'm going to do.
And now what we need to do is we need to add a breakpoint to our code.
And you can see over here in the gutter here, I'm sliding through and you can see that red dot coming
around.
That's a breakpoint.
So if I want to put the breakpoint right about there, it'll light up.
It'll also show up down here that says we've got a breakpoint.
Visual Studio code has a little glitch sometimes where it captures breakpoints from a lot of the projects.
Just the one you're working with.
And an easy way to deal with that is just to before you start debugging, just come over here and click
that X right there.
That'll clear up all the breakpoints everywhere, and then you can just set the breakpoint where you
want.
So I'm going to set the breakpoint again right there.
So now that we've done that, we're going to run the program.
And what we can do is we can come up here and click on that little green arrow and you'll see what happens.
The debugger comes into play.
A lot of things seem to happen all at once.
We've got this little menu bar right here, this bar of icons that allows us to step through our code
one line at a time, stop the code, we run the code and so forth.
We also have this variables window that popped up over here on the left, and this is showing the values
of our local variables.
Look at NUM.
It's initialized to 100, so it shows up right over here.
Also we can use our cursor and just hover over here and you can see its values 100.
So it lets us inspect the value of that variable as we're running the code, which is really neat.
So right here, this is the other line that shows you where I'm at in the program.
So you can see that yellow bar shows us exactly where our program is.
This line has not executed yet.
So if we want to execute it, we can come over here and step over it or step into it.
And we'll talk about all this in the course.
And in this case, I want to step over it and notice the output came out down here.
So that's the line we're at.
So I'm going to step over it again and now we're going to get the input from the user.
So let's say I type in 1200.
Now, as soon as I press enter, that 1200 will be red into num, right?
So my variables should change.
I'm going to press enter and now notice that number up here is 1200 and I can also hover over.
I'm over here and it's 100.
So this is a really, really powerful feature debugger.
The are are great.
We use them all the time.
We don't overuse them.
If it's a really simple program and you've got simple logic and you can test your code easily, then
there's real there's no real need to debug it.
But sometimes it comes in really handy when we're trying to figure out a problem or looking at some
really complex logic that we want to walk through step by step.
So that's it.
Now, if we want to stop the program, we could just click that little red box right here to stop.
If we want to just continue full blast and skip all the breakpoints, we can click that little arrow
right here.
Or we could just click another few times until the program ends.
And let me just stop it here so you can see what that looks like.
So I stop it.
Right.
So now it's on notice.
It's changed back to blue down here.
And I can come back to my.
Explorer and I can see everything that I'm doing.
So there you go.
That's how the debugger works.
In the next video, what I'm going to do is I'm going to show you how to use Visual Studio code with
the source code that I've provided for the course.
The source code that I provided for the course is in the format of code light projects, but that's
okay.
The work in Visual Studio code as well, and I'll show you exactly how to use them in the next video.