OK.
So in this lecture, we are going to go over some of the coding part of the last lecture, we actually
showed some code in the last lecture, but this one, we're going to kind of mess around with some of
the seeing object stuff.
And we're also going to talk about the keyboard buffer and what we can do when there is that failure
of scene.
Remember, we talked about that scene dot fail with the parentheses.
The Dot fail was, of course, a function.
And once again, pretty much like every lecture, I'm going to say something like, Oh, this will go
over this in the future, but I really want to kind of explain why I'm introducing something like seeing
Dot fail at Dot anything right now.
So the reason that I am introducing this is because I think that it is very useful.
It's important to know about what's really happening with C in.
If you're going to learn about taking input.
I feel like you do need to know a little bit about the keyboard buffer and how to check for things.
And it just so happens that in this programming language, C++, this method or function is being called
on.
This the in object.
It's that's the way that it was set up, and I can either choose to kind of tell you some useful things
that can be used with this thing so you can better understand it.
Similar to the string where we had a string, the string variable length, I feel like that's an important
thing to know.
Of course, I didn't introduce all of those functions, but it's important to know.
And so some of you might be thinking, Well, we haven't been taught functions yet, and I don't know
what an object is.
I don't like you say, see an object.
I don't know what that is.
And this dot thing is confusing.
And why haven't we learned about functions yet?
I'm definitely going to heavily go over functions, but.
I don't want to have to go over all that right now and go over objects, which would require me to go
over classes and all of that just to show you a few useful things.
So what?
I'm hoping that you take out of this when I say dot, something with the parentheses is just you can
memorize just a handful of useful stuff that goes along with the topic that we're talking about.
So like just string dot length.
If you only remember that one thing dot length out of all of the member functions that exist for it,
that is perfect.
You can just remember that one thing.
And today I'm going to introduce two more dots something not just dot fail.
And if you just remember, you know, one or two of them, you know, that's great.
We're going to heavily go over the objects and function stuff in the near future.
But right now, I don't want to just let this stuff that has to do with the keyboard buffer just like,
you know, not be mentioned because I feel like it's it's very important.
It's important for you to know how this language works and know what the keyboard buffer is, because
you might not be able to understand what's happening with your input if we were not to go over this.
So just wanted to put that out there in case some of you are worried, you know why?
I'm just not explaining what an object and function are in more detail.
I've given a basic explanation of the function, but it doesn't really explain why there's this dot
function, right?
So just kind of want to get that out there.
I will explain that.
But for right now, if you can just remember this little bit that some of these things, like a string,
we have the dot length and for a scene, we're going to have a dot fail.
We'll talk about that today and we'll have some other two other, you know, dot and then a different
function name with the parentheses as well.
Just want to get out of the way so that we can go ahead and get started.
You notice that I've made a program here called Input DCP.
I've already kind of set up this standard skeleton of a program that we will be using a lot over and
over.
So go ahead and set this up if you haven't.
Of course, it is up to you, whether you want to add this line or not.
But yeah, so of course, we want to include ice cream, right?
Because like in the last lecture, we said that not only does see out come from Io stream, but seen
also comes from my stream.
Another thing is that I have my terminal and I have navigated already to the directory that contains
my program.
You notice this in this first program.
So let's go ahead and get started.
What I'm going to do is I'm going to declare a few variables because like you might have guessed, we're
going to try reading different variable types and from we're seen in different orders and kind of inspect
what's happening.
So I'm going to do a chart.
I'm just going to call this see, I'm going to do and you can call this, I'm going to do a float and
I call this F.
So then what I'm going to do is prompt the user to input some of this stuff, so I'm going to say see
out and I'm going to say into
char and float like that.
So actually, you know what, I'm going to do this and another order unless they flip it float and.
And I'm actually going to put another into our statement to.
So chocolate and Ms.
Hello.
And then this will prompt the user to enter that, and then I'm actually not going to put an end line
because I want them to enter it on the same line.
So then I will do CNN and we will do a char, which is see right will read it into our C variable,
that char variable.
And then I will do the float and then the AI and then the AI to.
And then what I'm going to do here is I'm going to sit out and let's just say Char, and then we'll
put see and then I'll say float, and then we'll put F. And then we'll say and one and I'll put I and
then we'll say and two and I'll say two, and then I'll put it in line here.
So we're going to print them all out, basically read them all in, print them all out.
So let's go ahead and just kind of enter something that's expected first, so I'm going, go ahead and
save this.
I'm going to actually just compile this.
Let's see.
I'll do.
I'll I'll just call this
test and put dots easy.
All right, so now I'm going to do this.
Run this test in front, so asks us to enter it.
And so what I'm going to do is just put a so I'll do a and then a space and then I'll do, let's see,
two point seven, which is a float and then I'll do a 10 and then I'll do one 23.
Cool, so it prints them up like kind of without spaces, I should put some spaces here, so let me
go ahead and add some spaces, so that's more reasonable shared on that originally.
So I'm going to say this, I'm actually going to clear this, compile it and let's go ahead and write
it again.
A.
Ten point three.
OK, so now they're separated enough to read it, so we notice that it reads them in correctly and prints
them correctly.
So we have this a is the cha two point seven is getting read into the float.
10 is being read into integer one and to three is being read into the integer oh two.
Cool.
So let's go ahead and run this again and test it with something else, so I'm going to put a car and
then I'm going to put a float and then I'm going to put another char.
So I'll put B and then I'm going to put an integer for the second one.
So let's see what happens.
Huh.
So this is pretty weird.
So we noticed that, yeah, we got the zero kind of like we saw in the last lecture, but then for this
second one, we got this like huge number.
I mean, that's pretty wild, right?
So what's happening with this second one?
Well, what's actually happening is that it's printing out and un initialized pretty much like garbage
value here.
So this this I too, it's just like some massive number because it hasn't been initialized to anything.
So it's kind of just like this random
chunk of like memory because we haven't really initialize to any specific value.
You know, if you you could put like a one here that would be initialized into a value.
Let's see if it actually shows up here.
So I'll compile this, and I saved it and I'll run it, I'll do the same exact thing now, so I'm going
to do.
Let's see the a two point seven, b 10.
So now you notice that it's actually a one because we initialized it to a one before we didn't initialize
it.
So it could just put some type of like this is what we call garbage a garbage value because it's some
un initialized value that is just pulling like from the memory wherever we are like, you know, putting
the end.
So that's why you get this unexpected number.
So but what can we do to kind of prevent this stuff from happening or or catch it, you know, how can
we see what's really going on here if we saw this?
You would probably be really confused with like, OK, well, yeah, the B.
Maybe that's easier because we insert a char, but then I'm getting this garbage value and you know,
what's what's going on with this stuff?
So let's see if I actually have another.
Let's see if.
Let me kind of demonstrate what's going on, what's going on with this, so the input is actually failing
here, but this stuff remains on the buffer.
So let's kind of let's kind of prove that I'm going to go ahead and try and prove that to you now.
So this is why I'm going to use some and fail.
And another scene that something is going to be seeing, don't ignore.
So here's something we can do with this see and fail.
We can use it if and then what we can do is inside of this if statement we can check, did it fail?
So see and fail is going to return this boolean value for us.
If you think back to the functions I was talking about when I originally introduced that idea of a function
and I said some functions returns, some things, some functions don't return.
Some things see end fail is a function call and it's going to kind of return.
This true or false thing is whether the season has failed or not.
And so we can check that with an if remember, inside of here, we're looking for a true or false value.
And so we can check, OK, if.
You know, true, if this is true, if seeing fail is true, then we can say something, you know,
and we'll put our brackets in so we can say, you know, oops, CNN failed.
So we'll just put that.
So let's go ahead and test this to see if it's in fact failing, I mean, it looks like it's failing,
right?
So I'm going to go ahead and clear this, compile it and run it.
So I will do this a two point seven b 10.
And it does say that it says, Oops, CNN failed if we were to run this again and we, you know, put
stuff that was OK.
You know, you notice it doesn't say that C and failed because we entered the right data types and stuff,
right?
We didn't have this B.
So I was telling you before that what happens is that once it fails on the B.
This other stuff kind of just remains in the keyboard buffer.
Which is pretty, pretty interesting.
So that's let's kind of mess around with.
Let's mess around with something else.
So.
If I was to enter stuff in a different order.
Let me see if I can come up with something interesting here.
If I was to.
Come up with.
Let me let me actually one change something in the code a little bit so.
Or actually what I'll do is I'll enter islands or something something else, so I'm going to do an A
for a char.
And then we will do a.
Let's see.
We will do.
A.
2.7, one, 10.
And then let's just say I also do a 100 year.
So we get our initial stuff that we expect, right, we get the a two point seven, a one and a 10,
and these are all read into the variables that we expect, right?
Because we had four of these variables the char, a float, one integer and then another integer.
And that's all represented here.
I also typed in this extra number, though.
So that's kind of interesting, right?
So let's say I go again and I have another scene in here and I'm just going to like, make you make
another variable up here.
I'm going to call this and I three.
And I put another C in here and I'm like, all right, you know, I did this like.
Scene right here and.
Let me actually I'm going to I'm going to put this after my print statement just to kind of like see
if I can prove the best point here, but I'm going to say, Oh no, I want to like CNY three.
So what I might be expecting when I make this program, as I say, Hey, OK, I'm going to read in these
four things or just whatever the user types, and then I'm going to print it out.
And then after that, I want to ask the user again for something.
So maybe I want to put another see out and I want to say, enter one more and write and do this on the
same line.
And then I'll say Seattle last and and I'll put a three.
So kind of cool, right?
So let's let's see what happens when I do something like this, though, so I'm going to go ahead and
save this.
So now we have like one more integer, right?
So what we're expecting is that we can enter a line of stuff.
And then we can print it out and then we can ask the user for one more integer and then it will prompt
them to enter it and they'll enter it, and then it will, you know, they'll press enter after they
type the integer.
And once they press, enter and then we'll print it out, right?
That's what should happen.
So let's go ahead and compile this.
Run it.
And if I go like this, let's say I got two point seven, one 10.
And then like, let's just say I accidentally like type an extra number on here.
I forget that it's really four and then I type like three integers.
So, look, this is a program just completely finished, actually.
So it did not prompt me to enter anything else, actually.
It just said enter one more end and then an end immediately printed this thing.
Right here, it's like it's like intro one more end.
And then we were expecting it to like, pause and let us enter something, but then it just immediately
printed last and 100.
Well, that's actually about 100 from right here.
And you want to know why it is printing this out without like pausing to ask us again.
That's because it's on the keyboard buffer.
It's some extra stuff on the buffer.
So we only asked it for four integers.
I'm sorry not for integers, we only use it for four different inputs.
Yeah, we accidentally entered a fifth input.
And so what happened was when this CNI three happened, we expected it to just let us let it let us
like have a pause and like, enter it, you know, it was supposed to stall.
Let us into something else, but it just grabbed whatever was on the keyboard buffer.
And this was on the keyboard buffer, this one hundred.
So that's why it grabbed it.
It did not pause for us and it immediately just printed it out.
So that is why it's important to know about this keyboard buffer.
So what can we do to kind of avoid some of this keyboard buffer stuff?
So I'm actually going to use my kind of go back and use a different example, so I'm not going to be
using this, but let's go back to what we had previously.
So I'm going to go ahead and say this, I'm going to come.
I saved it.
I'm going to compile it and run it again.
So let's enter argued on a two point seven.
But then we do the B and then we do 10, right?
So that's four things which is expected.
But the problem is, is that C and fails.
And I mean, look at this number now we have something totally different.
Look, it's a negative huge number.
So this is just another chance for me to prove to the point of these garbage values.
Look, it just picked up some other random huge number because i2 is not initialized.
In fact, none of these were initialized.
So if we had more integers that we were trying to read into, they would have garbage values like if
I put the i3 right here on the scene and it never made it to it, it would have some garbage value.