Code:
#ifndef __addressList__
#define __addressList__
#include <iostream>
using namespace std;
typedef struct node {
string Name;
string DOB;
string address;
string aniversary;
node *next;
} *nodePtr;
class AddressBook {
private:
nodePtr head;
nodePtr current;
nodePtr temp;
public:
AddressBook();
void userPromptStatement();
void AddNode(nodePtr);
void deleteName(string);
void EditNameOrDate();
void PrintAddressBook();
void GenBirthdayCards(string);
void GenAnnCard(string);
void ExitProgram();
};
#endif
Code:
#include <iostream>
#include <string>
#include <string.h>
#include "addressList.h"
using namespace std;
AddressBook::AddressBook(){
head=NULL;
current=NULL;
temp=NULL;
}
void setDOB(string);
string getDOB();
void setAniversary(string);
string getAniversary();
void AddressBook::AddNode(nodePtr temp)
{
if (head != NULL) { //list is already made
current = head; //makes current pointer point to head of the list
while (current->next != NULL)
{ //while the next of current is not at end
current = current->next; //advance to next node
}
current->next =new node;
current->next->address=temp->address;
current->next->aniversary=temp->aniversary;
current->next->DOB=temp->DOB;
current->next->Name=temp->Name;
current->next->next=nullptr;
}
else
{
head = new node; //now head is the front of the list
head->address=temp->address;
head->aniversary=temp->aniversary;
head->DOB=temp->aniversary;
head->Name=temp->Name;
head->next=nullptr;
}
}
void AddressBook::deleteName(string Delname)
{
//nodePtr delPtr = NULL;
temp = head;
current=head;
while (current != NULL && current->Name != Delname)
{
temp = current;
current=current->next;
}
if (current == NULL) {
cout<<Delname<<"Was not in the list\n";
}
else{
temp->next=current->next;
delete current;
cout<<"Name: "<< Delname <<" was deleted.\n";
}
}
void AddressBook::PrintAddressBook()
{
current = head;
if (current==nullptr) cout<<"The list is empty\n";
while (current != NULL) {
cout<<"NAME: "<<current->Name<<endl;
cout<<"ADDRESS: "<<current->address<<endl;
cout<<"BIRTHDAY: "<<current->DOB<<endl;
cout<<"ANNIVERSARY: "<<current->aniversary<<endl;
cout<<"\n";
current= current->next;
}
}
void AddressBook::EditNameOrDate()
{
cout<<"1. Edit Name: ";
cout<<"2. Edit Birthday: ";
cout<<"3. Edit Anniversary: ";
int input;
cin>>input;
if (input==1)
{
cout<<"Enter the name you want to edit: /n";
string name;
getline(cin,name,'\n');
nodePtr curr=head;
while(curr->next!= NULL && curr->next->Name != name)
{
curr=curr->next;
}
cout<<"The current name is: "<<curr->next->Name<<endl;
cout<<"Enter the new name: "<<name;
curr->next->Name=name;
cout<<"The name has been successfully changed to "<<curr->next->Name<<endl;
}
else if (input==2)
{
cout<<"Enter the Birthday you want to edit: ";
string date;
getline(cin,date,'\n');
nodePtr curr=head;
while(curr->next!=NULL && curr->next->DOB!=date)
{
curr=curr->next;
}
cout<<"The current name is: "<<curr->next->DOB<<endl;
cout<<"Enter the new name: "<<date;
curr->next->DOB=date;
cout<<"The name has been successfully changed to "<<curr->next->DOB<<endl;
}
else if (input==3)
{
cout<<"Enter the Anniversary date you want to edit: ";
string AnnDate;
getline(cin,AnnDate,'\n');
nodePtr curr=head;
while(curr->next!=NULL && curr->next->aniversary!=AnnDate)
{
curr=curr->next;
}
cout<<"The current name is: "<<curr->next->aniversary<<endl;
cout<<"Enter the new name: "<<AnnDate;
curr->next->aniversary=AnnDate;
cout<<"The name has been successfully changed to "<<curr->next->aniversary<<endl;
}
}
void AddressBook::GenBirthdayCards(string birthdayBoy)
{
cout<<"Dear "<<birthdayBoy<<"\n\n";
cout<<"Hope your birthday is really wonderful and this coming year is the best yet!\n";
cout<<endl<<endl;
cout<<"Love,\n";
cout<<"Joanne\n";
}
void AddressBook::GenAnnCard(string happyCouple)
{
cout<<"Dear "<<happyCouple<<endl;
cout<<endl<<endl;
cout<<"May your anniversary be the best yet and we wish you more to come\n";
cout<<endl<<endl;
cout<<"Love,\n";
cout<<"Joanna\n";
}
void AddressBook::userPromptStatement()
{
cout<<"Enter 1 to add a new contact"<<endl;
cout<<"Enter 2 to delete a contect"<<endl;
cout<<"Enter 3 edit a contect"<<endl;
cout<<"Enter 4 to print your address book"<<endl;
cout<<"Enter 5 to generate birthday cards"<<endl;
cout<<"Enter 6 to generate anniversary cards"<<endl;
cout<<"Enter 7 to exit the program"<<endl;
}
void AddressBook::ExitProgram()
{
EXIT_SUCCESS;
}
Code:
#include <iostream>
#include <string>
#include "addressList.h"
using namespace std;
int main()
{
AddressBook MyAddressBook;
int userInput;
//string NAME, BIRTHDAY, ADDY, NEWANIVERSARY;
nodePtr temp=new node;
MyAddressBook.userPromptStatement();
string NAME;
while(1)
{
cin>>userInput;
switch (userInput) {
case 1:
cout<<"NAME: ";
cin.ignore();
getline(cin,temp->Name,'\n');
cout<<"\nBIRTHDAY: ";
getline(cin,temp->DOB,'\n');
cout<<"\nADDRESS: ";
getline(cin,temp->address,'\n');
cout<<"\nANIVERSARY DATE: ";
getline(cin,temp->aniversary,'\n');
MyAddressBook.AddNode(temp);
MyAddressBook.userPromptStatement();
break;
case 2:
cout<<"Which contact would you like to delete?\n";
cin>>NAME;
MyAddressBook.deleteName(NAME);
MyAddressBook.userPromptStatement();
break;
case 3:
MyAddressBook.EditNameOrDate(); //need help here
break;
case 4:
MyAddressBook.PrintAddressBook();
MyAddressBook.userPromptStatement();
break;
case 5:
cout<<"Who would you like to send a Birthday card too?\n";
cin>>NAME;
MyAddressBook.GenBirthdayCards(NAME);
MyAddressBook.userPromptStatement();
break;
case 6:
cout<<"Who would you like to send a anniversary card too?\n";
cin>>NAME;
MyAddressBook.GenAnnCard(NAME);
MyAddressBook.userPromptStatement();
break;
case 7:
MyAddressBook.ExitProgram();
break;
default:
break;
}
}
return 0;
}
The errors are:
Lets say I enter Number 3 to edit a contact line 99 comes up with an error "Thread 1: EXE_BAD_ACCESS (code=1,address=0x60)". I do not even know what that means.
Delete Contact also is giving me errors. I am can't seem to see the error. The while loop on 99, 114, 130. I get the following error when I try to delete a name:
Linked Lists(5607,0x7fff73e1d310) malloc: *** error for object 0x100103b40: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug