Read the end of the section so we'll be doing the final part of the mini project that was started in
the last section and this will be the last lecture that we will do practice in the next section.
We'll have more of the library that we're going to use for the final game.
So we're not really going to do much practice with that.
We'll just use it for the final game instead.
So the final part of this Me project says Replace the C strings with C++ strings.
Use a vector instead of dynamic memory and use a text file instead of a binary file.
So we'll get to the rest of this later.
So first what I really want to do is when when approaching a problem like this where you do kind of
a revision of what you've done before.
Really what you want to do is to get to parity with the last version.
So you want to update the strings with the C++ strings and a vector and the text file and just get everything
working like it was before.
OK so we'll start with changing the strings or the C strings to use strings so will need an set of string.
Or any CPP just regular strings of strings and are going to also include the vector
K and the next thing we'll do is we'll change the data to reflect the changes that we need.
So will do a string for a first name
string for the last.
And instead of having the memory like we have here.
You will just use a vector so that this will do a vector of contacts.
Contact us and notice that it's not like an old pointer of contacts just contacts because vector itself
will do all the dynamic memory for us.
OK so now that we've kind of changed the data and we should also change this as well.
TXT file
now that you've done these kind of data changes we'll have to update our code to reflect the change.
So we're going to go through the whole thing and then just make whatever changes are necessary.
So here we don't need any of this in the main because this kind of had to do with the dynamic memory
portion of it.
So this kind of remove it.
We don't need to delete it after because the vector will actually take care of that for us
and that's it for this for now although you'll see why.
Oh well if we don't if we can't read the file We're just not going to bother.
OK so we're was going to load the contacts so they and in fact we won't need to create the file anymore
because again if we don't have the file then that's fine.
So now we will.
We don't need this function anymore.
Take away and all of this is needs changing.
So it's not binary anymore.
And let's just sort of remove all this and in a minute we will rewrite it.
It will do the same for the save
So we we don't need to resize the context at all because our vector for us so is this function entirely
in for the display.
You have to use the actual size function of the contact's vector.
That's how many contacts we have now.
And have these 0.10 contacts just contacts this.
How will this play an option.
We started with the resize but again we don't need to resize it because factor that for us we don't
need to add 1 to the number of contacts anymore because again once we add it properly it will just kind
of work.
So how do we fix this.
Right now it's getting the string as a C string but instead of that let's change this function to use
as a C plus string.
So when you have a new contact
like so.
And we're going to use it here.
So we're going to change this to be our new content.
And now we don't need this size anymore.
It could be big.
We want
a new contact here.
Like that.
And similarly here we just get the new content and here will just add it to the context to be
pushed back to do so just add it to the end.
It doesn't really matter the order for now and now we need to change this get string function to use
C++ strings and Cives Caesar.
So in that file and we'll need the string library here in this and we just change this to be a string
and we'll need to use stood colon colon because we're not going to put the line using namespace studio
here.
OK.
So if you don't have the using names is SDD when you actually say the actual namespace and then call
in Colon and then here will say string and we'll pass it by reference.
Name it a string.
And now we need to change the station as well.
We scroll down to it here.
This needs to be a string
blustering
and we could change some of the stuff so we don't need these two lines anymore.
We just need to say see in string because CNH works with small strings just fine.
And here we could just say string dot it's way that way and it's really all we need to do is function
back.
OK so we're creating new contact we're just populating that contacts first name last name and his phone
number and then just pushing it back into our contacts vector.
And saving it just like normal.
So now that that's done let's start tackling the load contacts and save contex.
Let's start with the Save contacts
easier slightly easier.
All you have to do is go through each each contact.
For his last then contacts
the dot contacts site.
That's how many there are
and we need to output those contacts to the file and we'll do that.
And I was out with our file do that with the insertion operator and we'll see what contacts the contacts
I adopt first name and we'll put a space between the names and the phone number as well.
Contacts the contacts
last name
the contacts.
It's
like that.
So this will actually print out to the file one on one contact essentially.
So each line of the file will be one contact.
OK.
And now I only want to output new lines if
if there are more contacts after me.
So after the current one Ratsch the either one right.
OK.
So we'll say contacts size minus one.
So as long as I is less than the last contact and the index of the last contact which is this right
here then will output a new line.
This is to make sure.
So make sure that there is no new line.
And file is what this is saying essentially and you may be wondering why don't we.
Why do we care about that.
Well sometimes the way we read in the way we'll be reading it in that will cause a problem if there
is.
So we're going to kind of make sure that doesn't happen.
OK so now look at load contacts function and here will say we'll just get a new contract.
This will be our
the one that we store.
Each line will read a contact and then we'll store it into this one.
OK and we'll say while not in file.
OK.
And what this is is while we're not at the end of the file.
So each file has kind of a marker at the very end of it which signifies that that's the end of the file.
And this is all this is doing and saying wow the the current cursor of the file is not at the end of
the file.
That's what we're doing here say in file.
So we'll read any whitespace first and then if if the file was empty let's say.
So if we're at the end of the file already then we should just get out of the file OK.
So this has to do with this kind of whitespace problem.
It was always kind of a weird problem in textfiles as usual I don't really like to use them that much.
So we'll just read the line contacts.
It's and you contact
first name
you contact each
last name and get the phone numbers.
Wow.
And again this extraction operator can read strings.
It's overloaded to read strings so.
And this will also
this will also ignore whitespace.
So these kind of white spaces that we put in here you'll just be ignored.
So this will be fine.
And then we just add it to the database.
Using pushback.
Dr.
in it's I think it it's like I said before we don't really care if the file exists or not we'll just
it will just be empty.
Right.
The vector to removing the file or the functions that need.
And if we run this
you'll see that you have no contacts in.
I don't
display that.
So I one one
display.
So we have and quit and that's just rerun it and make sure that C properly display.
So we have our two contacts.
So that's good.
So it's basically working as it did before.
And if you want to see the file See
here's the file that saved and it's in nice text that we can understand.
So says Fred Flintstone on one line with the space just like how we save it
is a first name last name first name last name in
that
phone.
And noticed that we read it in the same way so read back in first name last name or number.
So that kind of parity now with what we had before.
Now let's add in the rest of what it said.
So it says really a function that will remove a contact from the vector make this an option in the main
that way.
So let's do that.
So we'll add any function says void.
Remove option
2 contacts divi
reference.
Add it to an.
And we need to as I said here make this an option and make menus.
You need to change the main menu here.
Option user.
And we'll add it right after AD contex say our eyes remove contact
whine like that and add it here as well.
Our right.
It's a new option and it's three four options.
And we have four options.
And in this execute you need to change it so there is a new case to remove option.
Number
OK so let's let's just run this and see hopefully make steak so see that there is play add in or move
straight to.
Nothing happens yet.
We have implemented it.
So that's fine.
So let's implement remove the Remove the options.
Here we will.
How will we do this.
So how will the user know which one to remove.
We could make them enter the first name and last name maybe.
But what if they have the same name like John Smith.
So we actually remove it by phone number so
first we'll check to see if we have any contacts at all because if we don't then there's no point removed.
Right
Size like that.
So it's greater than zero.
Right.
Or if we say less than or equal to zero we could say ouch.
No contacts in the list.
And would just return like that.
So you could do a return with nothing right.
If it's void.
So this will just kind of exit the function.
So if we do have contacts so if we do get to this point we should probably I guess input the phone number
we could do it by could do it by first name my last name but again there might be the same last name
but maybe that will be better.
It could be a better option at least to see visually.
So maybe that's what we'll do.
So we'll say string and this also helps us with using strings more string first names and last name
really what you want is some kind of ID for every for every contact and we you would actually remove
it based off.
But we're not going to do it here.
So we'll actually get the first name and last name from the users say please enter first name
in Pasadena hairspring
first name it's right you get the last name.
So now that we have the first name and last name we have to actually go through our vector and see if
anyone matches up with that first name and last name.
OK so we'll need some kind of boolean to know if we found it or not and we'll just kind of delete the
first one we find so if there's duplicate say there's like two John Smiths Woods delete the first one
and it's the index that we found it at.
Found index will start at negative 1 because negative 1 will signify that we don't.
You haven't found anything.
It's only zero is less than the number of contacts in the database
signs.
So it's
and we haven't found it.
It's both conditions because if we do find it then we'll just kind of exit it.
So if the current one that we're looking at
is equal to the first name
and
if it's equal to
the last name
then we found it.
So it's true.
Index found index
is the current one.
I one day
so now that we found it we should check to see if it found it or if have all.
So if we found it then how do you remove something.
All we've learned so far is pop back right in our vector.
OK.
So how could you remove a contact at any position by only using the E by only popping the last one.
So how you could do this
is swapping the last one for the for the one that we found and then we'll swap it we'll swap the last
one in the vector for that first one.
So now that the the one we found is at the end and then we'll just pop that one.
So in order to do this we'll need the swap function and we could again we could write it ourselves.
But actually an algorithm that will need anyway it has the swap functions for a swap and sort K so what's
let's use it so we'll swap the one that we found with the very last the very last one if there is any.
So I'll say swap and say contacts eata contacts.
Or of the found index right.
Swap this one with the last one
how to get the last one.
Well it's fairly simple.
Contacts the contacts got size right.
Minus one right.
So remember that vectors are effectively just an array and always to get the very last element in an
array.
It's always the length of the reais minus one for that index.
OK.
So this will put the found index at the end and and then all you have to do is say contact D-B dot contacts
up pop back gay I'll remove it and then we'll just say well I moved out.
Moved
first.
I mean
last night
from contex
like that.
And if we didn't find it we're just output to the user that we didn't find it.
Yeah.
Could not find
his name.
Last name
in.
Resemble.
And so it's kind of go over this one more time so we have no we have no contacts don't do this option.
All right and we use this return with nothing.
I think that's the first time we've ever seen this use use this way.
So if it's a void function and you return nothing they will just exit the function and just exit.
It's a function that day and we'll get the first name the last name and we'll search obviously search
the contacts D-B see if we can find someone with that first name and that last name.
And if we do we'll effectively store that index and say that we found it right in now we'll actually
break out of the loop on the next iteration.
Okay.
And then if we found it we'll swap that index the found index with the very last very last
index.
OK the last element of the vector and then pop the last element and then say it.
OK.
So let's think we've set this all up.
So let's run this and see what happens.
So it's display.
So we have Fred and Barney.
So let's remove.
So enter the first name let's remove Fred.
So he's been removed from contacts and let's see if that's actually true.
And all we have is Barnie.
OK.
It's quiet.
And that's actually check to see
if
it's checked to see if we don't have fretted more.
And if you look at the contacts we don't have right.
OK.
Now the last thing we need to do is say change the display option to have the user choose the order
in which to display.
So we can either sort it by first name or sort of a last name so let's implement that display function
and play option.
So here we will
we should again check to see if we have anything in the first place.
Let's do that.
Read up on daks
size.
If you're less than or equal to zero then just return right.
Turn immediately say
you got it.
It's that way.
But if we do have contacts we shifted out to the user whether or how you want to sort it.
So
but if we do have contacts we shared with the user how they want to sort it.
So let's give them two options.
Current star search options and options will be by first name.
After this one and all for this one
last name in the for option
in we'll say option to get character
these shoes.
Or at option
two new lines for first name
or 0 for last.
It's her choice
put error string
and sort options options
and two and we'll say it's
an check to see what option we got.
The option is it's a search or this should be sort of option.
Sorry
if the option you got is the first one that's first name since its first name.
And we'll sort it by the first
so contacts the bead contacts again.
Contacts Heebee dot contacts dot and.
But we need a we need an actual function that wants to do the comparison and we don't have that yet.
So let's just write the two cases first.
Copy this
in lets just right now kind of.
We don't have it yet.
We'll here will say compare first name
and here you will say hair lasting OK and let's define these functions here let's declare them at least.
Compare first name and it always returns a boolean.
It always takes in the actual type that the vector holds.
So in this case though it will be a contes RAF contact
which is called C one and constant contact
is to notice that these are passed by that Vout.
So identical and actually implement them down here
and actually implement them kind of below or display functions so close together.
So strings actually are able to be compared with the greater than or less than sine.
So that's kind of one nice thing.
Well it doesn't really say which order to sort them so we'll just say see one dot first name.
It's less than c to dot first.
OK so you can actually compare them and it'll actually do what you think it should.
So in this case we're sorting them in ascending order.
And what does that mean for strings.
Well a will be first then be its center.
OK.
And will do the exact same thing for last
like so there is one less thing we have to do and that is to change the display option to pass the context
D-B not by Konst reference but just by reference.
And that is because we're changing the contact's order right.
So if we change it then we can't pass it by conc reference.
Change that.
So leave it in this one right here.
So and now if you run it
in which display first name the only one so far.
So let's add back in Fred
Stone and one of splay last name we should see read the first and Barni the second read first or second.
And -- niblets add some more
stone shows the same number as Fred and let's add Beddie double She is the same as party.
So we just again in stoupe my last name.
Was by first name and we will see Barney Betty Fred.
All right.
So it's in the proper order.
So that's it for this project.
In the next section we'll be dealing with a new library so that it can so that we can draw our output
in a different way than what we've been using.
So far so far we've only ever really used
see out for output.
But in our game in the next year in the last section we'll be using a different way to output to the
console that will be much better suited for games in general.
So I will see you in the next section.