So Ben mentions that you need to initialize Guess to " " so that you know what’s being stored there. Well, that’s the reason why… but why? Really, truly why?
Here’s something I learned from the Harvard CS50 class through edX…
If you were to define guess like this:
string Guess;
What will happen is the computer will set aside a chunk of memory labeled “Guess” that is of size String. What’s the size of string? Well that’s irrelevant right now, but it’s the compiler’s dedicated size for storing a string. Not important.
What is important is that if you don’t tell the program WHAT to initially store at that memory block called Guess, then the values stored inside that memory may be whatever was using that block of memory last!
That could be a calculator app, YouTube browser tab, or your operating system… whoops!
We call this data “garbage values”.
So what this means is, if you were to ever try and access or modify Guess before anything is stored in it, you may accidentally access memory you shouldn’t, which can crash, break, corrupt, or BSOD other apps or your operating system.
So to ensure that you aren’t taking random pieces of memory that other programs require and storing / editing it inside of Guess, you instead first tell guess to store " ", or a blank string.
This way, you can ensure you always know what’s inside it and you’re not accessing parts of the operating system you shouldn’t.
And this is true for ANY variable type, not just strings. Initialize all your variables to something, anything. Just don’t leave them blank. This also serves a second purpose (setting default values).