This is more or less, your standard dynamic address book program. Adding, and listing work just fine. However, deleting, editing and viewing relies on member function retAddress. This function returns an array of pointers that are pointing to created objects. In action, all it does is create an array of the objects.
When I use the detAddress function, and select to view a created address, it spits out a bunch of nasty ASCII characters and crashes. I'm guessing it's trying to read something it has no access to. Heres the code:
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <string>
- using namespace std;
- /* Used for each address. Functions manipulate date within */
- /* next is used in the link list operation. It's used to indicate next in the chain */
- class controller;
- class address {
- protected:
- string firstName, lastName, street, city, state;
- unsigned int zipCode;
- address * next;
- private:
- friend class controller;
- address() {
- next = NULL;
- firstName = lastName = street = city = state = "Unset";
- } // Default constructor. Most important use is setting pointer next to NULL.
- void setDetails() {
- cout << "Enter first name: ";
- cin >> firstName;
- cout << "Enter last name: ";
- cin >> lastName;
- cout << "Enter street address: ";
- cin >> street;
- cout << "Enter city: ";
- cin >> city;
- cout << "Enter state: ";
- cin >> state;
- cout << "Enter zipCode: ";
- cin >> zipCode;
- } // Sets the entry's details
- void viewDetails() {
- cout << "First name: " << firstName << endl
- << "Last name: " << lastName << endl
- << "Street Address: " << street << endl
- << "City: " << city << endl
- << "State: " << state << endl
- << "Zip Code: " << zipCode << endl;
- } // View date held within the object
- };
- /* Following object controls all existing address objects. The first is set */
- /* as a friend of the second, allowing it to manipulate it's values. */
- /* Start is always the first object. Head is always the created object, current maintains */
- /* the last created object. Finally locate is a temporary pointer used when searching */
- class controller {
- private:
- address *start, *head, *current, *locate;
- public:
- controller() {
- start = head = current = locate = NULL;
- } // Set all pointers to NULL. It's never wise have an aimless pointer.
- void addAddress() {
- if(start == NULL) {
- start = new address; // Makes a new object pointed to start
- current = head = start; // Sets all three pointers to the same start object
- current->setDetails(); // Loads up the function from the object to set details
- } else {
- head = new address; // Makes a new object. Note head is now used
- current->next = head; // Sets previous object null next to new object
- current = head; // Sets current object to created object
- current->setDetails(); // Sets the details of created object
- } // If start is null, then no object has been created yet.
- }
- /* Lists all created objects in the link list */
- void listAddress() {
- locate = start;
- unsigned int count = 0;
- if(start == NULL) {
- cout << "There are no address entries. Add some.\n";
- } else {
- cout << "Current list of unsaved entries: \n";
- do {
- cout << " " << count << ". " << locate->firstName << " "
- << locate->lastName << " " << locate->city << endl;
- count++;
- locate = locate->next; // Change the object being read in to the next in list
- } while(locate != NULL);
- }
- }
- /* Counts how many objects current exist in the link list */
- unsigned int retCount() {
- unsigned int count = 0;
- locate = start;
- while(locate != NULL) {
- count++;
- locate = locate->next;
- }
- return count;
- }
- /* Creates an array of pointers to existing link list. Allows for ease with deletion. */
- address **retAddress() {
- locate = start;
- unsigned int count = 0;
- unsigned int size = retCount();
- address ** ptrEntries = new address*[size]; // Creates dynamic array of pointers
- while(locate != NULL) {
- ptrEntries[count] = locate;
- locate = locate->next;
- }
- return ptrEntries;
- }
- /* Deletes a user supplied address. */
- void delAddress(unsigned int num) {
- unsigned int count = retCount();
- address *del, *prev, *after = NULL;
- if(count >= num && num > 0) {
- num--; // Arrays starts from 0. User sees list starting at 1
- address ** arrPtrAddress = retAddress(); // Creates array of pointers to objects
- del = arrPtrAddress[num]; // Object to be deleted
- /* Object has another following it, but is at the head of the list */
- if(del->next != NULL && start == del) {
- after = arrPtrAddress[++num];
- start = arrPtrAddress[num];
- }
- /* Object has another following it, and is not the head of the list */
- if(del->next != NULL && start != del) {
- prev = arrPtrAddress[num - 1];
- after = arrPtrAddress[num + 1];
- prev->next = after;
- }
- /* Object has nothing following it, but is not the head of the list */
- if(del->next == NULL && start != del) {
- prev = arrPtrAddress[num - 1];
- prev->next = NULL;
- }
- delete [] del; // Deleting the object
- del = NULL;
- delete [] arrPtrAddress; // Deleting the array of pointers
- cout << "You have deleted address #: " << num+1 << endl;
- } else { cout << "There is no address #: " << num << endl; }
- }
- void editAddress(unsigned int num) {
- unsigned int count = retCount(); // Counting how many objects exist
- if(count >= num && num > 0) {
- address ** arrPtrAddress = retAddress(); // Creating the array of pointers
- address *edit = arrPtrAddress[num - 1]; // Creating a new pointer to requested object to view
- cout << "You have chosen to edit entry #: \n";
- edit->setDetails();
- delete [] arrPtrAddress; // Deleting the array of pointers
- } else { cout << "There is no address #: " << num << endl; }
- }
- void detAddress(unsigned int num) {
- unsigned int count = retCount(); // Counting how many objects exist
- if(count >= num && num > 0) {
- address ** arrPtrAddress = retAddress(); // Creating the array of pointers
- address *view = arrPtrAddress[num - 1]; // Creating a new pointer to requested object to view
- view->viewDetails(); // Viewing the object's details
- delete [] arrPtrAddress; // Deleting the array of pointers
- } else { cout << "There is no address #: " << num << endl; }
- }
- /* Destructor deletes all existing objects in the link list */
- ~controller() {
- unsigned int count = retCount(); // Counting how many objects exist
- if(count > 0) {
- address ** arrPtrAddress = retAddress(); // Creating the array of pointers
- /* First delete the objects themselves */
- for(unsigned int i = 0; i <= count - 1; i++) {
- delete [] arrPtrAddress[i];
- }
- delete [] arrPtrAddress; // This deletes the array of pointers
- }
- }
- };
- int main() {
- controller control; // Creating the controller object for all address objects
- unsigned short choice = 0;
- unsigned int record = 0;
- /* The Menu */
- do {
- cout << "-----[MENU]---------------------------------------------\n"
- << "1. List all addresses for current session.\n"
- << "2. Add a new address for this session.\n"
- << "3. Delete an address created in this session\n"
- << "4. View detailed address by number\n"
- << "5. Edit an address details by number\n"
- << "6. Save session to hard disk\n"
- << "7. List saved sessions on hard disk\n"
- << "8. Delete a saved session on hard drive\n"
- << "9. Load a saved session\n"
- << "10. Quit program\n\n"
- << "#>";
- cin >> choice;
- /* Points the code to where the user requested */
- switch(choice) {
- case 1: { control.listAddress(); break; }
- case 2: { control.addAddress(); break; }
- case 3: {
- cout << "Enter address # from current session to delete\n" << "#>";
- cin >> record;
- control.delAddress(record);
- break;
- }
- case 4: {
- cout << "Enter address # from current session to view\n" << "#>";
- cin >> record;
- control.detAddress(record);
- break;
- }
- case 5: {
- cout << "Enter address # from current session to edit\n" << "#>";
- cin >> record;
- control.editAddress(record);
- break;
- }
- }
- } while(choice != 10);
- }