Help with the following code: 1. Rewrite to be contained in a vector object. 2.
ID: 3795461 • Letter: H
Question
Help with the following code:
1. Rewrite to be contained in a vector object.
2. Using linked lists, redo to handle as many entries as required.
3. Add the ability to add or delete a new entry to the address book.
4. When the program terminates, write the data in the address book to a disk.
# include <iostream>
# include <conio.h>
# include <stdio.h>
using namespace std;
class addressbook
{
private:
char name[500];
long ph_num; //class addressType :public addressbook
char address[500];
int year;
int mounth;
int day;
char gender;
int d, Th, y;
char M, F, m, f;
// char freind[];
public:
addressbook();
void setName();
void getName();
void getphone();
long setphone();
void setAddress();
void getAddress();
float getAverage();
int setDay();
void getDay();
int setMounth();
void getMounth();
int setYear();
void getYear();
char setGender();
void getGender();
};
addressbook::addressbook() // constructor
{
name;
ph_num = 0;
year = 0;
mounth = 0;
day = 0;
}
int addressbook::setDay()
{
cout << " Enter the day: " << endl;
cin >> d;
day = (d >= 1 && d<32) ? d : 0;
return day;
}
int addressbook::setMounth()
{
cout << " Enter the mounth: " << endl;
cin >> m;
mounth = (Th >= 1 && Th<13) ? Th : 0;
return mounth;
}
int addressbook::setYear()
{
cout << " Enter the year : " << endl;
cin >> y;
year = (y >= 1 && y<2008) ? y : 0;
return year;
}
void addressbook::getMounth()
{
cout << mounth << "/";
}
void addressbook::getDay()
{
cout << " BIRTH DAY : " << day << "/";
}
void addressbook::getYear()
{
cout << year;
}
char addressbook::setGender()
{
int flag = 0;
cout << " Enter the gender,(F)for female (M)for male: ";
cin >> gender;
while (flag != 1)
{
if (gender == ('F') || gender == ('M') || gender == ('m') || gender == ('f'))
flag = 1;
else
{
cout << " error.....Try Again ";
cin >> gender;
return gender;
}
}
}
void addressbook::getGender()
{
cout << " Gender : " << gender << endl;
}
void addressbook::getAddress()
{
cout << " Address : " << address << endl;
}
float addressbook::getAverage()
{
return 0.0f;
}
void addressbook::setAddress()
{
cout << " Enter the address: " << endl;
gets_s(address);
}
void addressbook::setName()
{
cout << " Enter the name: " << endl;
gets_s(name);
}
void addressbook::getName()
{
cout << " Name : " << name << " " << endl;
}
long addressbook::setphone()
{
cout << " Enter the person number: " << endl;
cin >> ph_num;
return ph_num;
}
void addressbook::getphone()
{
cout << " Person # : " << ph_num << endl;
}
/*class addressType :public addressbook
{
do that
};*/
int main()
{
int s;
cout << " Enter number person max(500): ";
cin >> s;
addressbook book[10];
for (int i = 1; i<2; i++)
{
book[i].setName();
book[i].setphone();
book[i].setAddress();
book[i].setDay();
book[i].setMounth();
book[i].setYear();
book[i].setGender();
cout << " *********************************** ";
}
for (int u = 0; u<2; u++)
{
book[u].getName();
book[u].getphone();
book[u].getAddress();
book[u].getGender();
book[u].getDay();
book[u].getMounth();
book[u].getYear();
}
_getch();
}
Explanation / Answer
Question 1
Changes to be made in the code:
Instead of:
char name[500];
write:
vector<char> name(500);
similarly, instead of:
char address[500];
change it to:
vector<char> address(500);
and finally change
addressbook book[10];
to:
vector<char> book(10);
You might have figured it out by the changes but for reference, changing an array to vector has to be done by mentioning "vector" followed by the "data_type" inside "<>" and then mentioning the name of the identifier(variable) like in an array and providing its size, but the size has to be inside "()" instead of "[]".
Edit1- include the vector header file at the header section as:
#include<vector>
Here is the complete code-
# include <iostream>
# include <conio.h>
# include <stdio.h>
#include <vector>
using namespace std;
class addressbook
{
private:
vector<char> name(500);
long ph_num; //class addressType :public addressbook
vector<char> address(500);
int year;
int mounth;
int day;
char gender;
int d, Th, y;
char M, F, m, f;
// char freind[];
public:
addressbook();
void setName();
void getName();
void getphone();
long setphone();
void setAddress();
void getAddress();
float getAverage();
int setDay();
void getDay();
int setMounth();
void getMounth();
int setYear();
void getYear();
char setGender();
void getGender();
};
addressbook::addressbook() // constructor
{
name;
ph_num = 0;
year = 0;
mounth = 0;
day = 0;
}
int addressbook::setDay()
{
cout << " Enter the day: " << endl;
cin >> d;
day = (d >= 1 && d<32) ? d : 0;
return day;
}
int addressbook::setMounth()
{
cout << " Enter the mounth: " << endl;
cin >> m;
mounth = (Th >= 1 && Th<13) ? Th : 0;
return mounth;
}
int addressbook::setYear()
{
cout << " Enter the year : " << endl;
cin >> y;
year = (y >= 1 && y<2008) ? y : 0;
return year;
}
void addressbook::getMounth()
{
cout << mounth << "/";
}
void addressbook::getDay()
{
cout << " BIRTH DAY : " << day << "/";
}
void addressbook::getYear()
{
cout << year;
}
char addressbook::setGender()
{
int flag = 0;
cout << " Enter the gender,(F)for female (M)for male: ";
cin >> gender;
while (flag != 1)
{
if (gender == ('F') || gender == ('M') || gender == ('m') || gender == ('f'))
flag = 1;
else
{
cout << " error.....Try Again ";
cin >> gender;
return gender;
}
}
}
void addressbook::getGender()
{
cout << " Gender : " << gender << endl;
}
void addressbook::getAddress()
{
cout << " Address : " << address << endl;
}
float addressbook::getAverage()
{
return 0.0f;
}
void addressbook::setAddress()
{
cout << " Enter the address: " << endl;
gets_s(address);
}
void addressbook::setName()
{
cout << " Enter the name: " << endl;
gets_s(name);
}
void addressbook::getName()
{
cout << " Name : " << name << " " << endl;
}
long addressbook::setphone()
{
cout << " Enter the person number: " << endl;
cin >> ph_num;
return ph_num;
}
void addressbook::getphone()
{
cout << " Person # : " << ph_num << endl;
}
/*class addressType :public addressbook
{
do that
};*/
int main()
{
int s;
cout << " Enter number person max(500): ";
cin >> s;
vector<addressbook> book(10);
for (int i = 1; i<2; i++)
{
book[i].setName();
book[i].setphone();
book[i].setAddress();
book[i].setDay();
book[i].setMounth();
book[i].setYear();
book[i].setGender();
cout << " *********************************** ";
}
for (int u = 0; u<2; u++)
{
book[u].getName();
book[u].getphone();
book[u].getAddress();
book[u].getGender();
book[u].getDay();
book[u].getMounth();
book[u].getYear();
}
_getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.