So to return back to input because now we have the tools necessary to start talking about input errors.
So let's say we hand this example here.
I say number one is to know three and in roots and say please input three integers.
Yes these.
Between
rows can we get them
and we're going to really output.
It's a very simple program.
OK.
So let's run this in ch should see.
So let's say we get 30 20 and 10.
So the integers you input were 30 20 and 10 so far so good.
So let's say we had 30 Fred and 10.
So Fred is clearly not an integer right.
So let's see what happens if we do this.
So the integers you input were 30 zero and zero.
So why did this happen.
Well it's because we didn't get the proper input for number two because there was a type mismatch.
So Fred is non-integer right.
And because we we got a type mismatch.
This actually puts our CNN into a failed state and once it's in a failed state it doesn't read input
anymore.
So that's why we don't get you know the 10 at least for the number three right because we input the
first and last numbers correctly.
But we didn't input the second one correctly.
So as soon as there is some kind of failure with C in it it propagates to everything.
So now we can't read any input at all.
So how do we detect when we're in this failed state.
Well to do that we actually have to use something called seen datt fail.
So rewrite this a little bit and say If
CN fail this this really says If CN is in a failure state.
Right.
Remember that if you have some kind of type mismatch then that'll put C in into a failure state.
Then then we have to do something.
So how do we fix this failure state.
So to do that we have to say see in clear and see and clear does is it really just takes away the failure
state it puts it back into hey you can read again that's all it's really saying.
You can you can read again from CNN or the cons..
OK.
So let's do that and let's try to read everything again.
Nice coffee this
settlers just going to do this check after each time we read something from the input
in see what happens.
30 Fred and 10.
So nothing changed.
Sure.
And this is because like I said all clear does is clear is essentially a flag in C in that says hey
you can read again but it didn't actually take out the offending input.
In this case Fred.
So we actually have to take out the input that is messing us up and how we can do that is to say see
in a more
answers and say See.
Ignore.
So what this does is it ignores from where it's reading from or where it's currently at it's going to
ignore 256 characters or up to a space so it would either ignore 256 characters or up to a space so
we're putting everything between with spaces.
So we're putting everything with spaces in between.
So this should work pretty nicely.
So let's just copy this
image going to put that after each clear and it has to be after that clear because otherwise it won't
it won't accept any operations we see in the wires.
So let's run this mercy 30 Fred and 10.
And now it is at least had at least read the last number.
So it failed on Fred again.
But at least it recovered by saying right here is the name the next number.
Number two said yes I'm in a failure state it cleared the failure state and then ignored up until the
space ignored.
Every character up until this space right before the 10.
And then that fixed it so seeing could read at least the last number.
So this is still not really ideal because it still skips number two.
So be what we think you really want is to riprap the user every time they make a mistake.
So any time they make some kind of silly mistake we want to riprap the user and say hey can you please
try again please try again.
So let's kind of rewrite this whole thing and let's try to use a loop in order to do this.
So let's say it numb in the pool feel your
numbers are b your input and explain the failure in the second.
So you can use a do while loop.
So this is going to be quite useful for us.
While failure
so failure is if there was an input failure we want to try again.
So if we have a failure in the input We'll set the failure to be true and that will really or will redo
this whole loop.
It's a failure going to set to false at the beginning because there is no failure yet.
And we're going to prompt.
So please enter a number it's in Numb
Arend do the same thing we did for if there was a failure in C in say C and that clear clear the failure
and ignore
it.
You want a constant here actually costs to inch nor characters to six of them.
Nor dares.
And instead of a space we're going to use the Enter key because there's only going to be one input to
be read here.
So we don't we don't need spaces here and we're going to set the failure to be true because there was
a failure.
Okay.
And then we're output to the user that there was an input error
error and we're going to say please try again.
So notice that we're using a do while loop and that's because we want to prompt the user at least once.
Right.
So we want to do this this whole thing one time at least OK.
So this is why do Weill's are actually useful.
They're very useful in this case and in this course for input from the user because the user could mess
up.
So we want to get the input checked to see if there is a failure with that inputs.
So say he so say we wanted a number but he typed Fred that would have it.
That would be a type mismatch because characters are not numbers.
And then we would clear that failure and ignore the characters up until they hit the enter key.
So that's I you know you always have to press the enter key and we just set the failure to be true.
And we just say hey please try again.
Then output
number you in the head most no.
So we know at this point online 35 that there was no failure at this point.
Thus we just put the number they that they that they input inputted.
So let's run this and see what happens.
So say Fred
and see we have actually a formatting problem.
So we've got to have the Endal here.
So let's put that there stop
and put it number Fred.
So please try again.
Let's try it again.
See Fred again not letting us write any kind of character put.
It's not allowing us to do that.
OK.
So it's just 10 a the input was 10 right.
So this is one nice thing about do while loops is that for input you always kind of want to do it at
least one time.
Right.
Because you always want to give the user a chance to input something if they mess up though that we
can actually do it again.
OK.
So in the next next lecture we'll do some practice problems using GPS.