Book: Data Structures Using C++ 2nd Edition chapter 5 programming Exercise 1: (O
ID: 3558197 • Letter: B
Question
Book: Data Structures Using C++ 2nd Edition
chapter 5 programming Exercise 1:
(Online Address Book Revisited) Programming Exercise 9 in Chapter 3 could handle a maximum of only 500 entries. Using linked lists, redo the program to handle as many entries as required. Add the following operations to your program:
a. Add or delete a new entry to the address book.
b. When the program terminates, write the data in the address book to a disk.
Files from Chapter 3 Exercise 9
***Main.cpp***
#include <iostream>
#include <fstream>
#include <string>
#include "addressBookType.h"
using namespace std;
void loadAddressBook(addressBookType& adBook);
void saveData(addressBookType& adBook);
void showMenu();
int main()
{
addressBookType addressBook;
string str;
string str1;
string str2;
int choice;
int loc;
int month;
loadAddressBook(addressBook);
showMenu();
cin >> choice;
while (choice != 9)
{
switch (choice)
{
case 1:
cout << "Enter the last name of the person: ";
cin >> str;
cout << endl;
loc = addressBook.search(str);
if (loc != -1)
cout << str << " is in the address book" << endl;
else
cout << str << " is not in the address book" << endl;
break;
case 2:
cout << "Enter the last name of the person: ";
cin >> str;
cout << endl;
loc = addressBook.search(str);
if (loc != -1)
addressBook.printAt(loc);
else
cout << str << " is not in the address book" << endl;
break;
case 3:
cout << "Enter the month number: ";
cin >> month;
cout << endl;
addressBook.printNameInTheMonth(month);
break;
case 4:
cout << "Enter starting last name: ";
cin >> str1;
cout << endl;
cout << "Enter ending last name: ";
cin >> str2;
cout << endl;
addressBook.printNamesBetweenLastNames(str1, str2);
break;
case 5:
cout << "Enter person type Family, Friend, Business: ";
cin >> str;
cout << endl;
addressBook.printNamesWithStatus(str);
break;
case 6:
addressBook.print();
break;
case 7:
saveData(addressBook);
break;
default:
cout << "Invalid choice" << endl;
}
showMenu();
cin >> choice;
}
char response;
cout << "Save data Yes (Y/y) No(N/n)?: ";
cin >> response;
cout << endl;
if (response == 'y' || response == 'Y')
saveData(addressBook);
return 0;
}
void loadAddressBook(addressBookType& adBook)
{
ifstream infile;
char filename[50];
string first;
string last;
int month;
int day;
int year;
string street;
string city;
string state;
string zip;
string phone;
string pStatus;
extPersonType temp;
cout << "Enter file name: ";
cin >> filename;
cout << endl;
infile.open(filename);
if (!infile)
{
cout << "Input file does not exists. "
<< "Program terminates!!!" << endl;
return;
}
int i = 0;
infile >> first >> last >> month >> day >> year;
infile.ignore(100, ' ');
getline(infile, street);
getline(infile, city);
getline(infile, state);
infile >> zip >> phone >> pStatus;
while (infile)
{
temp.setInfo(first, last, month, day, year, street, city, state,
zip, phone, pStatus);
adBook.insertAt(i, temp);
i++;
infile >> first >> last >> month >> day >> year;
infile.ignore(100, ' ');
getline(infile, street);
getline(infile, city);
getline(infile, state);
infile >> zip >> phone >> pStatus;
}
}
void saveData(addressBookType& adBook)
{
ofstream outfile;
char filename[50];
cout << "Enter file name: ";
cin >> filename;
cout << endl;
outfile.open(filename);
if (!outfile)
{
cout << "Input file does not exists. "
<< "Program terminates!!!" << endl;
return;
}
adBook.saveData(outfile);
}
void showMenu()
{
cout << "Welcome to the address book program.";
cout << "Choose among the following options:" << endl;
cout << "1: To see if a person is in the address book" << endl;
cout << "2: Print the information of a person" << endl;
cout << "3: Print the names of person having birthday in a particular month" << endl;
cout << "4: Print the names of persons between two last names" << endl;
cout << "5: Print the names of persons having a particular status" << endl;
cout << "6: Print the address book" << endl;
cout << "7: Save data" << endl;
cout << "9: Terminate the program" << endl;
}
***addressBookType.h***
//addressBookType.h
#ifndef H_addressBookType
#define H_addressBookType
#include <string>
#include <fstream>
#include "extPersonType.h"
#include "arrayListType.h"
using namespace std;
class addressBookType: public arrayListType<extPersonType>
{
public:
void print() const;
void printNameInTheMonth(int month);
void printInfoOf(string lName);
void printNamesWithStatus(string status);
void printAt(int i);
void printNamesBetweenLastNames(string last1, string last2);
int search(string lName);
void saveData(ofstream&);
addressBookType();
};
#endif
***addressType.h***
#ifndef H_addressType
#define H_addressType
#include <string>
using namespace std;
class addressType
{
public:
void print() const;
void setAddress(string sAddress, string c, string s, string z);
void getAddress(string& sAddress, string& c,
string& s, string& z);
addressType(string sAddress = "", string c = "",
string s = "", string z = "");
private:
string streetAddress;
string city;
string state;
string zip;
};
#endif
***arrayListType.h***
#ifndef H_arrayListType
#define H_arrayListType
#include <iostream>
#include <cassert>
using namespace std;
template <class elemType>
class arrayListType
{
public:
const arrayListType<elemType>& operator=
(const arrayListType<elemType>&);
//Overloads the assignment operator
bool isEmpty() const;
//Function to determine whether the list is empty
//Postcondition: Returns true if the list is empty;
// otherwise, returns false.
bool isFull() const;
//Function to determine whether the list is full.
//Postcondition: Returns true if the list is full;
// otherwise, returns false.
int listSize() const;
//Function to determine the number of elements in the list
//Postcondition: Returns the value of length.
int maxListSize() const;
//Function to determine the size of the list.
//Postcondition: Returns the value of maxSize.
void print() const;
//Function to output the elements of the list
//Postcondition: Elements of the list are output on the
// standard output device.
bool isItemAtEqual(int location, const elemType& item) const;
//Function to determine whether the item is the same
//as the item in the list at the position specified by
//Postcondition: Returns true if the list[location]
// is the same as the item; otherwise,
// returns false.
void insertAt(int location, const elemType& insertItem);
//Function to insert an item in the list at the
//position specified by location. The item to be inserted
//is passed as a parameter to the function.
//Postcondition: Starting at location, the elements of the
// list are shifted down, list[location] = insertItem;,
// and length++;. If the list is full or location is
// out of range, an appropriate message is displayed.
void insertEnd(const elemType& insertItem);
//Function to insert an item at the end of the list.
//The parameter insertItem specifies the item to be inserted.
//Postcondition: list[length] = insertItem; and length++;
// If the list is full, an appropriate message is
// displayed.
void removeAt(int location);
//Function to remove the item from the list at the
//position specified by location
//Postcondition: The list element at list[location] is removed
// and length is decremented by 1. If location is out of
// range,an appropriate message is displayed.
void retrieveAt(int location, elemType& retItem) const;
//Function to retrieve the element from the list at the
//position specified by location.
//Postcondition: retItem = list[location]
// If location is out of range, an appropriate message is
// displayed.
void replaceAt(int location, const elemType& repItem);
//Function to replace the elements in the list at the
//position specified by location. The item to be replaced
//is specified by the parameter repItem.
//Postcondition: list[location] = repItem
// If location is out of range, an appropriate message is
// displayed.
void clearList();
//Function to remove all the elements from the list.
//After this operation, the size of the list is zero.
//Postcondition: length = 0;
int seqSearch(const elemType& item) const;
//Function to search the list for a given item.
//Postcondition: If the item is found, returns the location
// in the array where the item is found; otherwise,
// returns -1.
void insert(const elemType& insertItem);
//Function to insert the item specified by the parameter
//insertItem at the end of the list. However, first the
//list is searched to see whether the item to be inserted
//is already in the list.
//Postcondition: list[length] = insertItem and length++
// If the item is already in the list or the list
// is full, an appropriate message is displayed.
void remove(const elemType& removeItem);
//Function to remove an item from the list. The parameter
//removeItem specifies the item to be removed.
//Postcondition: If removeItem is found in the list,
// it is removed from the list and length is
// decremented by one.
arrayListType(int size = 100);
//constructor
//Creates an array of the size specified by the
//parameter size. The default array size is 100.
//Postcondition: The list points to the array, length = 0,
// and maxSize = size
arrayListType(const arrayListType<elemType>& otherList);
//copy constructor
~arrayListType();
//destructor
//Deallocates the memory occupied by the array.
protected:
elemType *list; //array to hold the list elements
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list
};
template <class elemType>
bool arrayListType<elemType>::isEmpty() const
{
return (length == 0);
}
template <class elemType>
bool arrayListType<elemType>::isFull() const
{
return (length == maxSize);
}
template <class elemType>
int arrayListType<elemType>::listSize() const
{
return length;
}
template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
return maxSize;
}
template <class elemType>
void arrayListType<elemType>::print() const
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}
template <class elemType>
bool arrayListType<elemType>::isItemAtEqual
(int location, const elemType& item) const
{
return (list[location] == item);
}
template <class elemType>
void arrayListType<elemType>::insertAt
(int location, const elemType& insertItem)
{
if (location < 0 || location >= maxSize)
cerr << "The position of the item to be inserted "
<< "is out of range" << endl;
else
if (length >= maxSize) //list is full
cerr << "Cannot insert in a full list" << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem; //insert the item at the
//specified position
length++; //increment the length
}
} //end insertAt
template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType& insertItem)
{
if (length >= maxSize) //the list is full
cerr << "Cannot insert in a full list" << endl;
else
{
list[length] = insertItem; //insert the item at the end
length++; //increment the length
}
} //end insertEnd
template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
if (location < 0 || location >= length)
cerr << "The location of the item to be removed "
<< "is out of range" << endl;
else
{
for (int i = location; i < length - 1; i++)
list[i] = list[i+1];
length--;
}
} //end removeAt
template <class elemType>
void arrayListType<elemType>::retrieveAt
(int location, elemType& retItem) const
{
if (location < 0 || location >= length)
cerr << "The location of the item to be retrieved is "
<< "out of range." << endl;
else
retItem = list[location];
} //end retrieveAt
?
template <class elemType>
void arrayListType<elemType>::replaceAt
(int location, const elemType& repItem)
{
if (location < 0 || location >= length)
cerr << "The location of the item to be replaced is "
<< "out of range." << endl;
else
list[location] = repItem;
} //end replaceAt
template <class elemType>
void arrayListType<elemType>::clearList()
{
length = 0;
} //end clearList
template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item) const
{
int loc;
bool found = false;
for (loc = 0; loc < length; loc++)
if (list[loc] == item)
{
found = true;
break;
}
if (found)
return loc;
else
return -1;
} //end seqSearch
template <class elemType>
void arrayListType<elemType>::insert(const elemType& insertItem)
{
int loc;
if (length == 0) //list is empty
list[length++] = insertItem; //insert the item and
//increment the length
else if (length == maxSize)
cerr << "Cannot insert in a full list." << endl;
else
{
loc = seqSearch(insertItem);
if (loc == -1) //the item to be inserted
//does not exist in the list
list[length++] = insertItem;
else
cerr << "the item to be inserted is already in "
<< "the list. No duplicates are allowed." << endl;
}
} //end insert
template<class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem)
{
int loc;
if (length == 0)
cerr << "Cannot delete from an empty list." << endl;
else
{
loc = seqSearch(removeItem);
if (loc != -1)
removeAt(loc);
else
cout << "The item to be deleted is not in the list."
<< endl;
}
} //end remove
template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
if (size < 0)
{
cerr << "The array size must be positive. Creating "
<< "an array of size 100. " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new elemType[maxSize];
assert(list != NULL);
}
template <class elemType>
arrayListType<elemType>::~arrayListType()
{
delete [] list;
}
?
template <class elemType>
arrayListType<elemType>::arrayListType
(const arrayListType<elemType>& otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize]; //create the array
assert(list != NULL); //terminate if unable to allocate
//memory space
for (int j = 0; j < length; j++) //copy otherList
list [j] = otherList.list[j];
} //end copy constructor
template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator=
(const arrayListType<elemType>& otherList)
{
if (this != &otherList) //avoid self-assignment
{
delete [] list;
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize]; //create the array
assert(list != NULL); //if unable to allocate memory
//space, terminate the program
for (int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
return *this;
}
?
#endif
***date.h***
#ifndef date_H
#define date_H
class dateType
{
public:
void setDate(int month, int day, int year);
//Function to set the date.
//Data members dMonth, dDay, and dYear are set
//according to the parameters
//Post: dMonth = month; dDay = day;
// dYear = year;
void getDate(int& month, int& day, int& year);
//Function to return the date
//Post: month = dMonth; day = dDay;
// year = dYear;
void printDate() const;
//Function to output the date in the form mm-dd-yyyy.
dateType(int month = 1, int day = 1, int year = 1900);
//Constructor to set the date
//Data members dMonth, dDay, and dYear are set
//according to the parameters. If no values are
//specified when an object is declares, the default
//values are used.
//Post: dMonth = month; dDay = day;
// dYear = year;
private:
bool isLeapYear(int y);
//Function to check if a year is a leap year
int dMonth; //variable to store month
int dDay; //variable to store day
int dYear; //variable to store year
};
#endif
***extPersonType.h***
//extPersonType.
#ifndef H_extPersonType
#define H_extPersonType
#include <string>
#include "addressType.h"
#include "date.h"
#include "personType.h"
class extPersonType: public personType
{
public:
void printAddress() const;
void printInfo() const;
void setInfo(string fName, string lName,
int month, int day, int year,
string street, string c, string s,
string z, string phone, string pStatus);
void setInfo(string fName, string lName,
dateType d, addressType ad,
string phone, string pStatus);
bool isLastName(string lName);
void getLastName(string& lName);
void getAddress(string& sAddress, string& c,
string& s, string& z);
void getStatus(string& status);
void getPhoneNumber(string& phone);
bool isStatus(string status);
bool isDOB(int month, int day, int year);
void getDOB(int& month, int& day, int& year);
bool isMonth(int month);
extPersonType(string fName = "", string lName = "",
int month = 1, int day = 1, int year = 1900,
string street = "", string c = "", string s = "",
string z = "", string phone = "", string pStatus = "");
private:
addressType address;
dateType dob;
string phoneNumber;
string personStatus;
};
#endif
***personType.h***
//personType.h
#ifndef H_personType
#define H_personType
#include <string>
using namespace std;
class personType
{
public:
void print() const;
//Function to output the first name and last name
//in the form firstName lastName
void setName(string first, string last);
//Function to set firstName and lastName according to
//the parameters
//Post: firstName = first; lastName = last;
void getName(string& first, string& last);
//Function to return firstName and lastName via the parameters
//Post: first = firstName; last = lastName;
personType(string first, string last);
//Constructor with parameters
//Set firstName and lastName according to the parameters
//Post: firstName = first; lastName = last;
personType();
//Default constructor;
//Intialize firstName and lastName to empty string
//Post: firstName = ""; lastName = "";
private:
string firstName; //store the first name
string lastName; //store the last name
};
#endif
***addressBookTypeImp.cpp***
//addressBookTypeImp.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "addressBookType.h"
using namespace std;
void addressBookType::print() const
{
for (int i = 0; i < length; i++)
list[i].printInfo();
}
void addressBookType::printNameInTheMonth(int month)
{
for (int i = 0; i < length; i++)
{
if (list[i].isMonth(month))
{
list[i].print();
cout << endl;
}
}
}
void addressBookType::printInfoOf(string lName)
{
int i = search(lName);
if (i != -1)
list[i].printInfo();
else
cout << lName << " is not in address book." << endl;
}
void addressBookType::printNamesWithStatus(string status)
{
for (int i = 0; i < length; i++)
if (list[i].isStatus(status))
{
list[i].print();
cout << endl;
}
}
void addressBookType::printAt(int i)
{
if (i < length)
list[i].printInfo();
else
cout << "No such person" << endl;
}
void addressBookType::printNamesBetweenLastNames(string last1, string last2)
{
string lName;
for (int i = 0; i < length; i++)
{
list[i].getLastName(lName);
if (last1 <= lName && lName <= last2)
{
list[i].print();
cout << endl;
}
}
}
int addressBookType::search(string lName)
{
bool found = false;
int i = 0;
for (i = 0; i < length; i++)
if (list[i].isLastName(lName))
{
found = true;
break;
}
if (found)
return i;
else
return -1;
}
?
addressBookType::addressBookType()
{
length = 0;
}
void addressBookType::saveData(ofstream& outFile)
{
string first;
string last;
int month;
int day;
int year;
string street;
string city;
string state;
string zip;
string phone;
string pStatus;
?
for (int i = 0; i < length; i++)
{
list[i].getName(first,last);
list[i].getDOB(month, day, year);
list[i].getAddress(street,city,state,zip);
list[i].getPhoneNumber(phone);
list[i].getStatus(pStatus);
outFile << first << " " << last << endl;
outFile << month << " " << day << " " << year << endl;
outFile << street << endl << city << endl << state << endl << zip << endl;
outFile << phone << endl << pStatus << endl;
}
}
***addressTypeImp.cpp***
#include <iostream>
#include <string>
#include "addressType.h"
using namespace std;
void addressType::print() const
{
cout << streetAddress << endl;
cout << city << ", " << state << " - " << zip << endl;
}
void addressType::setAddress(string sAddress, string c, string s, string z)
{
streetAddress = sAddress;
city = c;
state = s;
zip = z;
}
void addressType::getAddress(string& sAddress, string& c,
string& s, string& z)
{
sAddress = streetAddress ;
c = city;
s = state;
z = zip;
}
addressType::addressType(string sAddress, string c, string s, string z)
{
streetAddress = sAddress;
city = c;
state = s;
zip = z;
}
***dateImp.cpp***
//Implementation file date
?
#include <iostream>
#include "date.h"
using namespace std;
void dateType::setDate(int month, int day, int year)
{
if (year >= 1)
dYear = year;
else
dYear = 1900;
if (1 <= month && month <= 12)
dMonth = month;
else
dMonth = 1;
switch (dMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (1 <= day && day <= 31)
dDay = day;
else
dDay = 1;
break;
case 4:
case 6:
case 9:
case 11:
if (1 <= day && day <= 30)
dDay = day;
else
dDay = 1;
break;
case 2:
if (isLeapYear(year))
{
if (1 <= day && day <= 29)
dDay = day;
else
dDay = 1;
}
else
{
if (1 <= day && day <= 28)
dDay = day;
else
dDay = 1;
}
}
}
void dateType::getDate(int& month, int& day, int& year)
{
month = dMonth;
day = dDay;
year = dYear;
}
void dateType::printDate() const
{
cout << dMonth << "-" << dDay << "-" << dYear;
}
dateType::dateType(int month, int day, int year) //constructor with parameter
{
setDate(month, day, year);
}
bool dateType::isLeapYear(int y)
{
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
return true;
else
return false;
}
***extPersonTypeImp.cpp***
//extPersonTypeImp.cpp
#include <iostream>
#include <string>
#include "extPersonType.h"
void extPersonType::printAddress() const
{
personType::print();
cout << endl;
address.print();
}
void extPersonType::printInfo() const
{
personType::print();
cout << endl;
cout << "Date of Birth: ";
dob.printDate();
cout << endl;
cout << "Phone Number: " << phoneNumber << endl;
cout << "Person Type: " << personStatus << endl;
address.print();
cout << endl;
}
void extPersonType::setInfo(string fName, string lName,
int month, int day, int year,
string street, string c, string s,
string z, string phone, string pStatus)
{
personType::setName(fName,lName);
dob.setDate(month, day, year);
address.setAddress(street,c,s,z);
phoneNumber = phone;
personStatus = pStatus;
}
void extPersonType::setInfo(string fName, string lName,
dateType d, addressType ad,
string phone, string pStatus)
{
personType::setName(fName,lName);
dob = d;
address = ad;
phoneNumber = phone;
personStatus = pStatus;
}
bool extPersonType::isLastName(string lName)
{
string first;
string last;
personType::getName(first,last);
return (last == lName);
}
void extPersonType::getLastName(string& lName)
{
string first;
string last;
personType::getName(first,last);
lName = last;
}
void extPersonType::getAddress(string& sAddress, string& c,
string& s, string& z)
{
address.getAddress(sAddress,c,s,z);
}
void extPersonType::getPhoneNumber(string& phone)
{
phone = phoneNumber;
}
void extPersonType::getStatus(string& status)
{
status = personStatus;
}
bool extPersonType::isStatus(string status)
{
return (status == personStatus);
}
bool extPersonType::isDOB(int month, int day, int year)
{
int m;
int d;
int y;
dob.getDate(m, d, y);
return (m == month && d == day && y == year);
}
void extPersonType::getDOB(int& month, int& day, int& year)
{
dob.getDate(month, day, year);
}
bool extPersonType::isMonth(int month)
{
int m;
int d;
int y;
dob.getDate(m, d, y);
return (m == month);
}
extPersonType::extPersonType(string fName, string lName,
int month, int day, int year,
string street, string c, string s,
string z, string phone, string pStatus)
{
personType::setName(fName,lName);
dob.setDate(month, day, year);
address.setAddress(street, c, s, z);
phoneNumber = phone;
personStatus = pStatus;
}
***personTypeImp.cpp***
//personTypeImp.cpp
#include <iostream>
#include <string>
#include "personType.h"
using namespace std;
void personType::print() const
{
cout << firstName << " " << lastName;
}
void personType::setName(string first, string last)
{
firstName = first;
lastName = last;
}
void personType::getName(string& first, string& last)
{
first = firstName;
last = lastName;
}
//constructor with parameters
personType::personType(string first, string last)
{
firstName = first;
lastName = last;
}
personType::personType() //default constructor
{
firstName = "";
lastName = "";
}
Explanation / Answer
[code]
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
struct Person
{
string name; // The name of record management system
string addr; // The number of contacts
};
// functin prototypes
void addData(AddressBook list[], int& size);
void dispData(const AddressBook list[], int size);
void saveFile(const AddressBook list[], int size);
void openFile(AddressBook list[], int& size);
char getMenuResponse();
int main(int argc, char *argv[])
{
AddressBook contactList[MAX_SIZE];
int numOfContacts = 0;
bool run = true;
do
{
cout << "Address Book - " << numOfRecs << " Number of Contacts" << endl;
switch ( getMenuResponse() )
{
case 'A': addData(contactList, numOfContacts); break;
case 'D': dispData(contactList, numOfContacts); break;
case 'O': openFile(contactList, numOfContacts); break;
case 'S': saveFile(contactList, numOfContacts); 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 addData(AddressBook list[], int& size)
{
AddressBook tmp; // declare a temp contact that we will load before putting in the array
char response;
char str[256]; // needed for cin.getline; we are going to use a char array
if (size < MAX_SIZE) {
system("cls");
cout << "Enter Contact Information" << endl << endl;
cout << "Name: ";
// Get up to 256 characters from the keyboard including white space.
// Stop reading if encounter the first. If there's any chance of
// more than 256 characters you will have to clean up cin with
// cin.ignore before the next input.
cin.getline(str, 256, ' '); // for char arrays; different from the other getline
tmp.name = str;
cout << "Address: ";
cin.getline(str, 256, ' ');
tmp.addr = str;
cout << endl;
cout << "Phone Number: ";
cin.getline(str, 256, ' ');
tmp.addr = str;
cout << endl;
cout << "E-mail Address: ";
cin.getline(str, 256, ' ');
tmp.addr = str;
cout << endl;
// see if this record should be added to the array
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 Address Book 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. Email" << endl;
cout << "*******************************************" << endl;
cout << left;
for (int i = 0; i < size; i++) {
cout << setw(21) << list[i].name << right
<< setw(10) << list[i].addr<<left<<endl;
}
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << right << setw(3) << size;
cout << " Contacts"<< endl;
}
system("PAUSE");
system("cls");
}
// Save records to disc
void saveFile(const AddressBook list[], int size) {
string fileName;
ofstream outfi;
cout<<"Enter file name: ";
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 < size; i++) {
outfi << list[i].name << ';'
<< list[i].addr<< ';';
// Start a new line after all but the last record
// Simplifies reading the file as EOF is at end of last line
if (i < size-1) outfi << endl;
}
cout << endl << 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");
}
}
// Open file and load array
void openFile(AddressBook list[], int& size)
{
ifstream infi("AddressBook.txt");
string str;
stringstream strstrm;
// make sure the file stream is open before doing IO
if (!infi.fail()) {
system("cls");
cout << "Reading Address Book from the disc ";
size = 0; // overwrite any existing records
while(!infi.eof() && size < MAX_SIZE) {
// get and store the name
getline(infi, str, ';');
list[size].name = str;
// get, convert and store the quantity
getline(infi, str, ';');
strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
strstrm << str;
strstrm >> list[size].addr;
}
cout << endl << 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");
}
}
char getMenuResponse()
// Task: Put the menu on screen and get a response
// Accepts: Nothing
// Returns: The users response
// Modifies: Nothing
// NOTE: Characters are far more intuitive at the command
// line than numbers; avoid using numbers.
{
char response;
cout << endl << "Make your selection" << endl
<< "(A)dd contact, (D)isplay contact0, (O)pen File, (S)ave File, (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);
}