By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,814 Members | 1,671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,814 IT Pros & Developers. It's quick & easy.

Help with array of pointers

P: 2
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller.

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
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. /* Used for each address. Functions manipulate date within */
  6. /* next is used in the link list operation. It's used to indicate next in the chain */
  7. class controller;
  8. class address {
  9.     protected:
  10.         string firstName, lastName, street, city, state;
  11.         unsigned int zipCode;
  12.         address * next;
  13.     private:
  14.         friend class controller;
  15.         address() {
  16.             next = NULL;
  17.             firstName = lastName = street = city = state = "Unset";
  18.         } // Default constructor. Most important use is setting pointer next to NULL.
  19.         void setDetails() {
  20.             cout << "Enter first name: ";
  21.             cin >> firstName;
  22.             cout << "Enter last name: ";
  23.             cin >> lastName;
  24.             cout << "Enter street address: ";
  25.             cin >> street;
  26.             cout << "Enter city: ";
  27.             cin >> city;
  28.             cout << "Enter state: ";
  29.             cin >> state;
  30.             cout << "Enter zipCode: ";
  31.             cin >> zipCode;
  32.         } // Sets the entry's details
  33.         void viewDetails() {
  34.             cout << "First name: " << firstName << endl
  35.                 << "Last name: " << lastName << endl
  36.                 << "Street Address: " << street << endl
  37.                 << "City: " << city << endl
  38.                 << "State: " << state << endl
  39.                 << "Zip Code: " << zipCode << endl;
  40.         } // View date held within the object
  41. };
  42. /* Following object controls all existing address objects. The first is set */
  43. /* as a friend of the second, allowing it to manipulate it's values. */
  44. /* Start is always the first object. Head is always the created object, current maintains */
  45. /* the last created object. Finally locate is a temporary pointer used when searching */
  46. class controller {
  47.     private:
  48.         address *start, *head, *current, *locate;
  49.     public:
  50.         controller() {
  51.             start = head = current = locate = NULL;
  52.         } // Set all pointers to NULL. It's never wise have an aimless pointer.
  53.         void addAddress() {
  54.             if(start == NULL) {
  55.                 start = new address; // Makes a new object pointed to start
  56.                 current = head = start; // Sets all three pointers to the same start object
  57.                 current->setDetails(); // Loads up the function from the object to set details
  58.             } else {
  59.                 head = new address; // Makes a new object. Note head is now used
  60.                 current->next = head; // Sets previous object null next to new object
  61.                 current = head; // Sets current object to created object
  62.                 current->setDetails(); // Sets the details of created object
  63.             } // If start is null, then no object has been created yet.
  64.         }
  65.         /* Lists all created objects in the link list */
  66.         void listAddress() {
  67.             locate = start;
  68.             unsigned int count = 0;
  69.             if(start == NULL) {
  70.                 cout << "There are no address entries. Add some.\n";
  71.             } else {
  72.                 cout << "Current list of unsaved entries: \n";
  73.                 do {
  74.                     cout << "     " << count << ". " << locate->firstName << " "
  75.                         << locate->lastName << "        " << locate->city << endl;
  76.                     count++;
  77.                     locate = locate->next; // Change the object being read in to the next in list
  78.                 } while(locate != NULL);
  79.             }
  80.         }
  81.         /* Counts how many objects current exist in the link list */
  82.         unsigned int retCount() {
  83.             unsigned int count = 0;
  84.             locate = start;
  85.             while(locate != NULL) {
  86.                 count++;
  87.                 locate = locate->next;
  88.             }
  89.             return count;
  90.         }
  91.         /* Creates an array of pointers to existing link list. Allows for ease with deletion. */
  92.         address **retAddress() {
  93.             locate = start;
  94.             unsigned int count = 0;
  95.             unsigned int size = retCount();
  96.             address ** ptrEntries = new address*[size]; // Creates dynamic array of pointers
  97.             while(locate != NULL) {
  98.                 ptrEntries[count] = locate;
  99.                 locate = locate->next;
  100.             }
  101.             return ptrEntries;
  102.         }
  103.         /* Deletes a user supplied address. */
  104.         void delAddress(unsigned int num) {
  105.             unsigned int count = retCount();
  106.             address *del, *prev, *after = NULL;
  107.             if(count >= num && num > 0) {
  108.                 num--; // Arrays starts from 0. User sees list starting at 1
  109.                 address ** arrPtrAddress = retAddress(); // Creates array of pointers to objects
  110.                 del = arrPtrAddress[num]; // Object to be deleted
  111.                 /* Object has another following it, but is at the head of the list */
  112.                 if(del->next != NULL && start == del) {
  113.                     after = arrPtrAddress[++num];
  114.                     start = arrPtrAddress[num];
  115.                 }
  116.                 /* Object has another following it, and is not the head of the list */
  117.                 if(del->next != NULL && start != del) {
  118.                     prev = arrPtrAddress[num - 1];
  119.                     after = arrPtrAddress[num  + 1];
  120.                     prev->next = after;
  121.                 }
  122.                 /* Object has nothing following it, but is not the head of the list */
  123.                 if(del->next == NULL && start != del) {
  124.                     prev = arrPtrAddress[num - 1];
  125.                     prev->next = NULL;
  126.                 }
  127.                 delete [] del; // Deleting the object
  128.                 del = NULL; 
  129.                 delete [] arrPtrAddress; // Deleting the array of pointers
  130.                 cout << "You have deleted address #: " << num+1 << endl;
  131.  
  132.             } else { cout << "There is no address #: " << num << endl; }
  133.         }
  134.         void editAddress(unsigned int num) {
  135.             unsigned int count = retCount(); // Counting how many objects exist
  136.             if(count >= num && num > 0) {
  137.                 address ** arrPtrAddress = retAddress(); // Creating the array of pointers
  138.                 address *edit = arrPtrAddress[num - 1]; // Creating a new pointer to requested object to view
  139.                 cout << "You have chosen to edit entry #: \n";
  140.                 edit->setDetails();
  141.                 delete [] arrPtrAddress; // Deleting the array of pointers
  142.             } else { cout << "There is no address #: " << num << endl; }
  143.         }
  144.         void detAddress(unsigned int num) {
  145.             unsigned int count = retCount(); // Counting how many objects exist
  146.             if(count >= num && num > 0) {
  147.                 address ** arrPtrAddress = retAddress(); // Creating the array of pointers
  148.                 address *view = arrPtrAddress[num - 1]; // Creating a new pointer to requested object to view
  149.                 view->viewDetails(); // Viewing the object's details
  150.                 delete [] arrPtrAddress; // Deleting the array of pointers
  151.             } else { cout << "There is no address #: " << num << endl; }
  152.         }
  153.         /* Destructor deletes all existing objects in the link list */
  154.         ~controller() {
  155.             unsigned int count = retCount(); // Counting how many objects exist
  156.             if(count > 0) {
  157.                 address ** arrPtrAddress = retAddress(); // Creating the array of pointers
  158.                 /* First delete the objects themselves */
  159.                 for(unsigned int i = 0; i <= count - 1; i++) {
  160.                     delete [] arrPtrAddress[i];
  161.                 }
  162.                 delete [] arrPtrAddress; // This deletes the array of pointers
  163.             }
  164.         }
  165. };
  166. int main() {
  167.     controller control; // Creating the controller object for all address objects
  168.     unsigned short choice = 0;
  169.     unsigned int record = 0;
  170.     /* The Menu */
  171.     do {
  172.         cout << "-----[MENU]---------------------------------------------\n"
  173.             << "1. List all addresses for current session.\n"
  174.             << "2. Add a new address for this session.\n"
  175.             << "3. Delete an address created in this session\n"
  176.             << "4. View detailed address by number\n"
  177.             << "5. Edit an address details by number\n"
  178.             << "6. Save session to hard disk\n"
  179.             << "7. List saved sessions on hard disk\n"
  180.             << "8. Delete a saved session on hard drive\n"
  181.             << "9. Load a saved session\n"
  182.             << "10. Quit program\n\n"
  183.             << "#>";
  184.         cin >> choice;
  185.         /* Points the code to where the user requested */
  186.         switch(choice) {
  187.             case 1: { control.listAddress(); break; }
  188.             case 2: { control.addAddress(); break; }
  189.             case 3: {
  190.                 cout << "Enter address # from current session to delete\n" << "#>";
  191.                 cin >> record;
  192.                 control.delAddress(record);
  193.                 break;
  194.                     }
  195.             case 4: {
  196.                 cout << "Enter address # from current session to view\n" << "#>";
  197.                 cin >> record;
  198.                 control.detAddress(record);
  199.                 break;
  200.                     }
  201.             case 5: {
  202.                 cout << "Enter address # from current session to edit\n" << "#>";
  203.                 cin >> record;
  204.                 control.editAddress(record);
  205.                 break;
  206.                     }
  207.         }
  208.     } while(choice != 10);
  209. }
Oct 3 '07 #1
Share this Question
Share on Google+
2 Replies


RedSon
Expert 5K+
P: 5,000
Next time you post please use CODE tags. We forgive the first offense but if you want people to answer you questions don't forget the code tags.
Oct 3 '07 #2

P: 2
Ah, okay, thanks. ;)
Oct 3 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.