So instead doing some regular practice problems for this section what we do sort of a mini project kind
of review thing we've learned up to this point and including the file IO that we just learned.
So let's take a look at the problem pretty program that will keep a list of contacts the contact will
have a name of worsening last name and a phone number with no dashes.
The program will read in all the saved contacts at the start of the program.
User will be able to either look at all the contacts or add new contacts or quit.
And once the contact is added it should be say the name should be no more than 256 characters.
So let's start breaking down the problem.
Are these the data of the problem.
So I notice that we need some kind of contact and it will have a name.
Well first name a last name and a phone numbers so that tells me that we should have a struct or a struct
and call it contact.
And it has a first name something that make a E-Strings a first name
and you'll have Max doing 56 characters.
In fact you need such an same size
yes it's characters.
Here
so and do that for the last name
is and says we have a phone number with no dashes.
It just means integer number
so we have an object called contact.
And but we need a list of contacts.
OK.
And do something kind of interesting here since we need so I guess an array.
That's the only thing we know right now that has that can store many values right now actually going
to make a new struct
called contact DB.
OK.
And this contact D-B which stands for database will have a pointer to contacts.
Now be kind of the array it all contacts like so and we need to know how many contacts we have.
So number of contacts and one of sweet thing it says it didn't say there's a limit to how many contacts
we can have.
Right.
So we'll just kind of have to assume that there is no limit.
And so to do this we're going to have a capacity of our contact array.
So the contact array will have a number of contacts in how many this array was created for.
So if we create say a thousand capacity would be a thousand even if we only had a 10 actual contacts.
And this is actually the type that we're going to use in order to
hold all our contacts.
So that's really it for the data.
Essentially.
So let's start I guess breaking down the problem.
And I kind of want to we'll kind of do this without really doing a real top down design.
Okay but you'll see kind of how we go about it.
It'll still sort of look like a top line but we'll actually write the code.
So essentially what this is saying is
so user will be able to either look at all their contacts or add new contacts they or quit the app.
And so these are the things that the user is able to do.
K so there's three options that they're able to do so that's actually
right out kind of the baseline of everything.
So let's say we need the option that they can do
and we'll go through
and while the option
is not it's quit of quit odysseys.
Q And we'll kind of do the option or get the option from these areas.
User and we will kind of execute this option game or do the option
again we don't have implementations for this.
We're just sort of designing how it's going to work.
Using the option.
And of course we'll probably need our contactee.
So
this is called contact.
It's the
X
the like so and initially it will be empty.
X TV.
So Dot we don't have any right because we haven't read Minya and
you set the size number contact's zero.
And capacity currently zero.
Right.
The very start of her and we don't have anything so everything's kind of just default.
So it says at the start of the program it will read in the list of contacts.
So we'll need some way of loading all of our current contacts if it exists.
So at this start we'll say lude on tax
contacts
the OK so will need to load it somehow.
Right.
Ask Nizer.
OK so we load the contacts will get the option from the user and then we'll just keep the option.
But what I guess will need to also pass in the contacts range because what if they want to add something
and will need context.
So this is another way you can sort of design your program.
It doesn't always have to be the top down design method that we've been use and you know that's kind
of what we're doing right now anyway.
It just means that we don't have to always kind of write out all the pseudo code first and then then
code after which is kind of going against what I told you before but that was really to help you solve
the problem.
And we're still solving the problem.
So right now we're just sort of kind of writing B.
The main function and that'll kind of guide us to you what everything what what we need to do for to
actually solve the problem.
OK so now we have these kind of functions and are not intimate at all really yet.
Right.
That's all this is all we've done.
Not too much yet.
So you need to get the option from the user and execute the option and load context right.
So we'll need those functions.
So you just declare them so.
Option from user will be character or return of character.
This will just be our get character function.
There
they execute option or turn nothing.
And it will take us CH and it will take the option and the contacts Stevy probably by reference ranks
could change
so and we also need load contex.
We're actually going to make it Boullion and going to keep building because what if the file doesn't
exist yet.
So
X is also the right reference.
And let's kind of just to some of the it's
not going to rate everything at this kind of rates.
And some of the simple ones.
So this option from user We just want to call and everready included the huge hills to peer mood tunes
h Gach
and you should actually get it OK.
So we're going to have three options.
So cons are options for the proper input.
Say d is for display everything a is for adding and for.
A return.
Get character
the prompt will be please choose option.
New lines in here and I'll just say I like D for display.
Contact's
in a we're adding contacts and Hugh.
A What is your choice.
I like that.
So I remember that slash and is for a new lines like the Entergy essentially.
So the next thing we need is the error strings right.
Such and such for string
you're.
Again so
that in and of course all the options three of them and we'll say C C lowercase
and it's lowercase because you use lowercase Q here for quit.
It's Y.
So that's kind of it for this.
So let's just run this and see
that.
So what is your choice.
A Nothing happens D nothing happens.
Q It's gay that's for that and let's rate execute option.
So here we'll do a switch on the options
have a case for adding a and for this will say well need new function.
I'll just add option
and
Imbert
and don't need another one for displaying the same display and contacts
so notice how we're kind of doing the stuff this now.
So we're kind of doing the design of this kind of piecemeal right.
So we just kind of said oh I'll probably need something like this for I get the option for Muser and
then I have to actually do the option from the user right and then we'll do that while he doesn't want
to quit.
Right.
So that's how we kind of came up with this main loop and same with like this all we need to somehow
read it into the actual context.
Right no we haven't done that yet.
And similarly here next cute option we need some way of adding a contact and it's a way of displaying
all the contacts.
Right.
So we're just kind of doing this piecemeal and what we think makes sense.
So we're still sort of doing this top down design we're just not writing at all in pseudo code for us
we're just kind of writing code as we kind of think of it.
So we need to add option and display option.
So it's this or read option
and it's going to take the context D-B by reference.
Change the contacts there.
And we need display.
It's display option.
This will be by conc because we don't have to change it.
Copy this
piece it down here.
OK.
So let's kind of implement the display option should be quite simple.
So we just loop through all of our contacts and then display contact.
So 4 0 is less than an hour.
It's the number of contacts it's right.
And all we do is just put everything in the name
is
going to tax the rich winter.
First rate.
And that's because this kind of this is called the indexing operator.
Right.
That we've been using for arrays and this actually will dereferences.
So that's why we don't need a star or a star.
But this thing is Arrow right.
You reference it it's say Dot
and we'll do that for the last few
mean and
then we'll put the phone numbers on
there
and we'll put two initials here because we want a space between each entry.
And that's really it for display option really.
So again we're just sort of doing this all piecemeal right.
Or just kind of taking a little piece of work and then reading that code the way we think it should
work and then we're moving on to the next part right.
So add option it's go with this.
This will be a little bit more complicated I think.
So like I said before in the program it never said that we could have.
There's no upper limit to how many contacts we can have.
OK.
So that means that we need to grow our contacts array any time that we're at the maximum.
So here now let's say if
the contacts D-B dot the number of contacts is somehow equal to the capacity then we somehow need to
resize this whole this whole contacts D.B right we need to grow it to be bigger.
So we can add more contacts.
So say resign as contact us Eby.
And so we're just kind of kind of eyeballing this right we're kind of just like going off the cuff a
little bit.
We're just saying Oh like we see a problem and then we just kind of I'm just writing a function that
doesn't exist yet and we're saying OK it's going to it's just going to resize this thing somehow we
don't know yet.
Right.
So how much should we need.
Well need the context to me obviously that's what resizing.
So how much should we resize it by.
We could just add one right too.
So we just make capacity one more right.
But instead of that because we don't want to resize it all the time are actually going to say contacts.
TB dot capacity.
And when you say times 2 and then plus 10 so we'll multiply whatever the capacity is times 2 and then
just add 10 more.
Okay.
And I'm doing this because what if the capacity with zero.
Right.
Which is which it is at the very start.
Right.
And what if it was zero then contacts D.B capacity which is zero times to zero.
So if I didn't have this plus 10 they would still amount to zero.
So that's why I'm kind of just adding a few more.
OK so what does resize.
And then once it's resize and everything's good then we will actually get all the input.
So the next thing we need to do is actually get the first name and last name so don't have a way in
you to actually get a string although we've done that in hangman before.
Right.
So what I've done you tildes is I've actually added a method called Get string and I I took this directly
from hangman and I use sort of the same signature as get character with like the valid input or not
that out and put it like the scared character with the prompt in the air and actually just feel this
string.
And with some maximum.
So if you want to take a look at how I implemented this it you shouldn't be surprised what's happening.
Right.
So we get the string string which is named the function a failure we have a length.
Again I just stole this basically exactly from hangman and the one thing I added was this line.
So by the way if you want to copy this down just pause the video and then copy the whole thing.
OK so the one thing I did add that that's new is this line right here which will kind of get the whitespace.
This is actually a special thing that kind of comes with the scene in I'll stream and this will get
all the white space before we actually get the string.
It's kind of weird why we need to do this.
But sometimes I guess the input stream really doesn't clear properly once we get once we call this get
function k.
So that's why we kind of need it here.
So just remember to put this whitespace see in whitespace here.
Otherwise I've also added this doesn't matter too much I don't think.
But I bet it doesn't matter.
And everything else should be the same in all it does is turn the length of the string that you got
and that's it.
So this is directly from Hank except for this part.
So let's go back.
So here I wanted to say get string
and or say please enter first name
and input it's it's
air string
Turks TV your contacts so are actually going to change this array directly.
And this should be contacts D-B DOT number of contacts so put it in the next available slot is what
this is saying.
Right.
Because if even if we had zero as long as we had some as long as we actually had a positive capacity
I would just add it to the next available slot.
And even if there's a zero this would work.
So first name that's the string and then Max Nemes size as well.
OK.
And again minus one because we're not here.
This puts the knot here and there it was a string these enter the last name.
As your string
xt.
The pointer X
contacts and last
and then we'll just say see each
actually will say
get integer.
So we'll just see an integer here please.
Phone number
is in contact.
It's.
Number of contacts phone number OK.
So that will put the whatever the user input into our into the next available phone numbers.
And then we'll just add one to the number of contacts.
It's the
kinda plus.
And then we'll save contacts and again we do not have this function right.
But but we know we need to save here because in the problem once the contact is added it should be saved.
OK.
So again I'm just I know I'm not doing it.
Top down design for this.
I'm just kind of going off the cuff and just writing the code that I think we will need right.
You know it's just.
I'll just say to the user say.
So I want to call it quits for this lecture.
But in the next lecture we will actually implement the resize all the contacts and we'll implement the
saved contacts and load decks.
So I'll see you in the next lecture.