0

I'm new to C++ and I have a project about creating this Address Book program and I'm having trouble with the code below. The code is supposed to create records.

cin >> ch;
switch(ch)
{
    case '1':
        system("cls");
        cout << "\n\nINSERT RECORD";
                        cout << "\n\nInput FIRST NAME: ";
                        cin.ignore();
                        getline(cin,firstname,'\n');
                        cout << "\n\nInput LAST NAME: ";
                        getline(cin,lastname,'\n');
                        cout << "\n\nInput PHONE NUMBER: ";
                        getline(cin,phonenumber,'\n');
                        cout << "\n\nInput DAY OF BIRTH: ";
                        getline(cin,dayofbirth,'\n');
                        cout << "\n\nInput MONTH OF BIRTH: ";
                        getline(cin,monthofbirth,'\n');
                        cout << "\n\nInput YEAR OF BIRTH: ";
                        getline(cin,yearofbirth,'\n');
                        cout << "\n\nInput AGE: ";
                        getline(cin,age,'\n');
                        cout << "\n\nInput STREET NAME (Address): ";
                        getline(cin,streetname,'\n');
                        cout << "\n\nInput CITY (Address): ";
                        getline(cin,city,'\n');
                        cout << "\n\nInput STATE (Address): ";
                        getline(cin,state,'\n');
                        cout << "\n\nInput ZIP CODE (Address): ";
                        getline(cin,zipcode,'\n');
                        cout << "\nRecord inserted!";
                    current = ad.AddNode(temp);
            ad.userPromptStatement();
    break;


    node* AddressBook::AddNode(nodePtr temp)
    { 
        string firstname;
        string lastname;
        string phonenumber;
        string dayofbirth;
        string monthofbirth;
        string yearofbirth;
        string age;
        string streetname;
        string city;
        string state;
        string zipcode;
        AddressBook ad;

           if(head != NULL)
            {
                current = head;
                while(current -> next != NULL)
                {
                    current = current -> next;
                }
                current = new node;
                current -> next -> firstname = temp -> firstname;
                current -> next -> lastname = temp -> lastname;
                current -> next -> phonenumber = temp -> phonenumber;
                current -> next -> dayofbirth = temp -> dayofbirth;
                current -> next -> monthofbirth = temp -> monthofbirth;
                current -> next -> yearofbirth = temp -> yearofbirth;
                current -> next -> age = temp -> age;
                current -> next -> streetname = temp -> streetname;
                current -> next -> city = temp -> city;
                current -> next -> state = temp -> state;
                current -> next -> zipcode = temp -> zipcode;
                current -> next = nullptr;
                return current;
                ad.userPromptStatement();
            }
           else
            {
                head = new node;
                head -> firstname = temp -> firstname;
                head -> lastname = temp -> lastname;
                head -> phonenumber = temp -> phonenumber;
                head -> dayofbirth = temp -> dayofbirth;
                head -> monthofbirth = temp -> monthofbirth;
                head -> yearofbirth = temp -> yearofbirth;
                head -> age = temp -> age;
                head -> streetname = temp -> streetname;
                head -> city = temp -> city;
                head -> state = temp -> state;
                head -> zipcode = temp -> zipcode;
                head -> next = nullptr;
                return current;
            }
    }

I keep getting an error in "xstring".

When I create 1 record, the error does not happen, but when I create the second one, I get this: "Unhandled exception at 0x00934ABB in dummy3.exe: 0xC0000005: Access violation reading location 0xCDCDCDE5."

bool _Grow(size_type _Newsize,
    bool _Trim = false)
    {   // ensure buffer is big enough, trim to size if _Trim is true
    if (max_size() < _Newsize)
        _Xlen();    // result too long
    if (this->_Myres < _Newsize) /*** <- next statement that will be executed points to this line ***/
        _Copy(_Newsize, this->_Mysize); // reallocate to grow
    else if (_Trim && _Newsize < this->_BUF_SIZE)
        _Tidy(true, // copy and deallocate if trimming to small string
            _Newsize < this->_Mysize ? _Newsize : this->_Mysize);
    else if (_Newsize == 0)
        _Eos(0);    // new size is zero, just null terminate
    return (0 < _Newsize);  // return true only if more work to do
    }

any ideas? I don't know anything about the "xstring" part..

  • You are accessing a null pointer. Probably helpful: ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Samuel Mar 07 '14 at 09:12
  • 1
    Your AddNode either (a) detects a NULL head, allocates a node, leaks its memory, then returns a mystery pointer, `current`, that was never set nor even declared in the function scope, or (b) detects a non-NULL head, walks to the end of the linked list, then promptly loses the pointer just-obtained to allocate a new node, dereferences its `next` incorrectly (repeatedly), returning a pointer to that new node but never adding it to the list. I.e this code has not yet begun to fight. – WhozCraig Mar 07 '14 at 09:21
  • 1
    Is `temp` already dynamically allocated somewhere else, and all you want to do is link it to the tail end of the (potentially empty) linked list pointed to by `head` ? And where does `current` come from? This is clearly a function, yet neither `current` nor `head` seem to be declared or passed in. Post the **real** function, *with parameters*, **and** document any global/external variables. There are plenty of bugs in this code and fixing them is dependent on that info. – WhozCraig Mar 07 '14 at 09:33
  • Don't start your variable/function/anything names from underscore : http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – zoska Mar 07 '14 at 09:49

2 Answers2

1

Your problem lays here:

current = new node;
current -> next -> firstname = temp -> firstname;

You just created a new node (replacing current) and then you try to access its child (next) which is not initialized.

This should be better:

current -> next = new node;
current -> next -> firstname = temp -> firstname;
...
current -> next -> next = nullptr;
return current -> next;

Another hint: If tmp is dynamically created, then think about using it directly instead of copying its elements. Like this:

current -> next = tmp;

I don't know all of your code, so I cannot say if that works for you.

Mike de Dood
  • 393
  • 1
  • 10
  • 1
    I'm fairly certain the `current = new node;` should be removed, and the rest remain as-is. look again. (the inappropriate return value not withstanding). – WhozCraig Mar 07 '14 at 09:26
  • Oh, yes. Now I get the error. I will update my answer. – Mike de Dood Mar 07 '14 at 09:27
  • 1
    yeah, *both* return value make no sense, nor is it clear whether the `temp` being passed in was itself dynamically allocated and should just be linked as `current->next`. (and the code as you have it now allocates a new node, then overwrites the content of the *existing* data of the `current` node, so it still isn't right). – WhozCraig Mar 07 '14 at 09:28
  • I added it to my answer, but to use it or not is his choice (maybe he has a good reason to copy it). – Mike de Dood Mar 07 '14 at 09:32
0

The cause of that error propably is the trying to accessing null pointer.

You should create temp before use it. I couldnt see any pointer which name is temp. Please first create temp, and allocate it on memory with malloc or memset. Then you can pass the error.

Cracker
  • 500
  • 7
  • 21