WEBVTT

00:00.760 --> 00:09.820
So instead doing some regular practice problems for this section what we do sort of a mini project kind

00:09.820 --> 00:18.140
of review thing we've learned up to this point and including the file IO that we just learned.

00:19.910 --> 00:29.320
So let's take a look at the problem pretty program that will keep a list of contacts the contact will

00:29.320 --> 00:35.580
have a name of worsening last name and a phone number with no dashes.

00:36.070 --> 00:40.700
The program will read in all the saved contacts at the start of the program.

00:41.800 --> 00:50.390
User will be able to either look at all the contacts or add new contacts or quit.

00:50.560 --> 00:59.250
And once the contact is added it should be say the name should be no more than 256 characters.

00:59.320 --> 01:04.090
So let's start breaking down the problem.

01:04.660 --> 01:06.780
Are these the data of the problem.

01:07.060 --> 01:15.010
So I notice that we need some kind of contact and it will have a name.

01:15.680 --> 01:24.790
Well first name a last name and a phone numbers so that tells me that we should have a struct or a struct

01:24.880 --> 01:27.110
and call it contact.

01:30.610 --> 01:36.550
And it has a first name something that make a E-Strings a first name

01:40.840 --> 01:43.740
and you'll have Max doing 56 characters.

01:43.750 --> 01:51.440
In fact you need such an same size

01:54.410 --> 01:58.630
yes it's characters.

01:59.060 --> 01:59.560
Here

02:05.630 --> 02:08.130
so and do that for the last name

02:14.840 --> 02:19.590
is and says we have a phone number with no dashes.

02:19.590 --> 02:23.780
It just means integer number

02:26.710 --> 02:30.170
so we have an object called contact.

02:30.400 --> 02:33.160
And but we need a list of contacts.

02:33.220 --> 02:33.900
OK.

02:34.400 --> 02:41.370
And do something kind of interesting here since we need so I guess an array.

02:41.410 --> 02:48.340
That's the only thing we know right now that has that can store many values right now actually going

02:48.340 --> 02:51.700
to make a new struct

02:54.540 --> 02:58.090
called contact DB.

02:59.140 --> 03:00.150
OK.

03:00.510 --> 03:13.290
And this contact D-B which stands for database will have a pointer to contacts.

03:13.300 --> 03:28.350
Now be kind of the array it all contacts like so and we need to know how many contacts we have.

03:28.350 --> 03:38.880
So number of contacts and one of sweet thing it says it didn't say there's a limit to how many contacts

03:38.970 --> 03:39.950
we can have.

03:39.950 --> 03:40.510
Right.

03:40.770 --> 03:44.680
So we'll just kind of have to assume that there is no limit.

03:45.060 --> 03:56.490
And so to do this we're going to have a capacity of our contact array.

03:56.760 --> 04:04.140
So the contact array will have a number of contacts in how many this array was created for.

04:04.200 --> 04:14.100
So if we create say a thousand capacity would be a thousand even if we only had a 10 actual contacts.

04:14.550 --> 04:19.380
And this is actually the type that we're going to use in order to

04:21.950 --> 04:24.290
hold all our contacts.

04:25.890 --> 04:29.750
So that's really it for the data.

04:29.790 --> 04:31.840
Essentially.

04:32.240 --> 04:36.690
So let's start I guess breaking down the problem.

04:37.640 --> 04:48.000
And I kind of want to we'll kind of do this without really doing a real top down design.

04:48.230 --> 04:53.500
Okay but you'll see kind of how we go about it.

04:53.720 --> 04:58.240
It'll still sort of look like a top line but we'll actually write the code.

04:58.610 --> 05:01.700
So essentially what this is saying is

05:04.780 --> 05:13.210
so user will be able to either look at all their contacts or add new contacts they or quit the app.

05:13.330 --> 05:16.270
And so these are the things that the user is able to do.

05:17.280 --> 05:25.380
K so there's three options that they're able to do so that's actually

05:28.540 --> 05:32.750
right out kind of the baseline of everything.

05:32.770 --> 05:38.140
So let's say we need the option that they can do

05:42.110 --> 05:45.310
and we'll go through

05:48.190 --> 05:50.230
and while the option

05:53.290 --> 05:57.290
is not it's quit of quit odysseys.

05:57.310 --> 06:06.540
Q And we'll kind of do the option or get the option from these areas.

06:10.660 --> 06:21.140
User and we will kind of execute this option game or do the option

