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..
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.