Very sorry for the late response. I normally respond within an hour. I have redone a lot and will post the whole code. It is all working and now I am tweaking a few things. How is my design? What could be better?
Code:
#ifndef __addressList__
#define __addressList__
#include <iostream>
#include <string>
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 Initialize();
void userPromptStatement();
void AddNode(nodePtr);
void deleteName(string name);
void EditNameOrDate();
void PrintAddressBook();
void GenBirthdayCards(string);
void GenAnnCard(string);
void ExitProgram();
};
#endif
Code:
#include <iostream>
#include <string>
#include "addressList.h"
using namespace std;
AddressBook::AddressBook(){
head=NULL;
current=NULL;
temp=NULL;
}
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 name)
{
if (!head) cout<<"The List is empty\n";
else
{
nodePtr current=head;
if (head->Name==name)
{
current=head->next;
head=nullptr;
delete head;
head=current;
cout<<"complete "<<name<<" has been deleted\n";
}
else
{
nodePtr temp=nullptr;
current=head;
bool found=false;
while (current->next)
{
if (current->next->Name==name)
{
temp=current->next->next;
delete current->next;
current->next=temp;
found=true;
cout<<"complete "<<name<<" has been deleted\n";
}
}
if (!found) cout<<"The name "<<name<<" not found in the list\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()
{
if (!head) cout<<"The list is empty\n";
else
{
cout<<"1. Edit Name: \n";
cout<<"2. Edit Birthday: \n";
cout<<"3. Edit Anniversary: \n";
int input;
cin>>input;
if (input==1)
{
cout<<"Enter the name you want to edit: /n";
nodePtr curr = head;
string name;
name.clear();
cin.ignore();
getline(cin,name,'\n');
while(curr!= NULL && curr->Name != name)
{
curr=curr->next;
}
if (!curr) cout<<"The name "<<name<<" wasn't found\n";
else
{
cout<<"The current name is: "<<curr->Name<<endl;
cout<<"Enter the new name: ";
name.clear();
getline(cin,name,'\n');
curr->Name=name;
cout<<"The name has been successfully changed to "<<curr->Name<<endl;
}
}
else if (input==2)
{
cout<<"Enter the Birthday you want to edit: ";
string date;
date.clear();
cin.ignore();
getline(cin,date,'\n');
nodePtr curr=head;
while(curr!=NULL && curr->DOB!=date)
{
curr=curr->next;
}
if (!curr) cout<<"The date "<<date<<" wasn't found\n";
cout<<"The current date is: "<<curr->DOB<<endl;
cout<<"Enter the new name: \n";
cin.clear();
cin.ignore();
getline(cin, date, '\n');
curr->DOB=date;
cout<<"The date has been successfully changed to "<<curr->DOB<<endl;
}
else if (input==3)
{
cout<<"Enter the Anniversary date you want to edit: ";
string AnnDate;
AnnDate.clear();
cin.ignore();
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: \n";
AnnDate.clear();
cin.ignore();
getline(cin,AnnDate,'\n');
curr->aniversary=AnnDate;
cout<<"The name has been successfully changed to "<<curr->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;
MyAddressBook.userPromptStatement();
int userInput;
nodePtr temp=new node;
string NAME;
while(1)
{
cin>>userInput;
if (!(cin>>NAME)) {
cerr<<"Only enter integers, please\n\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n'); //prevents a never ending loop
MyAddressBook.userPromptStatement();
}
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.ignore();
getline(cin, temp->Name, '\n');
MyAddressBook.deleteName(temp->Name);
MyAddressBook.userPromptStatement();
break;
case 3:
MyAddressBook.EditNameOrDate();
MyAddressBook.userPromptStatement();
break;
case 4:
MyAddressBook.PrintAddressBook();
MyAddressBook.userPromptStatement();
break;
case 5:
cout<<"Who would you like to send a Birthday card too?\n";
getline(cin, temp->Name, '\n');
MyAddressBook.GenBirthdayCards(NAME);
MyAddressBook.userPromptStatement();
break;
case 6:
cout<<"Who would you like to send a anniversary card too?\n";
getline(cin, temp->Name, '\n');
MyAddressBook.GenAnnCard(NAME);
MyAddressBook.userPromptStatement();
break;
case 7:
MyAddressBook.ExitProgram();
break;
default:
break;
}
}
return 0;
}
How would I use list?
Code:
struct addressBook
{
std::string name;
std::string age;
std::string streetAddress;
};
class MyAddressBook {
private:
std::list<addressBook> Myaddress;
public:
MyAddressBook();
~MyAddressBook();
void addContact(std::string);
void deleteContact();
};
Would I just push data into it and everything is done? I just found this one and after reading and reading online and watching youtube. I cannot see how to use it like I used it above. Can anyone shed some light on that one for me.
In my other code I am having an issue with the if statement in the main.cpp. I wanted to check if the user entered a char and if so throw an error. It works however the user has to enter an int twice before the list of requests shows up. So if I want to enter a new contact I have to enter 1, twice. It is an annoying flaw that I would like to fix. Any advice?