OK, so we are now at the point in which we are going to go over and not only just putting data into
our own programs, but getting data from the user to use in our programs.
So up until this point, we have kind of been making these standalone programs that are almost just
like a simulation rather than an interactive program, right?
We've just been hard coding, like making our own data into the program, into the file that we write
the code in and then we run it.
And it just kind of does something and shows us something.
But that's kind of a simulation.
It doesn't allow us to interact with it.
Modern software you notice on your computer like you can normally click things and whatever software
you are using or you can type things, and we're going to get into that realm right now.
Of course, we're going to start out with the easier thing, which is typing things and specifically
typing things into the console.
What do I mean by console?
If you forgot what that was, it is similar to kind of what is in the terminal.
We use the terminal.
The terminal can kind of double as a console because when we run the program, we notice that our output
like Hello World and stuff was going to be terminal there.
So it's kind of seen as the console.
Also, the console could be at the bottom of the screen or something in your I.D. or.
You know, wherever it's just kind of a box where the output goes and it shows you the output, so wherever
your hello world and other text that we've been using or mathematical operations that were printed using
the sea out, wherever that showed up, that's kind of what the council is.
I just wanted to point that out.
So specifically today, we're going to be talking about the sea in object.
So what is the see in object?
Well, we've already seen the sea out object for output streams, so it's seen is very similar, but
instead of an output stream, it's an input stream.
So when we use sea in a C++ code, what it does is enable us to capture keyboard input.
So the keystrokes that the user is typing after they run the program in the console, which the user
in this case will be us because we'll be testing our own programs.
So the keyboard input is saved to something called the keyboard buffer.
So just imagine this buffer as a magic array where it just contains all this stuff that's been entered
and it all happens behind the scenes.
So I'm not going to get into too much granular detail about it because I think that it's kind of defeating
the point of the reason that I'm introducing this right now.
So rather than just using the stream insertion operator as we have with see out, that was the less
than less than sign that came after the word sea out.
We're going to flip that around.
So it's going to look like this right here, and this is something called the stream extraction operator
instead of insertion operators, the extraction operator.
So when you use it, it looks something like this.
We put the word see in rather than see out.
And then we put these this flipped symbol here greater than greater than and then we read it into a
variable.
So here's a really basic example I make an integer variable that I declare, then I use scene to read
the console input into this integer.
So what's happening here is something that's kind of the exact opposite of what we did with see.
So see out had is basically instead of seeing we had a out, then we had these flipped right, so the
openings were facing to the right.
Agree there was like the they were less than science, so the greater than part, the opening is we're
facing this way toward the variable.
And remember how I'd kind of described in a previous lecture that it's like our data from the variable
was flowing through those openings into the console out, which is see out, sort it flowing out to
the console for that console out.
This is console in.
So the data is coming from the other direction.
The data is flowing from the console through these openings into the variable.
So that's the difference now.
So what happens is when you run the program in the console or terminal, wherever you want to think
of that S and the C in line and your code is reached.
So this is like at this point, it's machine code, but once the computer recognizes that scene line
that you coded.
The program is going to stall at that point, so it'll pause and say, OK, I'm going to wait for the
user to type something now, so it'll stall, wait for the user to type something and really important
point here.
So it does not continue until you press enter.
And it won't always automatically continue when you press enter, it kind of depends on what your program
is expecting, like how much input your program is expecting.
But if you type enough stuff to satisfy the requirement of input from the program and you press enter,
then the program will have a chance to continue executing.
If you have more seen stuff, then it might ask you to, it might prompt you stall again and have you
enter more until you keep pressing enter and you have kind of fulfilled all that CNN requirement, then
it will be able to finish the program.
So seeing at that point will automatically attempt to convert whatever you typed as input into, in
this case, an integer.
But really, it has to do with whatever type the variable is.
If this was a float, seeing would attempt to convert the input you typed into a float.
Same thing with char, it would try and turn it into a char.
Then what happens, of course, is that data gets stored into the mine and variable and really important
thing about the typing, though the the type of the variable is what I mean, because if you enter something,
if you press some keyboard keys and keystrokes.
That are not the same, really, as the data type that was expected for the variable that can lead to
some strange, strange behavior and stuff.
So we'll see that later on in this lecture.
So that's an important detail.
So let's look at a full example, so slightly more comprehensive, it's an actual program.
Let's go ahead and trace through this.
It's a program that just calculates the area of a rectangle that the user provides the width and height
for.
So I'm going to go over every single line.
It may seem redundant, but I'm doing this just because I think that it can't hurt.
And if there might, maybe there's some people out there that are still a little confused on the flow
of some of these introductory programs we've been writing.
And so I would like to just go step by step since I haven't really had a chance to do that a lot.
And hopefully that will help solidify things for the potential people that may not completely grasp
how our current programs are working.
So first line here is the include directive, and specifically it's including EO stream.
This enables us not only to use the C out that we've been using, but also the C in that we are just
learning about now.
So seeing as part of our stream as well in this specific program, we are deciding to use the namespace.
Of course you don't have to.
You could put a Stevie Colon colon in front of the C out and would also need to put Stevie Colon colon
in front of the C, and if you did not choose to use that standard namespace.
Next, we define our main function, of course.
Then we declare a width integer variable.
So this will be used to store the width of the rectangle.
Same thing.
This is going to be used.
It's called height, and it's not.
This is a typo.
It's not supposed to be used to store the width.
It's going to be used to store the height.
And then we print out a little message to the user to ask them to please enter the width.
So this is printing out to the console, and it's going to prompt them to enter a value for it.
Next, we actually read that stuff that they typed into the width variable using in on the next line.
We do the same thing with the hype.
We ask the user by showing a message printing to the console for them to enter the hype.
Then once again, we you see end to read whatever they typed into the height variable now on this last
informative, kind of useful line, we print out the area of the rectangle, so we have a little message
here that says the area of the rectangle is.
And then after that we see out another thing, which is the width times, the height we are using the
multiplication operator, similar to how you've seen in previous lectures.
And we're multiplying these two variables that hold the values that came from the user.
The width from line eight and the height from line 10.
Then we have an in line at the end here, the main function returns a successful exit code back to the
operating system and our program is finished.
So that's a pretty simple example, this consider another example, so the only difference in this one
is that we have three variables and that we are not taking them in on separate lines.
We are taking the input in all on the same line.
And the same line is which we print out the message.
So we print out a message that says enter label width height all at once.
So it kind of prompts the user to enter them all at once.
Then what we do is on that same line, we read it in with CNN, so we read in.
Here's the extraction operator and we noticed that the extraction operators are separating in all three
of our variables here.
So we're reading in the label the width in the height.
So this time our rectangle has not only a width and height, but also a label which is expected as the
first input for the user to enter.
Then we calculate it down here, of course.
So this is us choosing to have the user enter everything on one line in the same line as which we prompted
them with the see out.
So let's take a look at what happens on the other side of things, so not in the text of our program,
but what happens when we run our program and what the user sees.
So here's a few examples.
I go ahead and I've made an executable called input out an executable or a binary, depending on the
system you're on.
This is input out because I am using Bash.
And you noticed that this one looks OK, right?
I enter a label, I then a space, then within a space, then a height.
Then it says the area of rectangle B, which is the label, is eighty eight times ten.
That makes sense, right?
However, these other ones don't make a ton of sense, so let's talk a bit about that.
So most people would probably think that the a user would see this program and they would just be like,
Oh, I'm going to enter things in the order that they have mentioned here label width, height.
That would be a wrong assumption, though you should never assume that the user is going to follow your
expectations or even your instructions, for that matter.
You should always be on the defense, and most importantly, because hackers can actually intercept
and input that can lead to you not only the crashing of your program based on that input, but also
it could eventually lead to them having control over whatever system your program is running on.
This is something outside the scope of this course, as it's more of a computer security topic.
But yeah, it can be bad if you do not sanitize the user input.
If you do not write code, that makes it makes your program not be so vulnerable to whatever the user
enters.
You should just assume that the user is going to enter any possible thing that could be entered.
So you want to protect against malicious input that could lead to a compromised system or, you know,
just crash your program kind of in less extreme cases.
So what happens if they enter things in the wrong order?
Well, we see right here is not entered in the right order.
We have a integer before the label, which was supposed to be a character and then a character and then
another integer, and we notice that it says the area of the rectangle.
Eight.
Rather than it being rectangle B, and then we see that our answer is zero, what seems strange, then
we have this one here where we got some weird results when a float was used, so eight point seven was
used and that caused the program to also have a result of zero.
Then this one down here, we noticed that there's no spaces in between the user input and in fact,
the program didn't even finish.
And that is because it interpreted this in a certain way in which it thought that it still needs to
collect input.
So that is something we'll look into in the next slides as well.
But here the program is hung up and it is still stalled, waiting for additional input.
So let's specifically take a look at each one of these cases and not only see what's going on, but
what is the keyboard buyer looking like, and then we will break down how the data is being read into
the variables that we created in our program.
So let's look at this top one first.
So this is the one that was actually correct and had a expected result, at least I won't say correct
because that's up to interpretation, whether we provided the correct instructions or not, but it came
out as expected.
The area was 80 and the label was B, so the keyboard buffer is right here.
I have some boxes.
Each one of these boxes kind of represents a character that is being stored on the keyboard buffer.
Down here, I show kind of what we're expecting with each piece of the input.
So first is a B, and that's OK.
That's read in as a chart.
The chart is just expecting one keystroke kind of because a car can just be one character.
Of course, if you look back at the ASCII table, you can notice that most of the things that can be
clicked on a keyboard and pressed on a keyboard are represented there.
So that gets red and it moves on, it has a space separating the char and the next thing, which is
a number.
It reads in this character, but it's supposed to interpret it as an end.
And it does because it's just an eight with a space after it.
And so it reads in this eight and says, OK, an integer eight is being read in.
Then there's a space and then there is a one on the buffer and a zero on the buffer.
And since the integer doesn't discriminate against only having one, you know, against, sorry, I
discriminate against having multiple digits.
You know, an integer doesn't only have to have one digit like the eight, it could have, you know,
tons of digits.
So it sees this one and then it sees this zero and it's like, OK, that's an integer.
And so it reads that in.
And so everything works out, OK, because it was kind of entered in the expected order.
Let's move on to this next one.
So what happens here?
So we have a be entered this kind of the same as the previous slide that begets readiness to cha OK.
And then we have a space, then we have an eight and C++ actually reads this eight and there's an integer
and it's like, OK, eight is an integer.
I'm going to read that in.
But the next thing was a dot, so I'm not going to have that be part of an integer.
So what ends up happening with the rest of this is that scene actually fails on this because it cannot
read the dot and the dot and as an integer.
And so what happens is that it says basically the scene is an object, something that you don't need
to worry too much about that terminology now, but it basically is able to note that it it failed.
And so it sets some sort of status saying that, Hey, I failed.
I failed to read this and something was wrong.
And this is something that can be checked in a similar manner to how we checked for the what the length
was of a string.
Remember how we had like a string variable dot length and then it had parentheses, and I was saying
that it was a length function that can be called on the string.
Similarly, we can call a dot fail function on the scene object.
And so don't get too carried away with the whole function being called an object.
Yet I keep saying over and over again that we will get to this and you're probably pretty antsy for
me to explain it.
But right now, just think of this as being similar to the dot length, except it does something else
for us.
It tells us whether the input stream seen has failed.
So whether encountered something that made it fail or not, this is something that we will be using
in the next lecture where we write some code and we're able to check whether it failed or not.
So at this point, what happens is that there's kind of all this stuff left from the keyboard, but
it failed at this point, and so we're not really going to read it in.
And what happens is that the result just turns out to be zero.
Even though this integer got red in the second integer was not successfully read, and so we didn't
get our expected result.
Let's take a look at this one.
So we have a similar thing, except the eight is the first thing.
Eight is a totally valid character, totally valid char.
You don't have to just have letters, pretty much any keyword press like we were talking about with
the ASCII table.
The AC III table can be considered as a legitimate char.
And so the eight gets read in is a char.
Rather than it being seen as a number, it's seen as a character.
And so it's fine with that.
So the input stream continues.
It has the space, but then it encounters a b and it attempts to process this b by reading it into an
integer process as an integer.
Yet it struggles with that.
And so it actually fails at this point in.
This can be confirmed if you were to use a C and fail to to check it out and see if that was true or
not.
And you would notice that it is in fact true, it did fail if it tries to reel in a B as an integer
and then we still have this leftover stuff.
So the leftover stuff is something interesting that we will talk about in the future as well, probably
the immediate future with either the next election or it's a, you know, immediately subsequent lecture
because stuff can get left on this keyboard buffer.
And then what happens is if you do not clear out the stuff left on this little magical keyboard buffer,
if you try to go to scene something else again.
It's instead of letting you type more stuff as the user is going to just grab whatever's already inside
of this keyboard buffer so you can think of like, whatever the user types, it just stacks up into
this keyboard buffer yet.
If there's something that's already left there, like there's some leftovers, like you can think of
some actual leftovers to be eaten, it'll eat up those leftovers first and read that into your variable.
And that's kind of confusing because if you don't know that there's leftovers, then when you get your
output, you're going to see some stuff that you didn't expect to be there because that's what you get
read in other rather than like what you expect to be read in.
So let's take a look at this last one, what happens here?
Well, there's no spaces, so what happens is that the bigger threat in as a child, even though this
is all smashed up against it, it's like, OK, a car is just one key press.
This is a B.
It's valid.
I read that cars all get to go.
But the second thing is these are the eight and the 10 smashed together.
And honestly, you can't blame C++ too much for this because really, think about it if somebody wrote
eight one zero right next to each other on a piece of paper and then they asked you to explain what
that was.
This can be interpreted in so many ways, but the most obvious thing is that it's the number 810, right?
You're not just going to look at this and be like, Oh, it's for sure.
Eight and then 10, it can be seen as eight, 10 as one number 810, it could be seen as eighty one
and zero.
It could be seen as eight and one and zero.
It can be seen as eight and 10.
All those things.
So you can't really blame C++ for noticing this as one integer eight 10.
And that's what it does.
It actually looks at this buffer at each of these spots, and it considers this one integer.
Well, why does it just stay blank here?
And it does not show my bash prompt again.
That's because the program hasn't finished.
Well, why hasn't it finished?
And that's because it's only read in one integer.
Yeah, in your program, you specified for it to continue trying to read in a second integer after this.
So it's still waiting for that next integer.
And that's why you notice the program has not finished.
You would have to, you know, continue to enter an integer and and or press the enter key.
So that's why that looks like that.
So enough of me explaining this stuff.
Hopefully, though, it was some good graphics to help you understand how the scene really works.
Hopefully, you're also pretty excited that we can make some interactive programs now and that is what
we were going to do in the next video and some of the subsequent videos.
We will be making some more interactive programs messing around with user input, but most importantly,
in the immediate future.
In the next video, we will be trying to see all the ways that you can really mess up with the input
reading and is the wrong data type and stuff like that getting unexpected results.
We're going to kind of suss all that out and throw it out there so you can really see what could potentially
happen when reading and user input.
OK, so with that, go ahead and open your next text editor if you're going to continue on from this
exact point.
Otherwise, if you take a quick break, I will see in the next lecture in whatever text editor you might
like, I'm going to go ahead and use Visual Studio Studio code.
I believe potentially.
But with that, I'll see you in the next lecture.