06:24.410 --> 06:26.150
again we don't have implementations for this.

06:26.150 --> 06:30.280
We're just sort of designing how it's going to work.

06:31.760 --> 06:34.220
Using the option.

06:36.200 --> 06:40.320
And of course we'll probably need our contactee.

06:40.350 --> 06:41.050
So

06:43.710 --> 06:46.110
this is called contact.

06:46.110 --> 06:47.230
It's the

06:55.130 --> 06:55.610
X

06:58.210 --> 07:04.450
the like so and initially it will be empty.

07:05.080 --> 07:06.070
X TV.

07:06.090 --> 07:15.490
So Dot we don't have any right because we haven't read Minya and

07:18.500 --> 07:23.340
you set the size number contact's zero.

07:25.210 --> 07:27.320
And capacity currently zero.

07:27.430 --> 07:28.220
Right.

07:28.270 --> 07:34.960
The very start of her and we don't have anything so everything's kind of just default.

07:37.000 --> 07:44.600
So it says at the start of the program it will read in the list of contacts.

07:45.950 --> 07:53.340
So we'll need some way of loading all of our current contacts if it exists.

07:53.340 --> 08:04.150
So at this start we'll say lude on tax

08:06.520 --> 08:07.840
contacts

08:10.510 --> 08:15.770
the OK so will need to load it somehow.

08:15.820 --> 08:16.270
Right.

08:20.700 --> 08:22.390
Ask Nizer.

08:22.660 --> 08:29.260
OK so we load the contacts will get the option from the user and then we'll just keep the option.

08:29.260 --> 08:36.580
But what I guess will need to also pass in the contacts range because what if they want to add something

08:37.680 --> 08:40.380
and will need context.

08:41.030 --> 08:45.310
So this is another way you can sort of design your program.

08:45.320 --> 08:50.810
It doesn't always have to be the top down design method that we've been use and you know that's kind

08:50.810 --> 08:53.890
of what we're doing right now anyway.

08:54.170 --> 08:59.780
It just means that we don't have to always kind of write out all the pseudo code first and then then

08:59.780 --> 09:06.860
code after which is kind of going against what I told you before but that was really to help you solve

09:06.860 --> 09:07.760
the problem.

09:07.850 --> 09:10.140
And we're still solving the problem.

09:10.160 --> 09:16.820
So right now we're just sort of kind of writing B.

09:17.990 --> 09:25.220
The main function and that'll kind of guide us to you what everything what what we need to do for to

09:25.220 --> 09:26.560
actually solve the problem.

09:26.730 --> 09:33.440
OK so now we have these kind of functions and are not intimate at all really yet.

09:33.440 --> 09:34.020
Right.

09:34.160 --> 09:36.290
That's all this is all we've done.

09:36.320 --> 09:37.230
Not too much yet.

09:38.130 --> 09:46.030
So you need to get the option from the user and execute the option and load context right.

09:47.560 --> 09:50.770
So we'll need those functions.

09:50.810 --> 09:54.410
So you just declare them so.

09:54.610 --> 10:00.220
Option from user will be character or return of character.

10:00.220 --> 10:03.280
This will just be our get character function.

10:05.410 --> 10:05.900
There

10:10.130 --> 10:13.720
they execute option or turn nothing.

10:13.730 --> 10:32.700
And it will take us CH and it will take the option and the contacts Stevy probably by reference ranks

10:33.210 --> 10:33.990
could change

10:41.500 --> 10:50.360
so and we also need load contex.

10:50.810 --> 10:57.110
We're actually going to make it Boullion and going to keep building because what if the file doesn't

10:57.110 --> 10:58.570
exist yet.

10:59.450 --> 11:00.200
So

11:03.360 --> 11:18.510
X is also the right reference.

11:18.570 --> 11:23.750
And let's kind of just to some of the it's

11:31.910 --> 11:36.050
not going to rate everything at this kind of rates.

11:36.060 --> 11:39.380
And some of the simple ones.

11:39.600 --> 11:55.410
So this option from user We just want to call and everready included the huge hills to peer mood tunes

11:55.680 --> 11:57.240
h Gach

11:59.970 --> 12:05.500
and you should actually get it OK.

12:05.580 --> 12:09.200
So we're going to have three options.

12:09.210 --> 12:15.660
So cons are options for the proper input.

12:16.090 --> 12:23.290
Say d is for display everything a is for adding and for.

12:26.440 --> 12:27.830
A return.

