C++ : Please include complete source code in answer This program will have names
ID: 3749956 • 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 <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
struct address_Book {
string fname;
string lname;
string address;
string phn_Num;
};
char get_MenuResponse();
void addContacts(vector<address_Book>&);
void dispContacts(const vector<address_Book>&);
void saveFile(const vector<address_Book>&);
void openFile(vector<address_Book>&);
int findTargetFirstName(vector<address_Book>& list, string target);
void findContact(vector<address_Book>&);
void sort_Fnames(vector<address_Book>& list);
int main() {
vector<address_Book> list;
bool runn = true;
cout << "Welcome To My Address Book Program For Assignment 2! " << endl;
do {
switch (get_MenuResponse()) {
case 'A': addContacts(list); break;
case 'D': dispContacts(list); break;
case 'M': sort_Fnames(list); break;
case 'O': openFile(list); break;
case 'S': saveFile(list); break;
case 'F': findContact(list); break;
case 'Q': runn = false; break;
default : cout << "That is NOT a valid choice" << endl;
}
} while (runn);
cout << endl << "Program Terminated" << endl;
return EXIT_SUCCESS;
}
void sort_Fnames(vector<address_Book>& list) {
string fname;
int i, j, minm;
bool sortd = false;
bool runn = true;
if(list[i].fname > 1) {
for (i = 0; i < (list[i].fname-1); i++) {
minm = i;
for (j = (i+1); j < list[i].fname; j++) {
if(list[j] < list[minm]) minm = j;
}
if (i != minm) swap(list[i], list[minm]);
}
sortd = true;
}
}
int findTarget_firstname(vector<address_Book>& list, string target) {
for (int i = 0; i < list.size(); i++)
if (list[i].fname == target) return i;
return -1;
}
void findContact(vector<address_Book>& list) {
if(list.size() > 0) {
string fname;
cout << "Enter the first name to find: ";
getline(cin, fname);
int ndx = findTarget_firstname(list, fname);
if (ndx >= 0) {
cout << fname << " is at index " << ndx << "." << endl;
} else cout << fname << " is not found!" << endl;
} else cout << "That name is not found!" << endl;
}
void addContacts(vector<address_Book>& list) {
address_Book temp;
char respnse;
string strng;
bool runn = true;
do {
system("cls");
cout << "Enter Contact Information" << endl;
cout << "Enter 'quit' at Name to exit." << endl << endl;
cout << "First Name: ";
getline(cin, strng);
temp.fname = strng;
cout << endl;
if (strng == "quit") break;
cout << "Last Name: ";
getline(cin, strng);
temp.lname = strng;
cout << endl;
cout << "Address: ";
getline(cin, strng);
temp.address = strng;
cout << endl;
cout << "phn_Num Number: ";
getline(cin, strng);
temp.phn_Num = strng;
cout << endl;
cout << "Add Contact to Address Book? (y/n) ";
cin >> respnse;
cin.ignore(256, ' ');
if (toupper(respnse) == 'Y') {
list.push_back(temp);
cout << "Contact added!"<< endl;
}else {
cout << "Contact not added!" << endl;
}
} while (runn);
system("cls");
}
void dispContacts(const vector<address_Book>& list) {
system("cls");
if(list.size() < 1) {
cout << "Nothing to display" << endl;
} else {
cout << "Contacts :" << endl << endl;
cout << fixed << setprecision(2);
cout << "Contact Name Address phn_Num No." << endl;
cout << "*******************************************" << endl;
cout << left;
for (int i = 0; i < list.size(); i++) {
cout << setw(15) << list[i].fname << left
<< setw(15) << list[i].lname << left
<< setw(30) << list[i].address << left
<< setw(15) << list[i].phn_Num << left << endl;
}
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << right << setw(3) << list.size();
cout << " Contacts"<< endl;
}
system("PAUSE");
system("cls");
}
void saveFile(const vector<address_Book>& list) {
string file_name;
ofstream outfi;
cout<<"Enter file name (please use address_Book.txt for now): ";
getline(cin,file_name);
outfi.open(file_name.c_str());
if (!outfi.fail()) {
system("cls");
cout << "Saving Address Book to the disc ";
for(int i = 0; i < list.size(); i++) {
outfi << list[i].fname << ';'
<< list[i].lname << ';'
<< list[i].address << ';'
<< list[i].phn_Num;
if (i < list.size()-1) outfi << endl;
}
cout << endl << list.size() << " Address Book written to the disc." << endl;
outfi.close();
system("PAUSE");
system("cls");
} else {
cout << "ERROR: problem with file" << endl;
system("PAUSE");
system("cls");
}
}
void openFile(vector <address_Book>& list) {
address_Book temp;
if (list.size() > 0) {
char respnse;
cout << "Are you sure? If you have already added contacts, this will "
<< "overwrite what you have entered with a previously saved "
<< "address_Book.txt." << endl;
cout << "Do you wish to proceed? (Y/N) : ";
cin >> respnse;
cin.ignore(256, ' ');
if(toupper(respnse) == 'N') return;
}
ifstream infi("address_Book.txt");
string strng;
if (!infi.fail()) {
system("cls");
cout << "Reading Address Book from the disc ";
list.clear();
getline(infi, strng, ';');
while(!infi.eof()) {
temp.fname = strng;
getline(infi, strng, ';');
temp.lname = strng;
getline(infi, strng, ';');
temp.address = strng;
getline(infi, strng);
temp.phn_Num = strng;
list.push_back(temp);
getline(infi, strng, ';');
}
cout << endl << list.size() << " Contacts read from the disc." << endl;
system("PAUSE");
system("cls");
} else {
cout << "ERROR: problem with file" << endl;
system("PAUSE");
system("cls");
}
}
char get_MenuResponse() {
char respnse;
cout << endl << "Make your selection" << endl
<< "(A)dd contact, (D)isplay Contacts, (M)Sort By First Name, "
<< " (O)pen File, (S)ave File, "
<<" (F)ind Index Of Contact By First Name, (Q)uit" << endl
<< "> ";
cin >> respnse;
cin.ignore(256, ' ');
return toupper(respnse);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.