In c++ with proper headers, see previous assignment layout for build, cannot con
ID: 3916190 • Letter: I
Question
In c++ with proper headers, see previous assignment layout for build, cannot convert my old book to link list, this is due soon.
/////////////////
This is the new assignment
Using classes and linked lists, design an Online Address Book to keep track of the names, addresses,phone numbers, and dates of birth of family members, close friends, and certain business associates.Your program should be able to handle as many entries as required.
1. By the Assignment 2, you have created the user defined data type extPersonType
2. Define the class addressBookType to built a linked list. The value of each node has the type of extPersonType . An object of the type addressBookType
should be able to process as many entries each of them as required.
3. Write a C++ program to test your design. The program should perform the following operations:
Add a new entry to a address book.
Delete an entry from a address book.
Print the value of each node of a address book.
//////////////////////////////////////////////////////////////////////////
This was the previous assignment.
Using classes and arrays, design an Online Address Book to keep track of the names, addresses, phone numbers, and dates of birth of family members, close friends, and certain business associates.
Your program should be able to handle as many entries as required.
1. Define a class,addressType, that can store a street address, city, state, and ZIP code. Use the appropriate functions to print and store the address. Also, use constructors to automatically initialize the member variables.
2. Define a class, personType , that can store the first name and the last name of a person. Use e appropriate functions to print and store the first and the last name. Also, use constructors to automatically initialize the member variables.
3. Define a class, dataType, that can store the day, the month, and the year of a date. Use the appropriate functions to print and store the day, the month, and the year. Also, use constructors to automatically initialize the member variables.
4. Define a class extPersonType . The class is a derived class from the above classes. The class adds a member variable to classify the person as a family member, friend, or business associate. Also, the class adds a member variable to store the phone number. Moreover, the class adds (or override ) fuctions to print and store the appropriate information. Use constructors to automatically initialize the member variables.
5. Define the class addressBookType using the previously defined classes. An object of the type addressBookType should be able to process as many entries as required.
6. Write a C++ program to test your design. The program should perform the following operations:
–
Add a new entry to the address book.
–
Delete an entry from the address book.
–
Print all the information of the address book
Explanation / Answer
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
struct AddressBook {
string firstname;
string lastname;
string addr;
string phone;
};
// Function prototypes
char getMenuResponse(); // Function to allow user to navigate program.
void addContacts(vector<AddressBook>&); // Function to add contacts.
void dispContacts(const vector<AddressBook>&); // Function to display contacts.
void saveFile(const vector<AddressBook>&); // Function to save to file.
void openFile(vector<AddressBook>&); // Function to read from file.
int findTargetFirstName(vector<AddressBook>& list, string target); // Function to find a target index
void findContact(vector<AddressBook>&); // Function to find a contact.
void sortFirstNames(vector<AddressBook>& list);
int main() {
// To operate, to avoid losing previous contacts, Open File first
vector<AddressBook> list;
bool run = true;
cout << "Welcome To My Address Book Program For Assignment 2! " << endl;
do {
switch (getMenuResponse()) {
case 'A': addContacts(list); break;
case 'D': dispContacts(list); break;
case 'M': sortFirstNames(list); break;
case 'O': openFile(list); break;
case 'S': saveFile(list); break;
case 'F': findContact(list); break;
case 'Q': run = false; break;
default : cout << "That is NOT a valid choice" << endl;
}
} while (run);
cout << endl << "Program Terminated" << endl;
// system("PAUSE"); // Commented out so the program exits upon request
return EXIT_SUCCESS;
}
void sortFirstNames(vector<AddressBook>& list) {
string firstname;
int i, j, min;
bool sorted = false;
bool run = true;
if(list[i].firstname > 1) {
for (i = 0; i < (list[i].firstname-1); i++) {
min = i;
for (j = (i+1); j < list[i].firstname; j++) {
if(list[j] < list[min]) min = j;
}
if (i != min) swap(list[i], list[min]);
}
sorted = true;
}
}
int findTargetFirstName(vector<AddressBook>& list, string target) {
// linear search:
for (int i = 0; i < list.size(); i++)
if (list[i].firstname == target) return i; // return ndx of target
return -1; // not in the list
}
void findContact(vector<AddressBook>& list) {
if(list.size() > 0) {
string firstname;
cout << "Enter the first name to find: ";
getline(cin, firstname);
int ndx = findTargetFirstName(list, firstname);
if (ndx >= 0) {
cout << firstname << " is at index " << ndx << "." << endl;
} else cout << firstname << " is not found!" << endl;
} else cout << "That name is not found!" << endl;
}
void addContacts(vector<AddressBook>& list) {
AddressBook tmp; // Declare a temp contact that we will load before putting in the array.
char response;
string str; // Needed for cin.getline; we are going to use a char array.
bool run = true;
do {
system("cls");
cout << "Enter Contact Information" << endl;
cout << "Enter 'quit' at Name to exit." << endl << endl;
cout << "First Name: "; // Entering first name.
getline(cin, str);
tmp.firstname = str;
cout << endl;
if (str == "quit") break;
cout << "Last Name: "; // Entering last name.
getline(cin, str);
tmp.lastname = str;
cout << endl;
cout << "Address: "; // Entering Address
getline(cin, str);
tmp.addr = str;
cout << endl;
cout << "Phone Number: "; // Entering phone number.
getline(cin, str);
tmp.phone = str;
cout << endl;
// See if this record should be added to the array.
cout << "Add Contact to Address Book? (y/n) ";
cin >> response;
cin.ignore(256, ' ');
if (toupper(response) == 'Y') {
list.push_back(tmp);
cout << "Contact added!"<< endl;
}else {
cout << "Contact not added!" << endl;
}
} while (run);
system("cls");
}
void dispContacts(const vector<AddressBook>& list) {
system("cls");
// If there is nothing in the list, statement that there are no contacts.
// Otherwise, continue.
if(list.size() < 1) {
cout << "Nothing to display" << endl;
} else {
cout << "Contacts :" << endl << endl;
cout << fixed << setprecision(2);
cout << "Contact Name Address Phone No." << endl;
cout << "*******************************************" << endl;
cout << left;
for (int i = 0; i < list.size(); i++) {
cout << setw(15) << list[i].firstname << left
<< setw(15) << list[i].lastname << left
<< setw(30) << list[i].addr << left
<< setw(15) << list[i].phone << left << endl;
}
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << right << setw(3) << list.size();
cout << " Contacts"<< endl;
}
system("PAUSE");
system("cls");
}
// Save records to file.
void saveFile(const vector<AddressBook>& list) {
string fileName;
ofstream outfi;
cout<<"Enter file name (please use addressbook.txt for now): ";
// Have set file name to addressbook.txt for now.
// Might change code to allow selection of file to load.
getline(cin,fileName);
outfi.open(fileName.c_str());
// To make sure the file stream is open before doing IO.
if (!outfi.fail()) {
system("cls");
cout << "Saving Address Book to the disc ";
for(int i = 0; i < list.size(); i++) {
outfi << list[i].firstname << ';'
<< list[i].lastname << ';'
<< list[i].addr << ';'
<< list[i].phone;
// Start a new line after all but the last record.
// Simplifies reading the file as EOF is at end of last line.
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 { // Something went wrong with writing to the file.
cout << "ERROR: problem with file" << endl;
system("PAUSE");
system("cls");
}
}
// Open file and load array
void openFile(vector <AddressBook>& list) {
AddressBook tmp;
// If Open File is selected after contacts are added, they will be overwritten.
// Warning user that this will occur.
// If 'N' selected, Open File will not continue, and will return to GetMenuResponse.
if (list.size() > 0) {
char response;
cout << "Are you sure? If you have already added contacts, this will "
<< "overwrite what you have entered with a previously saved "
<< "addressbook.txt." << endl;
cout << "Do you wish to proceed? (Y/N) : ";
cin >> response;
cin.ignore(256, ' ');
if(toupper(response) == 'N') return;
}
ifstream infi("addressbook.txt"); // Have set file name to addressbook.txt for now.
// Might change code to allow selection of file to load.
string str;
// Make sure the file stream is open before doing IO.
if (!infi.fail()) {
system("cls");
cout << "Reading Address Book from the disc ";
list.clear(); // Overwrite any existing records
getline(infi, str, ';');
while(!infi.eof()) {
// Store the first name.
tmp.firstname = str;
// Store the last name.
getline(infi, str, ';');
tmp.lastname = str;
// Store the address.
getline(infi, str, ';');
tmp.addr = str;
// Store the phone number.
getline(infi, str);
tmp.phone = str;
// Put tmp into the vector.
list.push_back(tmp);
getline(infi, str, ';');
}
cout << endl << list.size() << " Contacts read from the disc." << endl;
system("PAUSE");
system("cls");
} else { // Something went wrong with opening the file.
cout << "ERROR: problem with file" << endl;
system("PAUSE");
system("cls");
}
}
// Get's user's input from keyboard.
char getMenuResponse() {
char response;
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 >> response;
cin.ignore(256, ' ');
// Clean-up up to 256 chars including the delimiter specified ( , the endl)
// OR stop when the is encountered after removing it.
return toupper(response);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.