12:29.140 --> 12:30.480
Get character

12:34.020 --> 12:44.500
the prompt will be please choose option.

12:44.840 --> 12:50.930
New lines in here and I'll just say I like D for display.

12:54.100 --> 12:55.090
Contact's

12:57.350 --> 13:14.700
in a we're adding contacts and Hugh.

13:14.870 --> 13:18.840
A What is your choice.

13:22.180 --> 13:22.760
I like that.

13:22.920 --> 13:30.070
So I remember that slash and is for a new lines like the Entergy essentially.

13:30.130 --> 13:35.130
So the next thing we need is the error strings right.

13:36.440 --> 13:42.280
Such and such for string

13:46.220 --> 13:49.880
you're.

13:50.570 --> 13:54.010
Again so

14:01.910 --> 14:12.330
that in and of course all the options three of them and we'll say C C lowercase

14:15.010 --> 14:20.430
and it's lowercase because you use lowercase Q here for quit.

14:21.680 --> 14:23.080
It's Y.

14:23.310 --> 14:26.180
So that's kind of it for this.

14:26.180 --> 14:29.330
So let's just run this and see

14:33.310 --> 14:40.330
that.

14:40.330 --> 14:42.760
So what is your choice.

14:42.760 --> 14:48.160
A Nothing happens D nothing happens.

14:48.160 --> 14:55.970
Q It's gay that's for that and let's rate execute option.

14:58.090 --> 15:02.300
So here we'll do a switch on the options

15:06.460 --> 15:16.230
have a case for adding a and for this will say well need new function.

15:16.240 --> 15:18.160
I'll just add option

15:21.650 --> 15:22.110
and

15:26.130 --> 15:26.670
Imbert

15:30.150 --> 15:41.010
and don't need another one for displaying the same display and contacts

15:46.490 --> 15:54.930
so notice how we're kind of doing the stuff this now.

15:55.160 --> 16:00.180
So we're kind of doing the design of this kind of piecemeal right.

16:00.280 --> 16:04.860
So we just kind of said oh I'll probably need something like this for I get the option for Muser and

16:04.860 --> 16:10.390
then I have to actually do the option from the user right and then we'll do that while he doesn't want

16:10.390 --> 16:11.340
to quit.

16:11.410 --> 16:11.980
Right.

16:12.100 --> 16:18.640
So that's how we kind of came up with this main loop and same with like this all we need to somehow

16:18.640 --> 16:22.600
read it into the actual context.

16:22.970 --> 16:25.690
Right no we haven't done that yet.

16:25.720 --> 16:33.340
And similarly here next cute option we need some way of adding a contact and it's a way of displaying

16:34.000 --> 16:35.270
all the contacts.

16:35.270 --> 16:36.090
Right.

16:36.340 --> 16:41.220
So we're just kind of doing this piecemeal and what we think makes sense.

16:41.710 --> 16:49.150
So we're still sort of doing this top down design we're just not writing at all in pseudo code for us

16:49.180 --> 16:53.230
we're just kind of writing code as we kind of think of it.

16:55.520 --> 16:59.580
So we need to add option and display option.

16:59.630 --> 17:07.040
So it's this or read option

17:09.820 --> 17:16.200
and it's going to take the context D-B by reference.

17:16.730 --> 17:18.640
Change the contacts there.

17:20.790 --> 17:22.590
And we need display.

17:22.740 --> 17:30.230
It's display option.

17:30.380 --> 17:33.590
This will be by conc because we don't have to change it.

17:43.580 --> 17:44.690
Copy this

17:48.620 --> 17:49.980
piece it down here.

17:55.050 --> 17:55.750
OK.

17:56.290 --> 18:03.160
So let's kind of implement the display option should be quite simple.

18:03.820 --> 18:10.360
So we just loop through all of our contacts and then display contact.

18:10.500 --> 18:17.500
So 4 0 is less than an hour.

18:17.810 --> 18:23.480
It's the number of contacts it's right.

18:26.990 --> 18:31.400
And all we do is just put everything in the name

18:33.680 --> 18:34.640
is

18:37.330 --> 18:42.020
going to tax the rich winter.

18:42.530 --> 18:46.820
First rate.

18:47.030 --> 18:53.460
And that's because this kind of this is called the indexing operator.

18:53.510 --> 18:54.730
Right.

18:54.950 --> 18:58.190
That we've been using for arrays and this actually will dereferences.

18:58.190 --> 19:01.090
So that's why we don't need a star or a star.

