>> SPEAKER 1: Let's take a look at the CS50 library,
specifically its GetInt function.
Here we have the actual source code for GetInt.
And notice that it's not too long, and most of it constitutes a while loop--
an infinite loop at that-- that only returns a value once we've actually
gotten what we expected.
Let's walk through it.
>> Notice up here first, the while loop begins.
Notice next that we have a line of code that actually calls GetString,
and stores the return value in a variable, called line, of type string.
We then do a bit of a sanity check.
If line == null, then we curiously return INT_MAX.
>> Now it turns out that INT_MAX is a special constant declared elsewhere
that specifies the largest possible int that you can represent in a
program like this.
Now we've arbitrarily decided to return INT_MAX as a sentinel value of
sorts, one that we've reserved as meaning an error has occurred.
So the price we pay, of course, is that GetInt can apparently not
actually return a number as large as INT_MAX, because even if it wants to,
that return value should really be interpreted by the caller--
whoever's using GetInt--
as an error of some sort.
>> Next, notice that I've declared an int n and a char c.
In this next line of code, I call a function called sscanf, passing in
four arguments.
line, which is the string the user's typed in, "%i %c", which is a format
string that I'm expecting the user might type, followed by the address of
n, and the address of c.
Now sscanf's purpose in life is indeed to scan a string looking for the
particular format that the programmer has specified as that second argument.
In this case, %i is in there, as is %c.
So if sscanf encounters an int in the user's input, that int will be stored
inside of the variable called n, because we have provided as the third
argument to sscanf the address of n.
Which means that sscanf can indeed go there, and update the value therein.
>> Now, in case the user types in something more
than one or more digits--
in other words, a char of some sort--
that second variable c, whose address we passed into sscanf as its fourth
argument will also be populated.
Now the upside of checking for an additional character from the user is
that if he or she does not cooperate, and types in more than just an int,
we'll be able to detect it in this manner, because in that case, sscanf
is going to return 2, signifying that both of the placeholders were filled
with values.
But we're hoping that sscanf instead returns 1, which means the user only
provided an int.
>> What do we do if sscanf indeed returns 1?
Well, we immediately free the line that the user typed in, and then we
immediately return n, having gotten an int.
Else, if sscanf does not return 1, and the user therefore did not cooperate,
we still free the line, but we now prompt the user to retry.
And because we're still inside of that otherwise infinite loop, the process
will begin again, and maybe again, and maybe again, until the user actually
provides us an int.