C++ : Please include complete source code in answer This program will have names
ID: 3749716 • Letter: C
Question
C++ : Please include complete source code in answer 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 date. It will then generate the appropriate card message. 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 • Display the whole 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. Use classes and objects where appropriate. Be sure to comment your code and use appropriate variable, function, and class names so that it is clear how the program flows. The user gets the menu and creates information to be stored in your address book. (To save sanity in testing, you may fill in 5 or 6 records within the code or written to a file to start your address book.) The user can add a new record to your existing ones, delete a record, modify a record or print the whole address book on the screen. For each of the options, make sure you give suitable prompts on the screen so that the user knows what is expected by the program. Expect that the user will enter both a birthday and anniversary date. (If you’d like, you can handle the situation where the user chooses not to enter an anniversary date.) A year may be entered or omitted in birthday and anniversary dates. Also be sure to redisplay the menu any time a function has concluded, until the user presses the Exit option. 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 , Hope your birthday is really wonderful and this coming year is the best yet! Love, Joanne “Generate Birthday Cards” and “Generate Anniversary Cards” will use the system date (today) as the date to match. Using this date, display the card below it on the screen. Be ready to print multiple cards if more than one birthday or anniversary falls on the same day. “Display the Whole Address Book” will be done on the screen. Output should look something like: Wilson, Fred 123 Main Street Anytown, NJ 00000 Birthday: May 7 Anniversary: June 25 Or include the structure variable names you’ve used: lname: Wilson fname: Fred addr: 123 Main Street city: Anytown state: NJ zip: 00000 bday: May 7 aday: June 25
Explanation / Answer
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
char current;
class personDetails
{
public:
personDetails(string newName)
{
PName = newName;
next_person = 0;
}
string PName;
string address;
string Bday;
string anniversery;
personDetails *next_person;
};
personDetails* p_HeadNode;
void DrawMenu()
{
cout << "------------Menu-------------" << endl;
cout << "1. Enter a personDetails to the List" << endl;
cout << "2. Delete a personDetails from the List" << endl;
cout << "3. Change a personDetails's PName in the List" << endl;
cout << "4. Display Entries" << endl;
cout << "5. Locate personDetails by ID Number" << endl;
cout << "6. Locate personDetails by Name" << endl;
cout << "7. Load List from File" << endl;
cout << "8. Save List to the File" << endl;
cout << "9. Exit the program" << endl;
cout << "" << endl;
cout << "You choice: ";
}
personDetails* FindName(string p_name)
{
if (p_HeadNode)
{
personDetails *p_currentNode = p_HeadNode;
while(p_currentNode && p_currentNode->p_name!=p_name)
{
if (p_currentNode->p_name==p_name)
return p_currentNode;
p_currentNode = p_currentNode->next_person;
}
}
return 0;
}
void InsertNewNode(personDetails* p_newNode)
{
if (!p_HeadNode || (p_newNode->p_name < p_HeadNode->p_name))
{
p_newNode->next_person = p_HeadNode;
p_HeadNode = p_newNode;
} else {
personDetails *p_currentNode = p_HeadNode;
personDetails *pPrevNode = p_currentNode;
while(p_currentNode && p_currentNode->p_name < p_newNode->p_name)
{
pPrevNode = p_currentNode;
p_currentNode = p_currentNode->next_person;
}
p_newNode->next_person = pPrevNode->next_person;
pPrevNode->next_person = p_newNode;
}
}
void AddNewEntry()
{
string p_name;
string address;
cout << "Enter p_name of new Entry: ";
getline(cin, p_name);
cin.clear();
cin.sync();
getline(cin, p_name);
if (p_HeadNode)
{
if (FindName(p_name))
{
cout << "" << endl;
cout << "Name already exists";
cout << "" << endl;
return;
}
}
cout << "Enter ID Number for new Entry: ";
getline(cin, address);
if (p_HeadNode)
{
personDetails* p_newNode = new personDetails(p_name);
p_newNode->address = address;
InsertNewNode(p_newNode);
} else {
p_HeadNode = new personDetails(p_name);
p_HeadNode->address = address;
}
cout << "" << endl;
cout << "New Entry is added" << endl;
cout << "" << endl;
}
void DeleteEntry()
{
string p_name;
if (p_HeadNode)
{
cin.clear();
cin.sync();
cout << "Enter p_name of Entry you want to delete: ";
getline(cin, p_name);
personDetails *p_currentNode = p_HeadNode;
personDetails *pPrevNode = p_currentNode;
while(p_currentNode && p_currentNode->p_name!=p_name)
{
if (p_currentNode->p_name==p_name)
break;
p_currentNode = p_currentNode->next_person;
}
if (p_currentNode)
{
if (p_currentNode==p_HeadNode)
{
p_HeadNode = p_currentNode->next_person;
} else {
pPrevNode->next_person = p_currentNode->next_person;
}
delete p_currentNode;
} else {
cout << "" << endl;
cout << "Entry is not found" << endl;
cout << "" << endl;
}
} else {
cout << "" << endl;
cout << "Address book is empty already" << endl;
cout << "" << endl;
return;
}
}
void ChangeEntry()
{
string p_name;
string address;
if (p_HeadNode)
{
cin.clear();
cin.sync();
cout << "Enter p_name of Entry you want to edit: ";
getline(cin, p_name);
personDetails *p_currentNode = p_HeadNode;
personDetails *pPrevNode = p_currentNode;
while(p_currentNode && p_currentNode->p_name!=p_name)
{
if (p_currentNode->p_name==p_name)
break;
p_currentNode = p_currentNode->next_person;
}
if (p_currentNode)
{
cin.clear();
cin.sync();
cout << "Enter p_name of new Entry: ";
getline(cin, p_name);
if (p_HeadNode)
{
if (FindName(p_name))
{
cout << "" << endl;
cout << "Name already exists";
cout << "" << endl;
return;
}
}
if (p_currentNode==p_HeadNode)
{
p_HeadNode = p_currentNode->next_person;
} else {
pPrevNode->next_person = p_currentNode->next_person;
}
delete p_currentNode;
cout << "" << endl;
cout << "Entry is changed" << endl;
cout << "" << endl;
} else {
cout << "" << endl;
cout << "Entry is not found" << endl;
cout << "" << endl;
}
} else {
cout << "" << endl;
cout << "Address book is empty " << endl;
cout << "" << endl;
return;
}
}
void DisplayEntries()
{
string getcontent;
ifstream openfile ("PROG05.txt");
if(openfile.is_open())
{
while(! openfile.eof())
{
getline(openfile, getcontent);
cout << getcontent << endl;
}
}
}
void SearchEntriesName()
{
string p_name;
string address;
if (p_HeadNode)
{
cin.clear();
cin.sync();
cout << "Enter p_name of Entry you want to Search: ";
getline(cin, p_name);
personDetails *p_currentNode = p_HeadNode;
personDetails *pPrevNode = p_currentNode;
while(p_currentNode && p_currentNode->p_name!=p_name)
{
if (p_currentNode->p_name==p_name)
break;
p_currentNode = p_currentNode->next_person;
}
cout << p_currentNode->p_name << endl;
cout << p_currentNode->address << endl;
}
}
void SearchEntriesaddress()
{
string p_name;
string address;
if (p_HeadNode)
{
cin.clear();
cin.sync();
cout << "Enter the ID Number of the entry you want to search: ";
getline(cin, address);
personDetails *p_currentNode = p_HeadNode;
personDetails *pPrevNode = p_currentNode;
while(p_currentNode && p_currentNode->address!=address)
{
if (p_currentNode->address==address)
break;
p_currentNode = p_currentNode->next_person;
}
cout << p_currentNode->p_name << endl;
cout << p_currentNode->address << endl;
}
}
void SaveListToFile()
{
ofstream outfile;
outfile.open("PROG05.txt");
personDetails* p_currentNode = p_HeadNode;
while(p_currentNode)
{
personDetails* pNode = p_currentNode;
p_currentNode = p_currentNode->next_person;
outfile << pNode->p_name << endl;
outfile << pNode->address << endl;
}
outfile.close();
}
void LoadListFromFile()
{
ifstream infile;
infile.open("PROG05.txt");
string p_name;
while (getline(infile, p_name))
{
string address;
getline(infile, address);
if (!p_HeadNode)
{
p_HeadNode = new personDetails(p_name);
p_HeadNode->address = address;
} else {
personDetails *p_currentNode = p_HeadNode;
personDetails *pPrevNode = p_currentNode;
while(p_currentNode)
{
pPrevNode = p_currentNode;
p_currentNode = p_currentNode->next_person;
}
pPrevNode->next_person = new personDetails(p_name);
pPrevNode->next_person->address = address;
}
}
infile.close();
}
int main()
{
int choice;
bool stop = false;
LoadListFromFile();
do
{
do
{
DrawMenu();
cin >> choice;
if (choice<1 || choice>9)
cout << "Incorrect choice. Try again" << endl << endl;
}while(choice<1 || choice>9);
switch(choice)
{
case 1:
AddNewEntry();
SaveListToFile();
break;
case 2:
DeleteEntry();
SaveListToFile();
break;
case 3:
ChangeEntry();
SaveListToFile();
break;
case 4:
DisplayEntries();
break;
case 5:
SearchEntriesaddress();
break;
case 6:
SearchEntriesName();
break;
case 7:
LoadListFromFile();
DisplayEntries();
cout << "List has been Loaded" << endl;
break;
case 8:
SaveListToFile();
cout << "List has been Saved to the File" << endl;
break;
case 9:
cout << "Thank you for using the program" << endl;
stop = true;
break;
default:
break;
}
}while(!stop);
SaveListToFile();
personDetails* p_currentNode = p_HeadNode;
while(p_currentNode)
{
personDetails* pNode = p_currentNode;
p_currentNode = p_currentNode->next_person;
delete pNode;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.