I need to edit this code so that the address book is stored in a vector (C++) #i
ID: 3738910 • Letter: I
Question
I need to edit this code so that the address book is stored in a vector (C++)
#include<iostream>
#include<ctime>
#include<cstdlib>
#include <stdio.h>
#include <fstream>
#include<sstream>
using namespace std;
class addressType
{
private:
string userStreetAddress;
string UserCity;
string UserState;
string UserZipcode;
public:
void print()
{
cout<<" ----- User ADDRESS ----- ";
cout<<" Street: "<<userStreetAddress;
cout<<" UserCity: "<<UserCity;
cout<<" UserState: "<<UserState;
cout<<" UserZipcode: "<<UserZipcode;
}
addressType()
{
userStreetAddress = UserCity = UserState = UserZipcode = "";
}
addressType(string sa, string c, string st, string z)
{
userStreetAddress = sa;
UserCity = c;
UserState = st;
UserZipcode = z;
}
void setuserStreetAddress(string sa)
{
userStreetAddress = sa;
}
string getuserStreetAddress()
{
return userStreetAddress;
}
void setUserCity(string c)
{
UserCity = c;
}
string getUserCity()
{
return UserCity;
}
void setUserState(string sa)
{
UserState = sa;
}
string getUserState()
{
return UserState;
}
void setUserZipcode(string z)
{
UserZipcode = z;
}
string getUserZipcode()
{
return UserZipcode;
}
};
class dateType
{
private:
int date, month, year;
public:
void print()
{
cout<<" Date of Birth: "<<date<<":"<<month<<":"<<year;
}
dateType()
{
date = month = year = 0;
}
dateType(int d, int m, int y)
{
date = d;
month = m;
year = y;
}
void setDate(int d)
{
date = d;
}
int getDate()
{
return date;
}
void setMonth(int m)
{
month = m;
}
int getMonth()
{
return month;
}
void setYear(int y)
{
year = y;
}
int getYear()
{
return year;
}
};
class personType
{
public:
personType(string first = " ", string last = " ")
{
firstName = first;
lastName = last;
}
void print() const
{
cout<<" First Name: "<<firstName<<" Last Name: "<<lastName;
}
void setName(string first, string last)
{
firstName = first;
lastName = last;
}
string getFirstName( ) const
{
return firstName;
}
string getLastName( ) const
{
return lastName;
}
private:
string firstName;
string lastName;
};
class extPersonType : public personType
{
private:
string phoneNumber;
string personStatus;
public:
addressType address;
dateType dateOfBirth;
void print() const
{
cout<<" Phone Number: "<<phoneNumber;
cout<<" Person Type: "<<personStatus;
}
extPersonType()
{ }
extPersonType(addressType a, dateType d, string ph, string ps, string fi, string la):personType(fi, la)
{
address = a;
dateOfBirth = d;
phoneNumber = ph;
personStatus = ps;
}
void setPhoneNumber(string p)
{
phoneNumber = p;
}
string getPhoneNumber()
{
return phoneNumber;
}
void setPersonStatus(string p)
{
personStatus = p;
}
string getPersonStatus()
{
return personStatus;
}
};
class addressBookType
{
private:
extPersonType ept[100];
int numberOfRecord;
public:
int getNumberOfRecord()
{
return numberOfRecord;
}
void readFile();
void displayFile();
void sortLastName();
void searchLastName(string, int);
void duplicateMonth();
void duplicateType(string);
};
void addressBookType::duplicateType(string type)
{
int x, y;
for(x = 0; x < getNumberOfRecord(); x++)
{
if(ept[x].getPersonStatus() == type)
{
cout<<" ******************* Person Information ****************";
ept[x].personType::print();
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
}
}
}
void addressBookType::duplicateMonth()
{
int x, y;
int month;
for(x = 0; x < getNumberOfRecord(); x++)
{
month = ept[x].dateOfBirth.getMonth();
for(y = x+1; y < getNumberOfRecord(); y++)
{
if(ept[y].dateOfBirth.getMonth() == month)
{
cout<<" ******************* Person Information ****************";
ept[x].personType::print();
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
cout<<" ******************* Person Information ****************";
ept[y].personType::print();
ept[y].address.print();
ept[y].dateOfBirth.print();
ept[y].print();
}
}
}
}
void addressBookType::searchLastName(string name, int no)
{
int x;
int flag = 0;
for(x = 0; x < getNumberOfRecord(); x++)
{
if(ept[x].personType::getLastName() == name)
{
if(no == 2)
{
cout<<" ******************* Person Information ****************";
ept[x].personType::print();
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
}
else
{
cout<<" ******************* Person address phone number and date of birth ****************";
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
}
flag = 1;
}
}
if(flag != 1)
cout<<" Record for "<<name<<" not found.";
}
void addressBookType::readFile()
{
int co = 0;
ifstream rFile;
cout << "opening Address.txt file ";
rFile.open("Address.txt");
string f, l;
int d;
while(!rFile.eof())
{
rFile>>f;
rFile>>l;
ept[co].setName(f, l);
rFile>>l;
ept[co].address.setuserStreetAddress(l);
rFile>>l;
ept[co].address.setUserCity(l);
rFile>>l;
ept[co].address.setUserState(l);
rFile>>l;
ept[co].address.setUserZipcode(l);
rFile>>d;
ept[co].dateOfBirth.setDate(d);
rFile>>d;
ept[co].dateOfBirth.setMonth(d);
rFile>>d;
ept[co].dateOfBirth.setYear(d);
rFile>>l;
ept[co].setPhoneNumber(l);
rFile>>l;
ept[co].setPersonStatus(l);
co++;
}
rFile.close();
numberOfRecord = co;
}
void addressBookType::displayFile()
{
for(int x = 0; x < numberOfRecord; x++)
{
cout<<" ******************* Person "<<(x + 1)<<" Information ****************";
ept[x].personType::print();
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
}
}
void addressBookType::sortLastName()
{
int x, y;
extPersonType temp;
for(x = 0; x < getNumberOfRecord(); x++)
{
for(y = 0; y < getNumberOfRecord() - x - 1; y++)
{
if(ept[y].personType::getLastName() > ept[y + 1].personType::getLastName())
{
temp = ept[y];
ept[y] = ept[y + 1];
ept[y + 1] = temp;
}
}
}
}
int menu()
{
int choice;
cout<<" 1. Sort the address book by last name. ";
cout<<" 2. Search for a person by last name. ";
cout<<" 3. Print the address phone number and date of birth of a given person (if exist). ";
cout<<" 4. Print names of people whose birthdays are in the same month. ";
cout<<" 5. Depending on request, print all family members, friends or business associates. ";
cout<<" 6. Exit";
cout<<" Enter your choice: ";
cin>>choice;
return choice;
}
int main()
{
addressBookType ad;
ad.readFile();
int choice;
string data;
do
{
choice = menu();
switch(choice)
{
case 1:
ad.sortLastName();
ad.displayFile();
break;
case 2:
cout<<" Enter the last name to search record: ";
cin>>data;
ad.searchLastName(data, 2);
break;
case 3:
cout<<" Enter the last name to print address phone number and date of birth: ";
cin>>data;
ad.searchLastName(data, 3);
break;
case 4:
ad.duplicateMonth();
break;
case 5:
cout<<" Enter the person type (family / friends / business): ";
cin>>data;
ad.duplicateType(data);
break;
case 6:
exit(0);
default:
cout<<" Invalid choice!";
}
}while(1);
}
Explanation / Answer
// Code updated for vector instead of array, rest everything remains same
#include<iostream>
#include<ctime>
#include<cstdlib>
#include <stdio.h>
#include <fstream>
#include<sstream>
#include <vector>
using namespace std;
class addressType
{
private:
string userStreetAddress;
string UserCity;
string UserState;
string UserZipcode;
public:
void print()
{
cout<<" ----- User ADDRESS ----- ";
cout<<" Street: "<<userStreetAddress;
cout<<" UserCity: "<<UserCity;
cout<<" UserState: "<<UserState;
cout<<" UserZipcode: "<<UserZipcode;
}
addressType()
{
userStreetAddress = UserCity = UserState = UserZipcode = "";
}
addressType(string sa, string c, string st, string z)
{
userStreetAddress = sa;
UserCity = c;
UserState = st;
UserZipcode = z;
}
void setuserStreetAddress(string sa)
{
userStreetAddress = sa;
}
string getuserStreetAddress()
{
return userStreetAddress;
}
void setUserCity(string c)
{
UserCity = c;
}
string getUserCity()
{
return UserCity;
}
void setUserState(string sa)
{
UserState = sa;
}
string getUserState()
{
return UserState;
}
void setUserZipcode(string z)
{
UserZipcode = z;
}
string getUserZipcode()
{
return UserZipcode;
}
};
class dateType
{
private:
int date, month, year;
public:
void print()
{
cout<<" Date of Birth: "<<date<<":"<<month<<":"<<year;
}
dateType()
{
date = month = year = 0;
}
dateType(int d, int m, int y)
{
date = d;
month = m;
year = y;
}
void setDate(int d)
{
date = d;
}
int getDate()
{
return date;
}
void setMonth(int m)
{
month = m;
}
int getMonth()
{
return month;
}
void setYear(int y)
{
year = y;
}
int getYear()
{
return year;
}
};
class personType
{
public:
personType(string first = " ", string last = " ")
{
firstName = first;
lastName = last;
}
void print() const
{
cout<<" First Name: "<<firstName<<" Last Name: "<<lastName;
}
void setName(string first, string last)
{
firstName = first;
lastName = last;
}
string getFirstName( ) const
{
return firstName;
}
string getLastName( ) const
{
return lastName;
}
private:
string firstName;
string lastName;
};
class extPersonType : public personType
{
private:
string phoneNumber;
string personStatus;
public:
addressType address;
dateType dateOfBirth;
void print() const
{
cout<<" Phone Number: "<<phoneNumber;
cout<<" Person Type: "<<personStatus;
}
extPersonType()
{ }
extPersonType(addressType a, dateType d, string ph, string ps, string fi, string la):personType(fi, la)
{
address = a;
dateOfBirth = d;
phoneNumber = ph;
personStatus = ps;
}
void setPhoneNumber(string p)
{
phoneNumber = p;
}
string getPhoneNumber()
{
return phoneNumber;
}
void setPersonStatus(string p)
{
personStatus = p;
}
string getPersonStatus()
{
return personStatus;
}
};
class addressBookType
{
private:
//extPersonType ept[100];
// vector initializization for extPersonType
std::vector<extPersonType> ept(100);
int numberOfRecord;
public:
int getNumberOfRecord()
{
return numberOfRecord;
}
void readFile();
void displayFile();
void sortLastName();
void searchLastName(string, int);
void duplicateMonth();
void duplicateType(string);
};
void addressBookType::duplicateType(string type)
{
int x, y;
for(x = 0; x < getNumberOfRecord(); x++)
{
if(ept[x].getPersonStatus() == type)
{
cout<<" ******************* Person Information ****************";
ept[x].personType::print();
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
}
}
}
void addressBookType::duplicateMonth()
{
int x, y;
int month;
for(x = 0; x < getNumberOfRecord(); x++)
{
month = ept[x].dateOfBirth.getMonth();
for(y = x+1; y < getNumberOfRecord(); y++)
{
if(ept[y].dateOfBirth.getMonth() == month)
{
cout<<" ******************* Person Information ****************";
ept[x].personType::print();
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
cout<<" ******************* Person Information ****************";
ept[y].personType::print();
ept[y].address.print();
ept[y].dateOfBirth.print();
ept[y].print();
}
}
}
}
void addressBookType::searchLastName(string name, int no)
{
int x;
int flag = 0;
for(x = 0; x < getNumberOfRecord(); x++)
{
if(ept[x].personType::getLastName() == name)
{
if(no == 2)
{
cout<<" ******************* Person Information ****************";
ept[x].personType::print();
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
}
else
{
cout<<" ******************* Person address phone number and date of birth ****************";
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
}
flag = 1;
}
}
if(flag != 1)
cout<<" Record for "<<name<<" not found.";
}
void addressBookType::readFile()
{
int co = 0;
ifstream rFile;
cout << "opening Address.txt file ";
rFile.open("Address.txt");
string f, l;
int d;
while(!rFile.eof())
{
rFile>>f;
rFile>>l;
ept[co].setName(f, l);
rFile>>l;
ept[co].address.setuserStreetAddress(l);
rFile>>l;
ept[co].address.setUserCity(l);
rFile>>l;
ept[co].address.setUserState(l);
rFile>>l;
ept[co].address.setUserZipcode(l);
rFile>>d;
ept[co].dateOfBirth.setDate(d);
rFile>>d;
ept[co].dateOfBirth.setMonth(d);
rFile>>d;
ept[co].dateOfBirth.setYear(d);
rFile>>l;
ept[co].setPhoneNumber(l);
rFile>>l;
ept[co].setPersonStatus(l);
co++;
}
rFile.close();
numberOfRecord = co;
}
void addressBookType::displayFile()
{
for(int x = 0; x < numberOfRecord; x++)
{
cout<<" ******************* Person "<<(x + 1)<<" Information ****************";
ept[x].personType::print();
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
}
}
void addressBookType::sortLastName()
{
int x, y;
extPersonType temp;
for(x = 0; x < getNumberOfRecord(); x++)
{
for(y = 0; y < getNumberOfRecord() - x - 1; y++)
{
if(ept[y].personType::getLastName() > ept[y + 1].personType::getLastName())
{
temp = ept[y];
ept[y] = ept[y + 1];
ept[y + 1] = temp;
}
}
}
}
int menu()
{
int choice;
cout<<" 1. Sort the address book by last name. ";
cout<<" 2. Search for a person by last name. ";
cout<<" 3. Print the address phone number and date of birth of a given person (if exist). ";
cout<<" 4. Print names of people whose birthdays are in the same month. ";
cout<<" 5. Depending on request, print all family members, friends or business associates. ";
cout<<" 6. Exit";
cout<<" Enter your choice: ";
cin>>choice;
return choice;
}
int main()
{
addressBookType ad;
ad.readFile();
int choice;
string data;
do
{
choice = menu();
switch(choice)
{
case 1:
ad.sortLastName();
ad.displayFile();
break;
case 2:
cout<<" Enter the last name to search record: ";
cin>>data;
ad.searchLastName(data, 2);
break;
case 3:
cout<<" Enter the last name to print address phone number and date of birth: ";
cin>>data;
ad.searchLastName(data, 3);
break;
case 4:
ad.duplicateMonth();
break;
case 5:
cout<<" Enter the person type (family / friends / business): ";
cin>>data;
ad.duplicateType(data);
break;
case 6:
exit(0);
default:
cout<<" Invalid choice!";
}
}while(1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.