19:01.100 --> 19:04.910
But this thing is Arrow right.

19:05.100 --> 19:06.800
You reference it it's say Dot

19:11.830 --> 19:13.540
and we'll do that for the last few

19:27.860 --> 19:28.550
mean and

19:31.760 --> 19:34.260
then we'll put the phone numbers on

19:41.400 --> 19:42.040
there

19:57.160 --> 20:04.880
and we'll put two initials here because we want a space between each entry.

20:05.910 --> 20:09.090
And that's really it for display option really.

20:09.090 --> 20:12.560
So again we're just sort of doing this all piecemeal right.

20:12.690 --> 20:18.370
Or just kind of taking a little piece of work and then reading that code the way we think it should

20:18.390 --> 20:22.940
work and then we're moving on to the next part right.

20:24.000 --> 20:29.420
So add option it's go with this.

20:29.610 --> 20:35.160
This will be a little bit more complicated I think.

20:35.230 --> 20:41.570
So like I said before in the program it never said that we could have.

20:41.710 --> 20:47.220
There's no upper limit to how many contacts we can have.

20:47.410 --> 20:48.030
OK.

20:49.620 --> 20:59.490
So that means that we need to grow our contacts array any time that we're at the maximum.

21:00.730 --> 21:04.960
So here now let's say if

21:07.570 --> 21:23.500
the contacts D-B dot the number of contacts is somehow equal to the capacity then we somehow need to

21:23.530 --> 21:30.460
resize this whole this whole contacts D.B right we need to grow it to be bigger.

21:30.490 --> 21:33.570
So we can add more contacts.

21:34.060 --> 21:40.730
So say resign as contact us Eby.

21:40.980 --> 21:49.280
And so we're just kind of kind of eyeballing this right we're kind of just like going off the cuff a

21:49.280 --> 21:50.260
little bit.

21:50.260 --> 21:56.540
We're just saying Oh like we see a problem and then we just kind of I'm just writing a function that

21:56.690 --> 22:04.880
doesn't exist yet and we're saying OK it's going to it's just going to resize this thing somehow we

22:04.880 --> 22:06.010
don't know yet.

22:06.090 --> 22:06.500
Right.

22:06.860 --> 22:08.750
So how much should we need.

22:08.810 --> 22:12.430
Well need the context to me obviously that's what resizing.

22:12.470 --> 22:15.280
So how much should we resize it by.

22:15.300 --> 22:18.710
We could just add one right too.

22:18.770 --> 22:22.790
So we just make capacity one more right.

22:22.970 --> 22:30.670
But instead of that because we don't want to resize it all the time are actually going to say contacts.

22:30.710 --> 22:34.540
TB dot capacity.

22:34.820 --> 22:44.300
And when you say times 2 and then plus 10 so we'll multiply whatever the capacity is times 2 and then

22:44.300 --> 22:46.570
just add 10 more.

22:46.760 --> 22:47.330
Okay.

22:47.630 --> 22:52.660
And I'm doing this because what if the capacity with zero.

22:52.900 --> 22:53.860
Right.

22:54.170 --> 22:56.960
Which is which it is at the very start.

22:57.030 --> 22:57.460
Right.

22:58.780 --> 23:07.100
And what if it was zero then contacts D.B capacity which is zero times to zero.

23:07.300 --> 23:10.610
So if I didn't have this plus 10 they would still amount to zero.

23:10.620 --> 23:13.220
So that's why I'm kind of just adding a few more.

23:13.340 --> 23:18.660
OK so what does resize.

23:18.850 --> 23:28.360
And then once it's resize and everything's good then we will actually get all the input.

23:28.360 --> 23:40.250
So the next thing we need to do is actually get the first name and last name so don't have a way in

23:40.270 --> 23:48.510
you to actually get a string although we've done that in hangman before.

23:48.650 --> 23:50.180
Right.

23:50.270 --> 23:59.780
So what I've done you tildes is I've actually added a method called Get string and I I took this directly

23:59.780 --> 24:09.170
from hangman and I use sort of the same signature as get character with like the valid input or not

24:09.170 --> 24:16.820
that out and put it like the scared character with the prompt in the air and actually just feel this

24:16.820 --> 24:17.950
string.

24:18.080 --> 24:23.090
And with some maximum.

24:23.180 --> 24:32.910
So if you want to take a look at how I implemented this it you shouldn't be surprised what's happening.

