CS50 Video Player
    • 🧁

    • 🍫

    • 🍎

    • 🍿
    • 0:00:00SPEAKER 1: Welcome, everyone.
    • 0:00:01My name is Carter.
    • 0:00:02And today I'm going to show you my guessing game and how
    • 0:00:04I can use debug50 to help figure out what's wrong with it.
    • 0:00:07So here, I'm trying to write a guessing game that
    • 0:00:09is written in the spirit of the elementary school game
    • 0:00:10where you would guess a number and someone would tell you,
    • 0:00:13it's right or wrong.
    • 0:00:14And I called my program Guess.
    • 0:00:15So I can run it in the terminal with ./guess.
    • 0:00:19Here go to ./guess, hit Enter, and I have to guess a number.
    • 0:00:23So here I might guess, maybe 2, and hit Enter.
    • 0:00:26But then I get this flood of messages in the terminal.
    • 0:00:28And I'm wondering, well, what's wrong with this?
    • 0:00:31Why am I getting so many messages here?
    • 0:00:32And to try to figure that out, I can go into my code with code guest.c.
    • 0:00:39And then I can see, well, I'm not entirely sure what may be wrong here.
    • 0:00:44So I need to pause my code at some point.
    • 0:00:46And for that, I can use a break point.
    • 0:00:47So let me go ahead and go to this left side of my guess.c file, where
    • 0:00:51I see these red circles pop up.
    • 0:00:54I can click on one of them to turn it bright red.
    • 0:00:57And at that point, I've set a pause point in my code.
    • 0:01:00And I can use debug50 to go in and actually step through my code
    • 0:01:04and see what's going on in there.
    • 0:01:06So let me go ahead and type debug50 of guess, hit Enter.
    • 0:01:11So now that I have my code opened up in debug50,
    • 0:01:14I can see a few different things.
    • 0:01:15I can see that my number variable is currently set to 0.
    • 0:01:19And my guess is currently set to this odd 32,764.
    • 0:01:23But as I go through my code, I'll actually
    • 0:01:25see those numbers fall into place.
    • 0:01:27So if I use this Step Over button to walked to the next line of my code,
    • 0:01:32I can see that number becomes 5, as I expect it to on line 6.
    • 0:01:37And then as I go to line 8 and Step Over that, I get prompted for,
    • 0:01:41what's my guess?
    • 0:01:42And at that point, I might type, well, 3 again.
    • 0:01:45And I'll hit Enter.
    • 0:01:47And then I see guess becomes 3.
    • 0:01:50So now I'm wondering, well, if my guess is wrong and my number is 5,
    • 0:01:55my guess is 3, shouldn't I only get wrong guess just once?
    • 0:01:58So I'll actually just keep walking through my code with Step Over.
    • 0:02:01And I'll see, I'm printing "Wrong guess!", which seems right.
    • 0:02:04And now I should just exit the program.
    • 0:02:06That should be it.
    • 0:02:07But I really see that my line in that yellow highlight
    • 0:02:10is going back up to 10, line 10 there.
    • 0:02:14And it's actually continuing to go through and print "Wrong guess!"
    • 0:02:18And so at this point, I can actually see that maybe the bug in my code
    • 0:02:21is I shouldn't have used a while loop.
    • 0:02:23I should have, maybe, used an if statement,
    • 0:02:25where I'm only checking for this condition once.
    • 0:02:27So now that my while loop has changed to an if statement,
    • 0:02:30I should be able to recompile my code and see what's changed.
    • 0:02:33So I can use "make guess".
    • 0:02:35And then I'll run my code, guess.
    • 0:02:38And when I guess 3, I guess I should get "Wrong guess!"
    • 0:02:41So I'll do 3.
    • 0:02:42And then I'll get "Wrong guess!", but also "You're correct!".
    • 0:02:47And at this point, there's probably another bug in my code.
    • 0:02:50And so I'll actually leave this one up to you,
    • 0:02:52to use debug50 and see, well, why am I getting both "Wrong Guess!"
    • 0:02:56and "You're correct!"?
  • CS50.ai
Shortcuts
Before using a shortcut, click at least once on the video itself (to give it "focus") after closing this window.
Play/Pause spacebar or k
Rewind 10 seconds left arrow or j
Fast forward 10 seconds right arrow or l
Previous frame (while paused) ,
Next frame (while paused) .
Decrease playback rate <
Increase playback rate >
Toggle captions on/off c
Toggle mute m
Toggle full screen f or double-click video