Data Structures Using C++ 2nd Edition Chapter 3 Programming Exercise 9 Using cla
ID: 3713015 • Letter: D
Question
Data Structures Using C++ 2nd Edition
Chapter 3 Programming Exercise 9
Using classes, 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 a maximum of 500 entries.
a. 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 data members.
b. Define a class extPersonType using the class personType (as defined in Example 1-12, Chapter 1), the class dateType (as designed in Programming Exercise 2 of Chapter 2), and the class addressType. Add a data member to this class to classify the person as a family member, friend, or business associate. Also, add a data member to store the phone number. Add (or override) the functions to print and store the appropriate information. Use constructors to automatically initialize the data members.
c. Derive the class addressBookType from the class arrayListType, as defined in this chapter, so that an object of type addressBookType can store objects of type extPersonType. An object of type addressBookType should be able to process a maximum of 500 entries. Add necessary operations to the class addressBookType so that the program should perform the following operations:
i. Load the data into the address book from a disk.
ii. Search for a person by last name.
iii. Print the address, phone number, and date of birth (if it exists) of a given person.
iv. Print the names of the people whose birthdays are in a given month or between two given dates
v. Print the names of all the people having the same status, such as family, friend, or business.
vi. Print the names of all the people between two last names.
Here it what I have so far. I am getting an error in addressBookType.cpp: Line 64: binary '!=': no operator found which takes a left-hand operand of type 'std::basic_istream>' (or there is no acceptable conversion)
addressBookType.cpp
#include
#include "extPersonType.h"
#include "extPersonType.cpp"
#include "addressBookType.h"
void addressBookType::swap(int m, int n)
{
string str1, str2, str3, str4;
persons[m].getPerson(str1, str2);
persons[n].getPerson(str3, str4);
persons[m].setPerson(str3, str4);
persons[n].setPerson(str1, str2);
dateType bd1, bd2;
persons[m].getBirthDate(bd1);
persons[n].getBirthDate(bd2);
persons[m].setBirthDate(bd2);
persons[n].setBirthDate(bd1);
addressType add1, add2;
persons[m].getAddress(add1);
persons[n].getAddress(add2);
persons[m].setAddress(add2);
persons[n].setAddress(add1);
str1 = persons[m].getAssociation();
str2 = persons[n].getAssociation();
persons[m].setAssociation(str2);
persons[n].setAssociation(str1);
str1 = persons[m].getPhone();
str2 = persons[n].getPhone();
persons[m].setPhone(str2);
persons[n].setPhone(str1);
}
addressBookType::addressBookType()
{
ifstream inFile;
string fileName = " ";
cout << " Enter input file name:";
cin >> fileName;
inFile.open(fileName.c_str());
if (!inFile.is_open())
{
cout << " Error reading in input file";
system("pause");
exit(0);
}
string strs[7];
int nums[4];
ind = 0;
while ((inFile >> strs[0] >> strs[1] >> nums[0] >> nums[1] >> nums[2] >> strs[2] >> strs[3] >> strs[4] >> nums[3] >> strs[5] >> strs[6]) != NULL) // the != in this while statement is throwing the error
{
persons[ind].setExtPerson(strs[0], strs[1], nums[0], nums[1], nums[2], strs[2], strs[3], strs[4], nums[3], strs[5], strs[6]);
ind++;
}
}
void addressBookType::sortByLastName()
{
int i, j, smallestInd;
string str1, str2, temp;
for (i = 0; i<(ind - 1); i++)
{
smallestInd = i;
for (j = i + 1; j
{
persons[j].getPerson(temp, str1);
persons[smallestInd].getPerson(temp, str2);
if (str1.compare(str2)<0)
smallestInd = j;
}
if (smallestInd != i)
swap(i, smallestInd);
}
}
void addressBookType::printAddressBook() const
{
for (int i = 0; i
{
cout << " ";
persons[i].print();
}
}
int addressBookType::searchByName() const
{
string keyName, name, temp;
int count = 0;
cout << " Enter the last name to search:";
cin >> keyName;
for (int i = 0; i
{
persons[i].getPerson(temp, name);
if (keyName.compare(name) == 0)
return i;
}
return -1;
}
void addressBookType::printAPerson() const
{
int result = searchByName();
if (result == -1)
cout << " No person found as specified by you.";
else
{
cout << " ";
persons[result].print();
}
}
void addressBookType::printByBirthday() const
{
dateType bd;
int month, count = 0;
cout << " Search address book for a given month of birthdays." << " Please remember name is case sensitive.";
cout << " Enter the month number (1...12):";
cin >> month;
for (int i = 0; i
{
persons[i].getBirthDate(bd);
if (bd.getMonth() == month)
{
cout << " ";
persons[i].print();
count++;
}
}
if (count == 0)
cout << " No person found as specified by you.";
}
void addressBookType::printNamesBetween() const
{
string keyName1, keyName2, name, temp;
int count = 0;
cout << " Search address book between two given names." << " Please remember name is case sensitive.";
cout << " Enter the last name1:";
cin >> keyName1;
cout << " Enter the last name2:";
cin >> keyName1;
if (keyName1.compare(keyName2)>0)
keyName1.swap(keyName2);
for (int i = 0; i
{
persons[i].getPerson(temp, name);
if ((keyName1.compare(name) <= 0) && (keyName2.compare(name) >= 0))
{
cout << " ";
persons[i].print();
count++;
}
}
if (count == 0)
cout << " No person found as specified by you.";
}
void addressBookType::printAssociation() const
{
string asso;
string types[3] = { "Family member", "Family friend", "Business Associate" };
int choice, count = 0;
cout << " Search address book by association." << " Please remember name is case sensitive.";
cout << " Associatin Types";
cout << " Family member : 1";
cout << " Family friend : 2";
cout << " Business Associate : 3";
cout << " Enter the association type: ";
cin >> choice;
for (int i = 0; i
{
asso = persons[i].getAssociation();
if (asso.compare(types[choice - 1]) == 0)
{
cout << " ";
persons[i].print();
count++;
}
}
if (count == 0)
cout << " No person found as specified by you.";
}
addressBookType.h
#ifndef addressBookType_H
#define addressBookType_H
#include
#include
#include
#include
#include "extPersonType.h"
using namespace std;
const int MAX_ENTRIES = 500;
class addressBookType
{
public:
addressBookType();
void sortByLastName();
int searchByName() const;
void printAPerson() const;
void printAddressBook() const;
void printByBirthday() const;
void printNamesBetween() const;
void printAssociation() const;
private:
extPersonType persons[MAX_ENTRIES];
int ind;
void swap(int, int);
};
#endif
addressType.cpp
#include "addressType.h"
#include
void addressType::setStreet(string myStreet)
{
street = myStreet;
}
void addressType::setCity(string myCity)
{
city = myCity;
}
void addressType::setState(string myState)
{
state = myState;
}
void addressType::setZIP(int myZIP)
{
ZIP = myZIP;
}
string addressType::getStreet() const
{
return street;
}
string addressType::getCity() const
{
return city;
}
string addressType::getState() const
{
return state;
}
int addressType::getZIP() const // corrected
{
return ZIP;
}
void addressType::printAddress() const
{
cout << " " << getStreet() << ", " << getCity() << " " << getState() << "-" << getZIP();
}
addressType::addressType(string myStreet, string myCity, string myState, int myZIP)
{
setStreet(myStreet);
setCity(myCity);
setState(myState);
setZIP(myZIP); //corrected
}
addressType.h
#ifndef H_addressType
#define H_addressType
#include
#include
using namespace std;
class addressType
{
public:
void setStreet(string myStreet);
void setCity(string myCity);
void setState(string myState);
void setZIP(int myZIP);
string getStreet() const;
string getCity() const;
string getState() const;
int getZIP() const;
void printAddress() const;
addressType(string = " ", string = " ", string = " ", int = 0);
private:
string street;
string city;
string state;
int ZIP;
};
#endif
dateType.cpp
#include
#include "dateType.h"
void dateType::setDate(int month, int day, int year)
{
char dateStr[9];
_strdate_s(dateStr);
int thisYear = 2000 + (dateStr[6] - 48) * 10 + dateStr[7] - 48;
if ((year >= 1900) && (year <= thisYear))
dYear = year;
else
dYear = 1900;
if (month >= 1 && month <= 12)
dMonth = month;
else
dMonth = 1;
int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (dYear % 400 == 0 || (dYear % 100 != 0 && dYear % 4 == 0))
days[1]++;
if (day >= 1 && day <= days[dMonth - 1])
dDay = day;
else
dDay = 1;
}
void dateType::getDate(int& month, int& day, int& year) //corrected
{
dMonth = month;
dDay = day;
dYear = year;
}
int dateType::getDay() const
{
return dDay;
}
int dateType::getMonth() const
{
return dMonth;
}
int dateType::getYear() const
{
return dYear;
}
void dateType::printDate() const
{
cout << " " << dMonth << "-" << dDay << "-" << dYear;
}
dateType::dateType(int month, int day, int year)
{
setDate(month, day, year);
}
void dateType::isLeapYear()
{
if (dYear % 400 == 0 || (dYear % 100 != 0 && dYear % 4 == 0))
cout << " Year" << dYear << "is a leap year.";
else
cout << " Year" << dYear << "is not a leap year.";
}
dateType.h
#ifndef dateType_H
#define dateType_H
#include
#include
using namespace std;
class dateType
{
public:
void setDate(int month, int day, int year);
void getDate(int& month, int& day, int& year);
int getDay() const;
int getMonth() const;
int getYear() const;
void isLeapYear();
void printDate() const;
dateType(int nonth = 1, int day = 1, int year = 1900);
private:
int dMonth;
int dDay;
int dYear;
};
#endif
extPersonType.cpp
#include "extPersonType.h"
#include "personType.h"
#include "dateType.h"
#include "dateType.cpp"
#include "personType.cpp"
#include "addressType.h"
#include "addressType.cpp"
void extPersonType::setBirthDate(const dateType myDate)
{
birthDate.setDate(myDate.getMonth(), myDate.getDay(), myDate.getYear()); //corrected bithDate misspelld
}
void extPersonType::getBirthDate(dateType& myDate) const
{
myDate.setDate(birthDate.getMonth(), birthDate.getDay(), birthDate.getYear());
}
void extPersonType::setAddress(const addressType address) //corrected
{
myAddress.setStreet(address.getStreet());
myAddress.setCity(address.getCity());
myAddress.setState(address.getState());
myAddress.setZIP(address.getZIP());
}
void extPersonType::getAddress(const addressType &address) //corrected
{
myAddress.setStreet(address.getStreet());
myAddress.setCity(address.getCity());
myAddress.setState(address.getState());
myAddress.setZIP(address.getZIP());
}
void extPersonType::print() const
{
birthDate.printDate();
person.print();
myAddress.printAddress();
cout << " " << association << ", " << phone;
}
void extPersonType::setPerson(const string first, const string last)
{
person.setFirstName(first);
person.setLastName(last);
}
void extPersonType::getPerson(string& first, string& last) const
{
first = person.getFirstName();
last = person.getLastName();
}
void extPersonType::setAssociation(const string myAssociation)
{
association = myAssociation;
}
void extPersonType::setPhone(const string myPhone)
{
phone = myPhone;
}
string extPersonType::getAssociation() const
{
return association;
}
string extPersonType::getPhone() const
{
return phone;
}
void extPersonType::setExtPerson(string first, string last, int month, int day, int year, string myStreet, string myCity, string myState, int myZIP, string asso, string ph)
{
person.setFirstName(first);
person.setLastName(last);
birthDate.setDate(month, day, year);
myAddress.setStreet(myStreet);
myAddress.setCity(myCity);
myAddress.setState(myState);
myAddress.setZIP(myZIP);
setAssociation(asso);
setPhone(ph);
}
extPersonType::extPersonType(string first, string last, int month, int day, int year, string myStreet, string myCity, string myState, int myZIP, string asso, string ph) :person(first, last), birthDate(month, day, year), myAddress(myStreet, myCity, myState, myZIP)
{
setAssociation(asso);
setPhone(ph);
}
extPersonType.h
#ifndef extPersonType_H
#define extPersonType_H
#include
#include
#include "addressType.h"
#include "personType.h"
#include "dateType.h"
using namespace std;
class extPersonType
{
public:
void print() const;
void setBirthDate(const dateType);
void getBirthDate(dateType&) const;
void setAddress(const addressType address);
void getAddress(const addressType& address);
void setPerson(const string, const string);
void getPerson(string&, string&) const;
void setAssociation(const string);
string getAssociation() const;
void setPhone(const string);
string getPhone() const;
void setExtPerson(string, string, int, int, int, string, string, string, int, string, string);
extPersonType(string = " ", string = " ", int = 1, int = 1, int = 1900, string = " ", string = " ", string = " ", int = 0, string = " ", string = " ");
private:
personType person;
dateType birthDate;
addressType myAddress;
string association;
string phone;
};
#endif
personType.cpp
#include
#include "personType.h"
void personType::setFirstName(string myFirstName)
{
firstName = myFirstName;
}
void personType::setLastName(string myLastName)
{
lastName = myLastName;
}
string personType::getFirstName() const
{
return firstName;
}
string personType::getLastName() const
{
return lastName;
}
void personType::print() const
{
cout << " " << firstName << " " << lastName;
}
personType::personType(string myFirstName, string myLastName)
{
setFirstName(myFirstName);
setLastName(myLastName);
}
personType.h
#ifndef personType_H
#define personType_H
#include
#include
//#include "dateType.h"
using namespace std;
class personType
{
public:
void setFirstName(string myFirstName);
void setLastName(string myLastName);
string getFirstName() const;
string getLastName() const;
void print() const;
personType(string = " ", string = " ");
private:
string firstName;
string lastName;
};
#endif
main.cpp
#include
#include "addressBookType.h"
#include "addressBookType.cpp"
using namespace std;
int main()
{
cout << " A program that works with addressBookType.";
addressBookType add1;
cout << " Before sorting, the addresses are:";
add1.printAddressBook();
add1.sortByLastName();
cout << " After sorting, the addresses are:";
add1.printAddressBook();
if (add1.searchByName() == -1)
cout << " No person found as specified by you.";
else
cout << " Person found as specified by you.";
add1.printNamesBetween();
add1.printByBirthday();
add1.printAssociation();
return 0;
}
Explanation / Answer
/*
* File: main.cpp
*/
#include< cstdlib>
#include< iostream>
#include< fstream>
#include< string>
usingnamespace std;
enum contactGroupType {// used in extPersonType
FAMILY,
FRIEND,
BUSINESS,
UNFILLED
};
class addressType
{
private:
string st_address;
string city;
string state;
int zip;
public:
void print(string, string, string, int)const;
void setStreet(string);
string getStreet()const;
void setCity(string);
string getCity()const;
void setState(string);
string getState()const;
void setZip(int);
int getZip()const;
void set(string, string, string, int);// set all address fields
string get()const;// get address as one concatenated string
addressType();
// ~addressType();
};
class personType
{
private:
string firstName;
string lastName;
public:
void print()const;
void setName(string first, string last);
string getFirstName()const;
string getLastName()const;
string get()const;// return First Last names concatenated
personType & operator=(const personType &);
personType(string, string);
personType();
};
class dateType
{
private:
int dMonth;
int dDay;
int dYear;
public:
void setDate(int month, int day, int year);
int getDay()const;
int getMonth()const;
int getYear()const;
void print()const;
string get()const;// return string representation as DD/MM/YYYY
dateType & operator=(const dateType & d);
dateType(int, int, int);
dateType();
};
class extPersonType:public personType {
private:
addressType address;// added members
dateType birthday;
contactGroupType group;
string phone;
public:
// methods
void setPhone(string);
string getPhone()const;
void setGroup(contactGroupType);
contactGroupType getGroup()const;
void setBirthday(int, int, int);
dateType getBirthday()const;
void print();
string get()const;// return string representation of ext person type
extPersonType & operator=(const extPersonType & p);
string groupToString(contactGroupType)const;
contactGroupType extPersonType::stringToGroup(string)const;
// constructors
extPersonType();
extPersonType(string first, string last);
};
// because we have no arrayListType, we are using our own
// implementation with a small subset of functions
class arrayListType {
extPersonType array[500];
int size;
public:
arrayListType();
extPersonType & operator[](int i);
void removeLast();// remove last element
void add (const extPersonType &);// add new element
int getSize()const;// get array size
};
class addressBookType :public arrayListType {
private:
staticconstchar FS=' ';// field separator in file (TAB char)
int current;// current position
string fileName;// filename
fstream fileStream;// file as fstream
/* filiters */
contactGroupType fltGroup;
string fltFromLast, fltToLast;
dateType fltFromDate, fltTiDate;
/* flags for effective filters */
bool fltStatus, fltLast, fltDateRange, fltDate;
/* field numbering in the file */
staticconstint _first=0;
staticconstint _last=1;
staticconstint _street=2;
staticconstint _city=3;
staticconstint _state=4;
staticconstint _phone=5;
staticconstint _zip=6;
staticconstint _year=7;
staticconstint _month=8;
staticconstint _day=9;
staticconstint _group=10;
staticconstint _end=_group;
public:
addressBookType();
bool readFile(string);// read file into addressBook array
bool writeFile(string);// pass filename, write to file
void reset();// reset 'current' position
extPersonType * getNext();// allows to navigate in forward direction
/* by group name */
void setFilterStatus(contactGroupType);
/* by last name */
void setFilterLastname(string from, string to);// define range
/* by birthday */
void setFilterBirthday(dateType from, dateType to);
/* clear all filters */
void clearFilters();
void print(int i);// print personal data of [i] person
};
// Main program
int main(){
return0;
}
/*****************implementation of print & set function*****/
// constructor with parameters
addressType::addressType(){
st_address="";
city="";
state="";
zip=0;
}
void addressType::set(string addr, string city, string state, int zip){
this->st_address=addr;
this->city=city;
this->state=state;
this->zip=zip;
}
void addressType::setStreet(string street)
{
this->st_address=street;
}
string addressType::getStreet()const
{
return this->st_address;
}
void addressType::setCity(string street)
{
this->city=street;
}
string addressType::getState()const
{
return this->state;
}
void addressType::setState(string street)
{
this->st_address=street;
}
void addressType::setZip(int code)
{
this->zip=code;
}
int addressType::getZip()const
{
return this->zip;
}
/* personType implementation */
personType::personType(){// constructor
this->setName("", "");
}
personType::personType(string first, string last){// constructor
this->setName(first, last);
}
void personType::setName(string first, string last){
firstName=first;
lastName=last;
}
string personType::getFirstName()const{
return firstName;
}
string personType::getLastName()const{
return lastName;
}
void personType::print()const{
cout<< get()<<" ";
}
string personType::get()const{
return firstName +" "+ lastName;
}
personType & personType::operator=(const personType & p){
setName(p.getFirstName(), p.getLastName());
return*this;
}
/* Constructor */
dateType::dateType(){
setDate(1, 1, 1900);
}
dateType::dateType(int d, int m, int y){
setDate(d, m, y);
}
int dateType::getDay()const{
return dDay;
}
int dateType::getMonth()const{
return dMonth;
}
int dateType::getYear()const{
return dYear;
}
void dateType::setDate(int d, int m, int y){
dDay=d;
dMonth=m;
dYear=y;
}
void dateType::print()const{
cout<< get()<<" ";
}
string dateType::get()const{
string a;
a=dDay;
a +="/";
a += dMonth;
a +="/";
a += dYear;
return a;
}
dateType & dateType::operator=(const dateType & d){
this->dDay=d.getDay();
this->dMonth=d.getMonth();
this->dYear=d.getYear();
return*this;
}
/* Implementation of extPersonType */
extPersonType::extPersonType(){
phone="";
group=UNFILLED;
birthday.setDate(01, 01, 1900);
}
extPersonType::extPersonType(string first, string last): personType::personType(first, last){
phone="";
group=UNFILLED;
birthday.setDate(01, 01, 1900);
}
void extPersonType::setBirthday(int d, int m, int y){
birthday.setDate(d, m, y);
}
dateType extPersonType::getBirthday()const{
return birthday;
}
void extPersonType::setGroup(contactGroupType gr){
group=gr;
}
contactGroupType extPersonType::getGroup()const{
return group;
}
void extPersonType::setPhone(string ph){
phone=ph;
}
string extPersonType::getPhone()const{
return phone;
}
// Override parent's 'get()' add phone and birthday
string extPersonType::get()const{
string result;
result = this->personType::get();
result=result+" "+ birthday.get()+" "+ phone +" "+ groupToString(group);
return result;
}
extPersonType & extPersonType::operator=(const extPersonType & p){
// first call superclass' operator=
(personType)*this=(personType) p;
// now assign birthday, phone and category
this->phone = p.getPhone();
this->birthday = p.getBirthday();
this->group = p.getGroup();
return*this;
}
string extPersonType::groupToString(contactGroupType a)const{
string result;
switch(a){
case FAMILY:
result="FAMILY";
break;
case FRIEND:
result="FRIEND";
break;
case BUSINESS:
result="BUSINESS";
break;
case UNFILLED:
result="";
break;
}
return result;
}
contactGroupType extPersonType::stringToGroup(string a)const{
contactGroupType result;
if(a.compare("FAMILY")==0) result=FAMILY;
elseif(a.compare("FRIEND")==0) result=FRIEND;
elseif(a.compare("BUSINESS")==0) result=BUSINESS;
else result=UNFILLED;
return result;
}
arrayListType::arrayListType(){
size=0;
}
int arrayListType::getSize()const{
return size;
}
void arrayListType::removeLast(){
size--;
}
void arrayListType::add(const extPersonType &p){
array[size++]=p;
}
extPersonType & arrayListType::operator[](int i){
return array[i];
}
/* addressBookType implementation */
addressBookType::addressBookType(): arrayListType::arrayListType(){
reset();
clearFilters();
}
void addressBookType::reset(){
current=0;
}
void addressBookType::clearFilters(){
fltStatus=fltDate=fltLast=fltDateRange=false;
}
/* Read array from file, return false on error */
bool addressBookType::readFile(string filename){
string line;
string fields[_end+1];// _last index of the last field
extPersonType p;// temporary 'person' instance
int pos1=0, pos2=0, index;
fileStream.open(filename.c_str(), ios::in);
reset();// reset 'current' counter
clearFilters();
while(!fileStream.eof()){// read line by line
fileStream >> line;
// fields are in the following order:
// first, last, street, city, state, zip, phone, status, year, month, day
for(index=0; index<=_end; index++) fields[index]="";// initialize
pos2=0; pos1=-1;
index=0;
do{// read field by field
pos1=pos2+1;// +1 is for field separator
pos2=line.find(FS, pos1);
fields[index]=line.substr(pos1, pos2-pos1);// get field from line
index++;
}while(pos2>=0 && index<=_end);
// now fields[] are filled with fields from file
p.setName(fields[_first], fields[_last]);
p.setPhone(fields[_phone]);
p.setGroup(p.stringToGroup(fields[_group]));// convert string to enum
// set birthday
p.getBirthday().setDate(atoi(fields[_month].c_str()),
atoi(fields[_day].c_str()),
atoi(fields[_year].c_str()));
// add 'p' to array
this->add(p);
}// while () next line from file
fileStream.close();
returntrue;
}
// Write to file (stub)
bool addressBookType::writeFile(string filename){
returntrue;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.