473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with array of pointers

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
2 2946
RedSon
5,000 Expert 4TB
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
Ah, okay, thanks. ;)
Oct 3 '07 #3

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

Similar topics

4
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
13
by: Ben | last post by:
I have a program which is using a lot of memory. At the moment I store a lot of pointers to objects in std::vector. (millions of them) I have three questions: 1) Lets say the average Vector...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
3
by: Ben | last post by:
Hi, I am trying to create a specific structure, but having problems with the type definitions. Basically it's an array of trees. Each element of the array should be a binary tree of type...
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
6
by: M Turo | last post by:
Hi, I was wondering if anyone can help. I'm want to pre-load a 'table' of function pointers that I can call using a its arrayed index, eg (non c code example) pFunc = func_A; pFunc = func_B;
3
by: googlinggoogler | last post by:
Hi This should all be pretty standard C stuff, but I'm going to use terms like mouse callback to communicate what Im tyring to do. Basically I have my program whirling around in an infinite...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.