OK, so in this lecture, we are going to go over a different way to get user input.
So you might be wondering, well, why would we need a different way?
It seems like CNN is working fine for us.
And yes, the works great for a lot of cases, but there are some edge cases where scene does not work
so great.
And that's what we're going to discuss today in this lecture.
So these specific educations will involve us using something called get line.
And another thing called Get.
So a good example of what might lead us to use these sort of things is what if we wanted to read in
someone's full name?
So this would involve us reading into a string and reading into a string is something that I've hinted
at in previous lectures, but I have not done yet.
So let's go ahead and jump over to the editor and we will go ahead and go over that.
So.
I'm here in the ED. I have a little program that I've written, and I've actually already compiled it.
I have a string here that's called input.
So I declare variable string variable.
Then I have this little message that says, Enter your name.
Then I'm using scene to read into the string.
And then I just print the string out.
So it's just a program that tells someone to enter their name and spits it back out at them.
So renew a string is kind of cool.
We haven't really done that yet, right?
We've only read into characters and integers and floats and stuff like that.
But you actually can read into strings as well, because the string data Typekit someone created a bunch
of code for this and they made it nice.
So you can use this operator with this string type, the string variable.
So let's go ahead and throw with this.
I've already called this program.
I did see I'm going to go ahead and run it, and I'm just going to type one and type a first name right
now.
So I type the first name and it just spit it out back at me.
So you know that it read it in successfully.
So what if I want to put it first and last name this, I knew Dylan and whatever program we had, last
name, right?
Huh.
That's weird.
It actually still just spit out only my first name.
So that's a little weird.
Why did it do that?
Well, the reason that it did that is because CNN went into reading into a string.
It actually only reads up.
Intel encounters something called a white space character, which is a space or a new line or a tab.
Anything that's like blank space that you could press on the keyboard, which would be Space Bar tab
enter key creates a new line that stuff.
Those are considered white space characters, and so the scene is only going to read up until it encounters
a white space character.
And we don't want that right.
We want to grab this whole thing, the whole name.
So how can we do that?
Well, I will explain that on this slide, we're going to use something called the get line function.
So to fix the problem, we can use something called the get line function, and yes, this is me introducing
yet another function before explaining how to make functions.
We're very close to that, but I still need to introduce some more functions that you can just use before
making your own.
So the get line function is kind of similar to like doing the dot length function, except you're not
going to use a dot before it.
All you have to do is put get line and then the parentheses.
And what we're going to actually do is put stuff inside of the parentheses.
So we were interested in calling the dot length on a string variable, right?
If you're thinking about it in terms of like using the function with something like with the string
variable, this time what we're going to do is actually put the things we're interested in using get
line on inside these parentheses.
So those things are going to be separated by comma.
The first thing will be the C and object.
So you put it right here.
And the second thing after the comma is just going to be the variable that you want to read into.
So when we have seen we were reading it into that input string, right?
So all we're going to do now is just put C in and a comma and then put the input.
And that will be like the same thing as reading through the stream coming from console in into our input
variable or string variable.
But instead of just doing that scene greater than greater than or, yeah, greater than greater than
and the variable, then we're doing we're actually doing a function get line and then parentheses and
putting the things of interest inside here.
So how does it work, though, so let's take an example here, so this up here is what the keyboard
buffer looks like.
When I enter this example, this example input, so you notice we use get line here.
I do a scene and the input.
I basically just only replace that one line from the last program that had a scene with the opposite
of this symbol, you know, greater than greater than an input.
That that line has been replaced with this line, Line eight is now get line with the scene and input.
And so when I type blah space, blah, it doesn't stop on the space in between these, it grabs the
first word blah and the space and the second word blah.
And then what happens is it actually stops on in the new line character.
So it reads everything up until the new line character.
The new line character is here because we pressed enter at the end of this.
When I entered the name, I said, Blah space, blah, and I pressed enter.
And that added, when I press enter.
It added the new line on the end of all this stuff on the keyboard buffer.
And so when you get line past all this stuff and it hit the new line, it was like, Oh, I grab everything,
everything, everything hit a new line.
Not going to grab that, not going to save that on to the variable input, but I'm going to stop.
So it reads everything up to the new line and it reads the new line, but it does not put the new line
into your input.
If it did put the new line into the input, it would add like an extra new line on the output PowerShell
kind of automatically to this new line.
But if you were to run this in a different terminal, that's not PowerShell.
You could see that if you were to not add this in line, then it would still just smash up right against
the prompt right here and the prompt.
This whole prompt would not be on a new line because the new line character would not be added on to
the end of the input variable.
It would just simply be blah blah, and it would only have it in line if you added the inline when you
were doing SEO.
So definitely pretty interesting.
So let's go straight into a practice problem now that we have gone over this, so we're going to keep
rolling with the password program idea, but we're going to make another one of those programs where
the user needs to guess a password.
But this time we're going to take the user's input as a string.
We're also going to do the same thing and compare it to a hard coded string, which will be the password,
so you're going to come up with it yourself.
You'll be like string password equals and then some double quotes and whatever you want the password
to be called.
If the user enters something that is not long enough.
So kind of hinting at you needing to use a function, right?
Meaning you use a certain function to know how long the string is that the user enters, says the user
enters something that's not long enough.
He'd print out a message and is too short if they enter something too long.
You need to print out a message as too long if they enter something that's the right length.
But it's not the correct password you need to tell them it's the right length, but it's still wrong.
If the user does enter the right length and if the right password, you let them know.
So here's some example output here.
So says What's the password?
One two three four was entered.
That was too short, so it said too short.
Once again, we put safe password.
Is this this is too long?
Asks again if a password is says correct length, but it's the wrong password.
And then finally do safe pass, and it just says, you guessed it, because that is the correct password.
OK, cool, so this one might be a little bit challenging.
The solution that I'm going to show you after you've tried this yourself will involve.
A new thing or two and might involve something that you haven't necessarily done before, it's not really
a new concept, but it involves using the conditionals in an interesting way.
But I'm trying to challenge you to try and challenge you to figure it out.
I would really recommend that you do your absolute best no matter how long it takes.
You can just stop the video here and keep working on this until you can figure it out.
Do not immediately watch the solution unless you have completely exhausted all of your ideas on how
to solve this.
So with that, go ahead and pause.
Stop the video and try and solve this on your own.
OK, so now I am going to go on to the ED and show you a solution and kind of walk you through it and
explain it.
So I'm going back over here, but I have another file called Get Line, I'm going to clear the console
here.
So.
This is how I would have written this, how I did write it.
So first off, we come up with a password, right?
So here is a strong data type.
Here is our variable name called password, and we immediately initialize it with the assignment operator
to safe pass and double quotes.
Here we create a string that's just declared, but we don't initialize it.
We just declare a string called input, and we're going to read from the user into that with get line.
But before we do that, when you see out, we're going to say, what's the password as just a preliminary
message to the user.
At this point, we can then use the get line function and we can use sea in which his counsel and to
read into our variable or our input variable.
So you see that you.
So the next part is doing all the checks, right?
So remember this dot length function that is something that we have talked about before, and now it
comes in handy.
So I just kind of hardcoded the value here because this is eight, but you could also do something.
It would probably be better is to do password length because then if I wanted to change the password
to something else, I wouldn't have to go change my if stuff.
So we check real quick to see if the input is not equal to the password, but you might notice something
interesting here about this.
There's an if and then open bracket and then there is stuff like another if inside of it.
So this is kind of a new concept, this is something called nesting.
So when you put some type of code thing inside of.
Itself basically not itself, but you have the same type of thing inside.
So here we have an F and inside of the if we have another, if that is called nesting.
So we're nesting conditional statements.
So it's not only an if it's an f and else if and an else all contained inside this.
If so, let me kind of explain the structure here.
What we're trying to do is we first want to look at a specific condition, right?
We want to look at the condition in which the user has not guessed the password.
So that means that the input is not equal to the password.
So.
If this is true, then we want to now consider more conditions, right, because we want to display
specific answers depending on whether they have put too little amount of characters or too much.
So I'm also going to replace this with a password length.
So what we do is if they have not put in the correct password, then we are going to check again with
a different if and in fact, that's going to have some.
And if an infinite asked the first if is going to say it's less check if it's less characters than it's
supposed to be, in which case we will say to short.
Else, if so, only one of these is going to run, there's either a three different options, it's either
they put something that's too small, either they put something that's too big, which where we say
too long.
Else, which is the last condition that means it's not too big or it's not too small or too big, it's
the correct length.
But the fact is is that this LS, in fact, all this is contained in contingent upon this passing.
This if needs to pass.
So this means that it's definitely not the right password.
But when we get down here.
It's not too little or not too big, but it still isn't right.
So in the house, we're going to say correct length, but wrong password.
Then on this outer layer where we had the if you notice we put the closed bracket here for this, if
appear, we're going to have an elf that gets matched with this f, which is the opposite of this condition
right here.
So.
All of this stuff in here was what we wanted to happen.
You know, we would check each one of these only if they did not enter the right password down here,
though, there's an else that's on this same line, this indentation level, which is the opposite.
So if this actually is true, if the input is equal to the password.
So when I say true, I don't actually mean.
When I say true, in this instance, I'm saying if input equals equals password, not not input, not
equal to password.
So if this evaluates to true, all this stuff goes down in here.
But if this evaluates to false, then we jump straight to the else.
In which case it is the correct password input.
Equals equals password is what else represents.
And so we're going to say, you guessed it, so a little bit confusing that we have, you know, and
if and else if it else inside of another if, but really it's like layers of.
Conditions, right?
So it's kind of like you can think of it as a test, right?
OK, so we're going to give someone three tests if they.
If they don't pass the first test or something like that, you know, so let's say we want this condition
like, all right, we're going to give some people a test and if they fail.
The first test, then we're going to give and we're going to test him on three other things.
Three other conditions or something.
But if they do pass, it will immediately just say you passed.
So this is the case in which this test fails.
So if the test fails, then we want to initiate some other tests.
You know, this test right here is like, whom did they get it?
What did they put something to short?
OK.
Separately, maybe they put something that was too big.
OK, if it's neither of those, that means it's the same length, so we only went through all of this
logic if they failed.
Right off the bat, you know, they failed to enter the password, then we're going to do all this other
stuff in here.
If they got it correct, there's no need to ask these questions, right?
There's no need to do these checks.
We don't need to do these if else checks.
Because they got it right, we can immediately just be like, you guessed it.
That's why if this evaluates to false.
Then we're going to jump straight to the else and do this.
So this is kind of more an exercise almost unconditional than it was on the get line, but we still
incorporated the get line.
You notice the get line is a new and completely needed because we don't have any spaces in here.
But if we wanted, we could have something with spaces, right?
So what it could do is put a space in here and call it Safe Space Pass.
And then I do get like a copy.
And then I'm going to run, actually, I did not save this, so I have to re compile this.
What's the password?
And I say Safe Passage says it's too short because it's counting this space in between here.
It's not a anymore.
It's a nine one two three four five six seven eight nine.
So, you know, an inch of that.
And if I do safe path, then it says, you guessed it.
So pretty cool, that's probably, you know, this will this will make it so if there was a if a space,
it would be totally fine.
And of course, if it was, you know, it would accept it on the user input, it really is.
What the important part is is that when we're using seeing as the input, if we put a space, it's totally
fine and it's going to read that space.
OK, so if you were able to figure this out, definitely perhaps to you as always, but if not, you
know, don't worry, this is kind of a hard one, this nested conditional thing, but I'm kind of sprinkling
these into the problem.
So even if I have a figure now, it gives me an opportunity to mix some other concepts that we don't
necessarily need to have a whole video about, don't need to have a whole video about nesting conditions.
The fact is is you can just keep nesting conditions like, you know, over and over and over again.
Like you can just say, if you know, if a equals equals a, you know, some ridiculous thing like this,
then you know, if
false, you know, which wouldn't work.
But it's true.
And then like if you know, you could just put a ton of stuff like if, if, if, if, if, like so
deep into here, like you can just keep going.
We don't necessarily need that.
But the fact of the matter is you can do as much nesting of these as if you want in the LCF.
If you can also put in another, if in the else, you can put another if and you know, of course,
wherever you're putting these, if you can do more if and also so it can be just going forever.
You know, if something if you know something else and then else if and then and else here, you know,
you can keep structuring things like this.
So pretty cool, pretty interesting.
And go ahead and just delete this, because that's not necessary.
OK, so let's go ahead and head back to the lecture.
So congrats if you were able to figure this out, if not, don't worry, just go ahead and practice
that nesting of the F and LCF and stuff like that so you can get comfortable with it.
One last thing we're going to talk about is a cool little function called get.
So it basically is kind of like get line, except it just reads a single character.
So but like it line, it doesn't refuse the way it's fake whitespace characters.
This is kind of not completely true, right?
Because, uh, get line.
Does it stop on the new line?
And the new line is a slash in.
So you saw that up here, right?
New line character, I didn't explain this much, but when you press enter, it is kind of seen as this
slash and that's a new line, right?
So get line, it will not refuse like space presses, you know, when we see a space between something
kind of like, you know, this space like this.
But it will stop on the new line, the thing about ghetto, though, is get will actually grab that
new line character.
Unlike Get Line Get is going to actually take this and store it into a variable if you want.
It does not like stop on the new line.
It totally just like, you know, absorbs it and we'll read it into an input.
If you want a cool thing you can use.
Get for, though, is pausing your program.
We're not really going to be using a whole lot until we deal with reading the input from files that
will be separately using something called a file stream.
And we can use get for that read character by character and that get will not ignore any character.
Get is going to get any single character that can be pressed on your keyboard, basically, so it does
not exclude anything.
That's a cool thing about it.
So because of that is pretty handy to just start the program because and if you wanted to, you could
save the new line here and you could do that by putting a variable equals C and get so like using the
assignment operator, you could say like char.
GRC and then you can say C equals C and get something like that, you can also put the character in
the parentheses and pass to it.
But that's how you can call it, you can do a C and get.
That's the syntax.
It is one of the dot ones.
So you notice here in this little example, what it does is it basically pauses and waits for the entire
key to be pressed for it to continue.
So something we're not going to focus on a lot right now because we don't really need it, but we will
be looking at it later when we're reading input from files because it is useful for looking at character
by character, everything that's in a file.
So if the one way you can be, like, very, very meticulous in the way that you are checking all of
the contents of the file, something cool will get into later.
Right now, focus on using get line and see in and understanding the differences between those and when
you can use them.