24:32.910 --> 24:33.180
Right.

24:33.200 --> 24:41.730
So we get the string string which is named the function a failure we have a length.

24:41.750 --> 24:52.740
Again I just stole this basically exactly from hangman and the one thing I added was this line.

24:52.770 --> 24:57.560
So by the way if you want to copy this down just pause the video and then copy the whole thing.

24:57.660 --> 25:08.290
OK so the one thing I did add that that's new is this line right here which will kind of get the whitespace.

25:08.290 --> 25:15.120
This is actually a special thing that kind of comes with the scene in I'll stream and this will get

25:15.150 --> 25:20.060
all the white space before we actually get the string.

25:20.580 --> 25:22.960
It's kind of weird why we need to do this.

25:23.340 --> 25:33.140
But sometimes I guess the input stream really doesn't clear properly once we get once we call this get

25:33.400 --> 25:35.650
function k.

25:35.790 --> 25:37.790
So that's why we kind of need it here.

25:37.800 --> 25:44.970
So just remember to put this whitespace see in whitespace here.

25:45.200 --> 25:50.020
Otherwise I've also added this doesn't matter too much I don't think.

25:50.030 --> 25:53.460
But I bet it doesn't matter.

25:54.410 --> 25:59.720
And everything else should be the same in all it does is turn the length of the string that you got

26:00.100 --> 26:02.060
and that's it.

26:02.060 --> 26:06.830
So this is directly from Hank except for this part.

26:07.340 --> 26:09.290
So let's go back.

26:09.290 --> 26:12.920
So here I wanted to say get string

26:15.380 --> 26:21.190
and or say please enter first name

26:24.680 --> 26:28.340
and input it's it's

26:31.050 --> 26:32.280
air string

26:34.590 --> 26:44.270
Turks TV your contacts so are actually going to change this array directly.

26:45.730 --> 26:57.280
And this should be contacts D-B DOT number of contacts so put it in the next available slot is what

26:57.280 --> 26:58.470
this is saying.

26:58.540 --> 26:59.220
Right.

26:59.350 --> 27:08.380
Because if even if we had zero as long as we had some as long as we actually had a positive capacity

27:09.150 --> 27:14.210
I would just add it to the next available slot.

27:14.290 --> 27:16.490
And even if there's a zero this would work.

27:17.410 --> 27:26.420
So first name that's the string and then Max Nemes size as well.

27:27.690 --> 27:28.310
OK.

27:28.480 --> 27:31.450
And again minus one because we're not here.

27:31.960 --> 27:45.810
This puts the knot here and there it was a string these enter the last name.

27:47.660 --> 27:50.370
As your string

27:54.500 --> 27:55.130
xt.

27:55.170 --> 27:59.300
The pointer X

28:06.750 --> 28:09.020
contacts and last

28:21.280 --> 28:25.100
and then we'll just say see each

28:29.590 --> 28:30.490
actually will say

28:33.160 --> 28:36.220
get integer.

28:37.090 --> 28:43.780
So we'll just see an integer here please.

28:46.110 --> 28:47.240
Phone number

28:50.050 --> 28:52.230
is in contact.

28:52.540 --> 28:54.890
It's.

29:05.430 --> 29:11.710
Number of contacts phone number OK.

29:11.770 --> 29:23.050
So that will put the whatever the user input into our into the next available phone numbers.

29:23.790 --> 29:29.570
And then we'll just add one to the number of contacts.

29:29.770 --> 29:31.390
It's the

29:34.400 --> 29:36.860
kinda plus.

29:36.970 --> 29:48.020
And then we'll save contacts and again we do not have this function right.

29:49.750 --> 29:57.220
But but we know we need to save here because in the problem once the contact is added it should be saved.

29:57.380 --> 29:58.200
OK.

29:58.630 --> 30:00.540
So again I'm just I know I'm not doing it.

30:00.540 --> 30:02.470
Top down design for this.

30:02.470 --> 30:09.660
I'm just kind of going off the cuff and just writing the code that I think we will need right.

30:09.710 --> 30:11.830
You know it's just.

30:11.900 --> 30:18.350
I'll just say to the user say.

30:18.910 --> 30:22.050
So I want to call it quits for this lecture.

30:22.060 --> 30:28.780
But in the next lecture we will actually implement the resize all the contacts and we'll implement the

30:28.780 --> 30:33.340
saved contacts and load decks.

30:33.700 --> 30:35.730
So I'll see you in the next lecture.
