Yeah, I can see how doing things that way would work better, but were only allowed to do what the teacher wants us to do. |
But what I am proposing doesn't violate the requirements of the assignment & it's not complicated either.
Chances are, I'm probably going to have to integrate the constructors and functions into the existing address book, and the change in menu will probably only be a temporary move, just to show us how to do it. |
I have a feeling I'm not doing what he's expecting. |
When were done working on this as an assignment, I plan to try out what your saying, I'd like to see if I can do it. |
In my mind it is best to do it the best & most elegant & organised way as possible. There don't seem to be too many restrictions in your assignment & I am not proposing anything really hard. I think you have the ability to do it properly now.
Maybe I need to explain it better.
The interface for the app is the menu, and this is implemented with a menu class. Ideally you would create a new menu class derived from the base menu class called CMenuAddrBk say. This is so you can create different menus for different things. This isn't really needed here but a good practice anyway, because it's easier to extend in the future. To do that you would need virtual functions, - I don't know whether you have learnt that either. Always think about how things might need to be scaled up in the future.
The example code for the menu class, indicates to me that you should set it up with the menu options & associate a function for each one.
The CMenuAddrBk class would have an addressbook object as a member, so you can call it's functions.
In your existing main(), each case in the switch would correspond to a function in the CMenuAddrBk class. The functions should call the appropriate addressbook function - which actually does the work. The addPerson function might look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
bool addressBook::addPerson() {
PERSON p;
cout << "Enter First Name " << endl;
cin >> p.m_fName;
cout << "Enter last Name " << endl;
cin >> p.m_lName;
cout << "Enter Address " << endl;
cin >> p.m_Address;
people.push_back(p);
if(tail == -1) {
tail++;
}
return true;
}
|
I am not sure of your reasoning for returning bool in this function as it always returns true. Error checking is important - I would have used a try - catch block with an exception, but you probably haven't learnt that yet.
What was the purpose of the head & tail variables?
So now the code is in the addressbook class & not in main() .
Once you have done that, there is not a lot left to go in main(). In a simple situation, main() should only create a menu object & call it's runmenu function - and that is all. That's why I said 10 -15 lines in my last post.
The menu functions should be brief too, because all they do is call an addressbook function.
All this stuff is about modularity & encapsulation - one of the main concepts in OOP.
I am just trying to get you to put the logic in the right place. Here is a real world example:
Say you are writing code to simulate a fruit shop, and you have machines that pack different sorts of fruit. There is a main function which calls a selection function that sends the right fruit to the correct packing machine, and each packing machine is a function of its own.
None of the functions care or even know what is happening with the details of the other functions - they are all separate. They have particular input & output, but what the details of next function does, is irrelevant to the current function.
What you are doing at the moment, is putting code for the Apple Packing Machine into the main function.
Anyway - you sound dedicated & committed to your programming which is really good, hopefully you have enough time left over for your other subjects!!
Hopefully I (& others) can steer you towards getting a really good grade which you deserve for putting in so much time & effort.