Answer:
See Explaination
Explanation:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
struct Person
{
string name;
string addr;
string phone;
string id;
};
const int MAX_SIZE = 8;
void addData(Person list[], int& size);
void dispData(const Person list[], int size);
void delData (const Person list[], int size);
void saveFile(const Person list[], int size);
void openFile(Person list[], int& size);
char getMenuResponse();
int main(int argc, char *argv[])
{
Person contactList[MAX_SIZE];
int numOfRecs = 0;
bool run = true;
do
{
cout << "Address Book - " << numOfRecs << " Number of Contacts" << endl;
switch ( getMenuResponse() )
{
case 'A': addData(contactList, numOfRecs); break;
case 'D': dispData(contactList, numOfRecs); break;
case 'T': delData(contactList, numOfRecs); break;
case 'O': openFile(contactList, numOfRecs); break;
case 'S': saveFile(contactList, numOfRecs); break;
case 'Q': run = false; break;
default : cout << "That is NOT a valid choice" << endl;
}
} while (run);
cout << endl << "Program Terminated" << endl;
return EXIT_SUCCESS;
}
void addData(Person list[], int& size)
{
Person tmp;
char response;
char str[256];
if (size < MAX_SIZE) {
system("cls");
cout << "Enter Contact Information" << endl << endl;
cout << "Name: ";
cin.getline(str, 256, '\n');
tmp.name = str;
cout << endl;
cout << "Address: ";
cin.getline(str, 256, '\n');
tmp.addr = str;
cout << endl;
cout << "Phone Number: ";
cin.getline(str, 256, '\n');
tmp.phone = str;
cout << endl;
cout << "E-mail id Address: ";
cin.getline(str, 256, '\n');
tmp.id = str;
cout << endl;
cout << "Add Contact to Address Book? (y/n) ";
cin >> response;
if (toupper(response) == 'Y')
list[size++] = tmp;
} else {
cout << "Sorry, Address Book is currently full." << endl;
system("pause");
}
system("cls");
}
void dispData(const Person list[], int size)
{
system("cls");
if(size < 1)
{
cout << "Nothing to display" << endl;
} else {
cout << "Contacts :" << endl << endl;
cout << fixed << setprecision(2);
cout << "Contact Name Address Phone No. ID" << endl;
cout << left;
for (int i = 0; i < size; i++)
{
cout << setw(10) << list[i].name << right
<< setw(10) << list[i].addr<<right
<< setw(10) << list[i].phone<<left
<< setw(15) << list[i].id<<left<<endl;
}
cout << right << setw(3) << size;
cout << " Contacts"<< endl;
}
system("PAUSE");
system("cls");
}
void delData(const Person list[],int size) {
vector<Person> :: iterator vItr = Contact.begin();
while (vItr != Contact.end() )
{
if (vItr->Name == Name)
{
vItr = Contact.erase (vItr);
break;
}
else
vItr++;
}
void saveFile(const Person list[], int size) {
string fileName;
ofstream outfi;
cout<<"Enter file name: ";
getline(cin,fileName);
outfi.open(fileName.c_str());
if (!outfi.fail()) {
system("cls");
cout << "Saving Address Book to the disc ";
for(int i = 0; i < size; i++) {
outfi << list[i].name << ';'
<< list[i].addr<< ';';
if (i < size-1) outfi << endl;
}
cout << endl << size << " Address Book in the disc." << endl;
outfi.close();
system("PAUSE");
system("cls");
}
else {
cout << "ERROR: problem with file" << endl;
system("PAUSE");
system("cls");
}
}
void openFile(Person list[], int& size)
{
ifstream infi("AddressBook.txt");
string str;
stringstream strstrm;
if (!infi.fail()) {
system("cls");
cout << "Reading Address Book from the disc ";
size = 0;
while(!infi.eof() && size < MAX_SIZE)
{
getline(infi, str, ';');
list[size].name = str;
getline(infi, str, ';');
strstrm.str(""); strstrm.clear();
strstrm << str;
strstrm >> list[size].addr;
}
cout << endl << size << " read contacts from the disc." << endl;
system("PAUSE");
system("cls");
}
else {
cout << "ERROR :file not accepted" << endl;
system("PAUSE");
system("cls");
}
}
char getMenuResponse()
{
char response;
cout << endl << "take chioce" << endl
<< "(A)dd contact, (D)isplay contact0, (O)pen File, (S)ave File, (Q)uit" << endl
<< "> ";
cin >> response;
cin.ignore(256, '\n');
return toupper(response);
}