WEBVTT

00:00.860 --> 00:09.650
So to return back to input because now we have the tools necessary to start talking about input errors.

00:09.950 --> 00:13.160
So let's say we hand this example here.

00:13.180 --> 00:31.000
I say number one is to know three and in roots and say please input three integers.

00:34.330 --> 00:37.570
Yes these.

00:37.580 --> 00:38.040
Between

00:50.320 --> 00:51.630
rows can we get them

00:54.390 --> 00:57.980
and we're going to really output.

00:58.070 --> 01:00.050
It's a very simple program.

01:30.330 --> 01:31.170
OK.

01:31.930 --> 01:38.200
So let's run this in ch should see.

01:38.200 --> 01:41.620
So let's say we get 30 20 and 10.

01:41.620 --> 01:48.450
So the integers you input were 30 20 and 10 so far so good.

01:48.680 --> 01:52.960
So let's say we had 30 Fred and 10.

01:53.390 --> 01:57.750
So Fred is clearly not an integer right.

01:57.860 --> 02:01.740
So let's see what happens if we do this.

02:01.770 --> 02:07.960
So the integers you input were 30 zero and zero.

02:08.020 --> 02:11.150
So why did this happen.

02:11.260 --> 02:19.390
Well it's because we didn't get the proper input for number two because there was a type mismatch.

02:19.390 --> 02:22.860
So Fred is non-integer right.

02:23.350 --> 02:28.040
And because we we got a type mismatch.

02:28.180 --> 02:38.740
This actually puts our CNN into a failed state and once it's in a failed state it doesn't read input

02:38.740 --> 02:39.330
anymore.

02:39.340 --> 02:49.090
So that's why we don't get you know the 10 at least for the number three right because we input the

02:49.090 --> 02:52.300
first and last numbers correctly.

02:52.300 --> 02:55.170
But we didn't input the second one correctly.

02:55.360 --> 03:05.770
So as soon as there is some kind of failure with C in it it propagates to everything.

03:06.190 --> 03:09.450
So now we can't read any input at all.

03:09.820 --> 03:14.910
So how do we detect when we're in this failed state.

03:15.160 --> 03:22.790
Well to do that we actually have to use something called seen datt fail.

03:23.860 --> 03:27.040
So rewrite this a little bit and say If

03:30.050 --> 03:39.650
CN fail this this really says If CN is in a failure state.

03:39.650 --> 03:40.670
Right.

03:40.850 --> 03:49.020
Remember that if you have some kind of type mismatch then that'll put C in into a failure state.

03:49.350 --> 03:52.570
Then then we have to do something.

03:52.570 --> 03:57.280
So how do we fix this failure state.

03:57.280 --> 04:11.690
So to do that we have to say see in clear and see and clear does is it really just takes away the failure

04:11.690 --> 04:17.340
state it puts it back into hey you can read again that's all it's really saying.

04:17.360 --> 04:23.040
You can you can read again from CNN or the cons..

04:23.540 --> 04:24.470
OK.

04:24.470 --> 04:31.450
So let's do that and let's try to read everything again.

04:31.590 --> 04:32.780
Nice coffee this

04:37.970 --> 04:45.220
settlers just going to do this check after each time we read something from the input

04:53.480 --> 04:55.660
in see what happens.

04:58.020 --> 05:01.590
30 Fred and 10.

05:02.310 --> 05:04.710
So nothing changed.

05:04.750 --> 05:05.750
Sure.

05:06.390 --> 05:19.200
And this is because like I said all clear does is clear is essentially a flag in C in that says hey

05:19.380 --> 05:28.200
you can read again but it didn't actually take out the offending input.

05:28.200 --> 05:29.820
In this case Fred.

05:30.180 --> 05:41.060
So we actually have to take out the input that is messing us up and how we can do that is to say see

05:41.060 --> 05:42.710
in a more

05:46.140 --> 05:49.610
answers and say See.

05:49.690 --> 05:51.640
Ignore.

05:51.680 --> 06:01.280
So what this does is it ignores from where it's reading from or where it's currently at it's going to

06:01.280 --> 06:14.880
ignore 256 characters or up to a space so it would either ignore 256 characters or up to a space so

06:14.880 --> 06:19.520
we're putting everything between with spaces.

06:24.300 --> 06:27.330
So we're putting everything with spaces in between.

06:27.330 --> 06:31.100
So this should work pretty nicely.

06:31.170 --> 06:34.020
So let's just copy this

06:37.460 --> 06:44.000
image going to put that after each clear and it has to be after that clear because otherwise it won't

06:44.570 --> 06:48.200
it won't accept any operations we see in the wires.

06:51.200 --> 06:59.040
So let's run this mercy 30 Fred and 10.

06:59.240 --> 07:08.710
And now it is at least had at least read the last number.

07:09.250 --> 07:12.270
So it failed on Fred again.

07:12.490 --> 07:20.040
But at least it recovered by saying right here is the name the next number.

07:20.070 --> 07:26.260
Number two said yes I'm in a failure state it cleared the failure state and then ignored up until the

