Only a few changes here. I removed the string arguments to the methods that had then as the temp node can be used for that
I added an initialize method, a ProcessInput method and a destructor. I also capitalized several names for consistency.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#ifndef __addressList__
#define __addressList__
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class AddressBook
{
private:
typedef struct node {
string Name;
string DOB;
string Address;
string Aniversary;
struct node *next;
} *nodePtr;
nodePtr head;
nodePtr current;
nodePtr temp;
void AddNode(nodePtr);
void DeleteName();
void EditNameOrDate();
void PrintAddressBook();
void GenBirthdayCards();
void GenAnnCard();
void ExitProgram();
public:
AddressBook();
void Initialize();
void ProcessInput();
void UserPromptStatement();
~AddressBook();
};
#endif // __addressList__
|
The main method has been simplified and the user input placed in a ProcessInput file (and method)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include "AddressList.h"
using namespace std;
int main()
{
AddressBook AdrBk;
AdrBk.Initialize();
AdrBk.ProcessInput();
}
AddressBook::AddressBook()
{
// Create class object
}
AddressBook::~AddressBook()
{
// Delete class object
delete temp;
}
void AddressBook::ExitProgram()
{
EXIT_SUCCESS;
}
|
The AddNode method has been simplified somewhat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include "AddressList.h"
void AddressBook::AddNode(nodePtr current)
{
nodePtr hold = new node;
current = NULL;
if(head) {
current = head;
while(current)
{
hold = current;
current = current->next;
}
current = hold->next = new node;
} else {
current = head = new node;
}
current->next = NULL;
current->Address = temp->Address;
current->Aniversary = temp->Aniversary;
current->DOB = temp->DOB;
current->Name = temp->Name;
}
|
You forgot to update the next link of the node just before the one deleted, I added that in the delete name method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
include "AddressList.h"
void AddressBook::DeleteName()
{
nodePtr Hold = new node;
Hold = head;
if(head) {
current = head;
while(current) {
if(current->Name == temp->Name) {
Hold->next = current->next;
delete current;
cout<<"Name: "<< temp->Name <<" was deleted.\n";
break;
}
Hold = current;
current = current->next;
}
}
if(head == NULL || current == NULL)
cout<<temp->Name<<"Was not in the list\n";
}
|
The Initialize method
1 2 3 4 5 6 7 8
|
#include "AddressList.h"
void AddressBook::Initialize()
{
temp = new node;
head = NULL;
current = NULL;
}
|
ProcessInput Looks pretty much the same except for the removal of string arguments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include "AddressList.h"
void AddressBook::ProcessInput()
{
string NAME;
int userInput;
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');
AddNode(temp);
UserPromptStatement();
break;
case 2:
cout<<"Which contact would you like to delete?\n";
getline(cin,temp->Name,'\n');
DeleteName();
UserPromptStatement();
break;
case 3:
EditNameOrDate(); //need help here
break;
case 4:
PrintAddressBook();
UserPromptStatement();
break;
case 5:
cout<<"Who would you like to send a Birthday card too?\n";
getline(cin,temp->Name,'\n');
GenBirthdayCards();
UserPromptStatement();
break;
case 6:
cout<<"Who would you like to send a anniversary card too?\n";
getline(cin,temp->Name,'\n');
GenAnnCard();
UserPromptStatement();
break;
case 7:
ExitProgram();
break;
default:
break;
}
}
}
|
Get Annaversary cards would look like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include "AddressList.h"
void AddressBook::GenAnnCard()
{
current = head;
while(current && (current->Name != temp->Name) && (current->Aniversary != temp->Aniversary)) {
current++;
}
if(current) {
cout<<"Dear "<<temp->Name<<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";
}
}
|
The other functions basicaly the same.
You still have the situation where there will be identical names. I suggest that you add a 16 bit hash code to your node typedef. the structure is so small that woud could add all fields ti get a unique code.
When searching for a particular person, you could bring up the names and street where they lived and have the user select the proper one.
Hope ypu find this helpful.
Largins