A) This program will have names and addresses saved in a linked list. In additio
ID: 3577047 • Letter: A
Question
A) This program will have names and addresses saved in a linked list. In addition, a birthday and anniversary date will be saved with each record.
When the program is run, it will search for a birthday or an anniversary using the current date to compare with the saved data. It will then generate the appropriate card.
Because this will be an interactive system, your program should begin by displaying a menu. Items on the menu should include:
Enter a new name into the address book
Delete a name from the address book
Change a name or date in the address book
Generate birthday cards
Generate anniversary cards
Exit the card program
Each of these sections will call individual functions to perform their appropriate task.
This address book is to be sorted in alphabetical order. Be aware of this when you are entering, deleting, or changing the name, and plan your code accordingly.
For this project, create and display the card created on the screen (you do not need to print it). You may design the layout of the card as you wish. For example, it could be:
Dear <name_saved>,
Hope your birthday is really wonderful and this coming year is the best yet!
Love,
Joanne
B) DELIVERABLES
Your C++ source code with any header files
Your executable code
A document detailing how you will test your program
An overview document, giving the name of each file submitted and its purpose, as well as a few paragraphs on your experiences coding the linked lists
N/B. Please the part B was not answered during my first request, I really want both A and B answered Please. Thank you.
Explanation / Answer
Part A:
THE CODE:
#include<iostream>
#include<cstring>
#include<ctime>
using namespace std;
string getmonth[] = {"January","February","March","April","May","June","July","August","September","October","November", "December"};
typedef struct node{
char fname[20]; char lname[20];//for name
char address[40]; char city[40]; char state[40];//for address
int bd; int bm; int by;//for birthday
int ad; int am; int ay;//for anniversary
struct node *next;
};
class records
{
private:
struct node *head;
public:
records()
{
head = 0;
}
void new_entry(struct node&);
void delrecord();
void changerecord();
void display();
void gen_b_cards();
void gen_a_cards();
};
void takeinput(struct node& temp);
int main()
{
records obj;
struct node a = {"Tom","Machete","S12 - NYC","New York","Washington",4,12,2009,1,1,2001,NULL};
obj.new_entry(a);
struct node b = {"Mike","Gieg","13 Baker Street","London","England",11,10,2009,1,1,2001,NULL};
obj.new_entry(b);
struct node c = {"Mr","Incredible","Ohio","Ohio","USA",4,1,2009,11,10,2001,NULL};
obj.new_entry(c);
struct node d = {"Fake","Naruto","Bakemono","hiroshima","Japan",4,1,2009,11,10,2001,NULL};
obj.new_entry(d);
struct node e = {"One","Piece","ICHIMICHI","Tokyo","Nihou",14,1,2009,1,12,2001,NULL};
obj.new_entry(e);
int choice;
do
{
do{
cout<<endl<<"***********Menu************"<<endl;
cout<<"Enter a new name into the address book(Enter 1) ";
cout<<"Delete a name from the address book(Enter 2) ";
cout<<"Change a name or date in the address book(Enter 3) ";
cout<<"Display the whole address book(Enter 4) ";
cout<<"Generate birthday cards(Enter 5) ";
cout<<"Generate anniversary cards(Enter 6) ";
cout<<"Exit the card program(Enter 7): ";
cin>>choice;
if(choice<=0||choice>=8)
{
cout<<"The value entered is invalid. Valid numbers are from 1 to 7.";
}
}while(choice<=0||choice>=8);
cout<<endl;
switch(choice)
{
case 1:
struct node temp;
takeinput(temp);
obj.new_entry(temp);
break;
case 2:obj.delrecord();
break;
case 3:obj.changerecord();
break;
case 4:obj.display();
break;
case 5:obj.gen_b_cards();
break;
case 6:obj.gen_a_cards();
break;
}
}while(choice!=7);
return 0;
}
void records::new_entry(struct node& a)
{
struct node *temp = new struct node;
*temp = a;
//Adding the node in the linked list
if(head==NULL)//If linked list is empty
{
head = temp;
temp->next = NULL;
}
else//Traverse the list to find the location to enter this node
{
struct node *trav,*prev;
trav = head;
prev = NULL;//To insert before trav, we need node before it.
while(trav!=NULL)
{
if((strcmp(trav->lname,temp->lname)>0) || ((strcmp(trav->lname,temp->lname)==0) && (strcmp(trav->fname,temp->fname)>0)) )
break;
else
{
prev = trav;
trav = trav->next;
}
}
if(prev == NULL)//if node has to be inserted at beginning.
{
temp->next = head;
head = temp;
}
else//connect node's next to the trav and prev's next to the node.
{
temp->next = prev->next;
prev->next = temp;
}
}
}
void records::delrecord()
{
char first[20],last[20];
struct node *trav, *prev;
cout<<"Enter the following information to delete the record."<<endl;
cout<<"Enter the first name: ";
cin>>first;
cout<<"Enter the last name: ";
cin>>last;
trav = head;
prev = NULL;
while(trav!=NULL)
{
if(strcmp(first,trav->fname)==0 && strcmp(last,trav->lname)==0)//if a match is found.
{
if(prev == NULL) //if the first node was match, change the head.
{
head = trav->next;
}
else
{
prev->next = trav->next; //remove the matching node, from the linked list.
}
delete trav; //free the meomory occupied by the node found.
cout<<"The record has been deleted."<<endl;
break;
}
else
{
prev = trav;
trav = trav->next;//
}
}
if(trav==NULL) //if trav has reached the end, the node should be deleted.
{
cout<<"No such record found."<<endl;
}
}
void records::changerecord()
{
char first[20],last[20];
struct node* temp, *trav, *prev;
struct node a;
cout<<"Enter the first name of the person whose record has to be changed: ";
cin>>first;
cout<<"Enter the last name of the person whose record has to be changed: ";
cin>>last;
trav = head;
prev = NULL;
while(trav!=NULL)
{
if(strcmp(first,trav->fname)==0 && strcmp(last,trav->lname)==0)
{
if(prev == NULL)
{
head = trav->next;
}
else
{
prev->next = trav->next;
}
delete trav;
break;
}
else
{
prev = trav;
trav = trav->next;//
}
}
if(trav==NULL)
{
cout<<"No such record was found."<<endl;
}
else
{
takeinput(a); //if a matching node is found, delete it and re-enter a new node.
new_entry(a);
}
}
void records::display()//pointer trav will traverse the linked list and print each node till it is null.
{
struct node *temp;
temp = head;
char c;
int user_num = 1;
if(temp == NULL)
{
cout<<"The record list is empty."<<endl;
}
while(temp!=NULL)
{
//Printing node information.
cout<<"***********Record "<<user_num<<"************"<<endl;
cout<<temp->lname<<", "<<temp->fname<<endl;
cout<<temp->city;
cout<<", ";
cout<<temp->state<<endl;
cout<<"Birthday: ";
if(temp->by!=-1)
{
cout<<temp->by<<" ";
}
cout<<getmonth[(temp->bm)-1]<<" "<<temp->bd<<endl;
cout<<"Anniversary: ";
if(temp->ay!=-1)
{
cout<<temp->ay<<" ";
}
cout<<getmonth[(temp->am)-1]<<" "<<temp->ad<<endl;
//Printing Complete
user_num++;
temp = temp->next;
}
}
void records::gen_b_cards()
{
bool flag = false;
time_t seconds;
int day,month;
seconds = time(NULL);
time_t t = time(0); // get time now - inbuilt function.
struct tm * now = localtime( & t );
month = (now->tm_mon + 1); //calculate month.
day = now->tm_mday; //calculate day.
struct node *temp;
temp = head;
while(temp!=NULL)
{
if(temp->bd == day && temp->bm == month)
{
flag = true;
cout<<"Dear "<<temp->fname<<" "<<temp->lname<<" Hope your birthday is really wonderful and this coming year is the best yet! Love, Me :D ";
}
temp = temp->next;
}
if(!flag)
cout<<"Today nobody has birthday."<<endl;
}
void records::gen_a_cards()
{
bool flag =false;
time_t seconds;
int day,month;
seconds = time (NULL);
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
month = (now->tm_mon + 1);
day = now->tm_mday;
struct node *temp;
temp = head;
while(temp!=NULL)
{
if(temp->ad == day && temp->am == month)
{
cout<<"Dear "<<temp->fname<<" "<<temp->lname<<" Hope your anniversary is really wonderful. Love, Me :D ";
flag = true;
}
temp = temp->next;
}
if(!flag)
cout<<"Today nobody has anniversary."<<endl;
}
void takeinput(struct node& temp)
{
temp.next = NULL;
char c;
//Taking inputs for name and address
cout<<"Enter the first name: ";
cin>>temp.fname;
cout<<"Enter the last name: ";
cin>>temp.lname;
c = getchar(); //To remove trailing enter key after cout.
cout<<"Enter the address: ";
gets(temp.address);
cout<<"Enter the city: ";
gets(temp.city);
cout<<"Enter the state: ";
gets(temp.state);
//Taking birthday input and its validation
do
{
cout<<"Enter the date of birthday(1-31): ";
cin>>temp.bd;
cout<<"Enter the month of birthday(1-12): ";
cin>>temp.bm;
if((temp.bd<1||temp.bd>31)||(temp.bm<1||temp.bm>12))
{
printf("Invalid Birthday. Valid inputs for date are 1-31 and for month are 1-12. ");
}
}while((temp.bd<1||temp.bd>31)||(temp.bm<1||temp.bm>12));
cout<<"Do you want to enter the birthday year?(Enter y/n): ";
cin>>c;
if(c=='y'||c=='Y')
{
do
{
cout<<"Enter the birthday year: ";
cin>>temp.by;
if(temp.by<1900)
{
cout<<"Invalid year. Valid range is greater than 1900.";
}
}while(temp.by<1900);
}
else
{
temp.by = -1;
}
//Taking anniversary date input and its validation
do
{
cout<<"Enter the date of anniversary(1-31): ";
cin>>temp.ad;
cout<<"Enter the month of anniversary(1-31): ";
cin>>temp.am;
if((temp.ad<1||temp.ad>31)||(temp.am<1||temp.am>12))
{
printf("Invalid anniversary date. Valid inputs are date 1-31, month 1-12 and year>1900. ");
}
}while((temp.ad<1||temp.ad>31)||(temp.am<1||temp.am>12));
cout<<"Do you want to enter the anniversary year?(Enter y/n): ";
cin>>c;
if(c=='y'||c=='Y')
{
do
{
cout<<"Enter the anniversary year: ";
cin>>temp.ay;
if(temp.ay<1900)
{
cout<<"Invalid year. Valid range is greater than 1900.";
}
}while(temp.ay<1900);
}
else
{
temp.ay = -1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.