07:26.260 --> 07:27.440
space ignored.

07:27.460 --> 07:34.560
Every character up until this space right before the 10.

07:34.810 --> 07:42.650
And then that fixed it so seeing could read at least the last number.

07:42.660 --> 07:50.430
So this is still not really ideal because it still skips number two.

07:50.670 --> 08:00.740
So be what we think you really want is to riprap the user every time they make a mistake.

08:00.830 --> 08:08.690
So any time they make some kind of silly mistake we want to riprap the user and say hey can you please

08:08.690 --> 08:11.630
try again please try again.

08:11.960 --> 08:20.760
So let's kind of rewrite this whole thing and let's try to use a loop in order to do this.

08:22.070 --> 08:29.100
So let's say it numb in the pool feel your

08:32.120 --> 08:38.100
numbers are b your input and explain the failure in the second.

08:38.180 --> 08:41.390
So you can use a do while loop.

08:41.480 --> 08:45.650
So this is going to be quite useful for us.

08:45.820 --> 08:47.170
While failure

08:50.160 --> 08:56.410
so failure is if there was an input failure we want to try again.

08:57.270 --> 09:07.110
So if we have a failure in the input We'll set the failure to be true and that will really or will redo

09:07.110 --> 09:09.000
this whole loop.

09:09.380 --> 09:14.940
It's a failure going to set to false at the beginning because there is no failure yet.

09:16.780 --> 09:18.210
And we're going to prompt.

09:18.440 --> 09:29.700
So please enter a number it's in Numb

09:34.400 --> 09:42.570
Arend do the same thing we did for if there was a failure in C in say C and that clear clear the failure

09:43.930 --> 09:45.580
and ignore

09:50.630 --> 09:51.780
it.

09:52.120 --> 10:02.550
You want a constant here actually costs to inch nor characters to six of them.

10:07.110 --> 10:10.080
Nor dares.

10:10.120 --> 10:16.960
And instead of a space we're going to use the Enter key because there's only going to be one input to

10:16.960 --> 10:18.550
be read here.

10:19.530 --> 10:25.930
So we don't we don't need spaces here and we're going to set the failure to be true because there was

10:25.930 --> 10:27.300
a failure.

10:27.300 --> 10:27.900
Okay.

10:28.210 --> 10:34.170
And then we're output to the user that there was an input error

10:36.660 --> 10:47.720
error and we're going to say please try again.

10:48.380 --> 10:57.960
So notice that we're using a do while loop and that's because we want to prompt the user at least once.

10:58.380 --> 10:58.800
Right.

10:58.940 --> 11:05.350
So we want to do this this whole thing one time at least OK.

11:05.410 --> 11:08.400
So this is why do Weill's are actually useful.

11:08.440 --> 11:16.150
They're very useful in this case and in this course for input from the user because the user could mess

11:16.150 --> 11:17.430
up.

11:17.530 --> 11:24.690
So we want to get the input checked to see if there is a failure with that inputs.

11:24.910 --> 11:30.720
So say he so say we wanted a number but he typed Fred that would have it.

11:30.760 --> 11:34.950
That would be a type mismatch because characters are not numbers.

11:35.050 --> 11:44.780
And then we would clear that failure and ignore the characters up until they hit the enter key.

11:45.040 --> 11:50.830
So that's I you know you always have to press the enter key and we just set the failure to be true.

11:51.160 --> 11:55.050
And we just say hey please try again.

11:55.500 --> 11:56.640
Then output

11:59.810 --> 12:09.660
number you in the head most no.

12:10.250 --> 12:17.540
So we know at this point online 35 that there was no failure at this point.

12:18.540 --> 12:23.840
Thus we just put the number they that they that they input inputted.

12:23.970 --> 12:26.660
So let's run this and see what happens.

12:27.120 --> 12:28.230
So say Fred

12:32.190 --> 12:35.700
and see we have actually a formatting problem.

12:37.010 --> 12:39.170
So we've got to have the Endal here.

12:39.350 --> 12:42.720
So let's put that there stop

12:46.050 --> 12:48.580
and put it number Fred.

12:48.730 --> 12:51.370
So please try again.

12:51.400 --> 12:53.130
Let's try it again.

12:53.230 --> 13:01.020
See Fred again not letting us write any kind of character put.

13:01.120 --> 13:03.530
It's not allowing us to do that.

13:03.910 --> 13:04.900
OK.

13:04.930 --> 13:09.340
So it's just 10 a the input was 10 right.

13:10.300 --> 13:19.650
So this is one nice thing about do while loops is that for input you always kind of want to do it at

13:19.650 --> 13:21.110
least one time.

13:21.380 --> 13:21.920
Right.

13:22.110 --> 13:27.570
Because you always want to give the user a chance to input something if they mess up though that we

13:27.570 --> 13:29.860
can actually do it again.

13:31.050 --> 13:31.880
OK.

13:32.150 --> 13:37.890
So in the next next lecture we'll do some practice problems using GPS.
