Can anyone possible to help me with this code? Please use pointer and array inst
ID: 3732556 • Letter: C
Question
Can anyone possible to help me with this code?
Please use pointer and array instead iterator and list!!!
Define a class named Family in a header file named: family.h (5 points) This file acts as a prototype. 1. 2. Create the implementation file family.cpp (15 points total) a. The class will have the following members 1. 2. Family name (5 points) Parent (array of 2) (5 points) a. b. c. First Name Last Name Age (you must validate the age is the proper data type. You can use the code from Lab 4) 3. Children (array of 10) (5 points) a. b. C. First Name Last Name Age (you must validate the age is the proper data type. You can use the code from Lab 4) The class will be able to add parents or children The class will be able to return information on parents and children. b. c. 3. Create the program Lab05b.cpp which uses the class Family to Implement the following a. The program will allow the user to (14 points total) 1. Add families a. With one or two parents 3 points) b. With zero or more children (3 points) Delete Families search by last name (should use the same search function from item 3 below. (1 point) Display Families searching by last name (2 points) Display a list of all families (5 points) 2. 3. 4.Explanation / Answer
ANSWER:
#include <cstdlib>
#include "family.h"
using namespace std;
int main(int argc, char** argv) {
Family obj_family;
while(true){
cout<<"1. Add a new family"<<endl;
cout<<"2. Delete a family"<<endl;
cout<<"3. View an existing family record by last name"<<endl;
cout<<"4. View Entire list of families"<<endl;
cout<<"5. Exit"<<endl;
int n;
cin>>n;
switch (n) {
case 1:
obj_family.AddParent();
obj_family.AddChildren();
break;
case 2:
obj_family.DeleteFamily();
break;
case 3:
obj_family.DisplayFamily();
break;
case 4:
obj_family.DisplayFamilies();
case 5:
obj_family.Exit();
exit(0);
break;
}
}
return 0;
}
//family.h//
#ifndef FAMILY_H
#define FAMILY_H
#include <string>
#include <iostream>
#include <vector>
using namespace std;
typedef struct{
string FirstName;
string LastName;
int Age;
}Parent;
typedef struct{
string FirstName;
string LastName;
int Age;
}Children;
typedef struct{
Parent parent[2]{};
Children children[10]{};
}V_family;
class Family{
public:
string FamilyName;
int size;
vector<V_family> v_family;
void AddParent();
void AddChildren();
void DeleteFamily();
int searchFamily(string);
void DisplayFamily();
void DisplayFamilies();
void Exit();
};
#endif / FAMILY_H /
/*///family.cpp///*/
#include <vector>
#include "family.h"
bool valid_age(int age){
return age>0 ? true : false;
}
void Family::AddParent(){
int i;
size = v_family.size()+1;
v_family.resize(size);
for(i =0 ; i <2; ++i){
if(v_family[size-1].parent[i].FirstName.empty()){
cout<<"Parent's first name :";
if(i==1){
cout<<"(or q if done)";
}
cin>>v_family[size-1].parent[i].FirstName;
if( (v_family[size-1].parent[i].FirstName.compare("q")==0)){
v_family[size-1].parent[i].FirstName = "";
return;
}
cout<<" Parent's last name :";
cin>>v_family[size-1].parent[i].LastName;
cout<<" Parent's age :";
cin>>v_family[size-1].parent[i].Age;
while(!(valid_age(v_family[size-1].parent[i].Age)) ){
cout<<" Give proper Parent's age :";
cin>>v_family[size-1].parent[i].Age;
}
}
}
}
void Family::AddChildren(){
for(int i =0 ; i <10; ++i){
if(v_family[size-1].children[i].FirstName.empty()){
cout<<" Child's first name : (or q if done)";
cin>>v_family[size-1].children[i].FirstName;
if( (v_family[size-1].children[i].FirstName.compare("q")==0)){
v_family[size-1].children[i].FirstName = "";
return;
}
cout<<" Child's last name :";
cin>>v_family[size-1].children[i].LastName;
cout<<" Child's age :";
cin>>v_family[size-1].children[i].Age;
}
}
}
int Family::searchFamily(string lastname){
if(v_family.size() == 0)
return -1;
for(int i = 0; i < size ; i++){
for(int j = 0 ; j<2 ; j++){
if((v_family[i].parent[j].LastName).compare(lastname)==0)
return i;
}
}
return -1;
}
void Family::DeleteFamily(){
string lastName;
cout<<" Enter the family name : ";
cin>>lastName;
int node = searchFamily(lastName);
if(node == -1){
cout<<"Family structure is empty.."<<endl;
return;
}
v_family.erase(v_family.begin()+node);
size--;
}
void Family::DisplayFamily(){
string lastName;
cout<<" Enter the family name : ";
cin>>lastName;
int node = searchFamily(lastName);
if(node == -1){
cout<<"Family structure is empty.."<<endl;
return;
}
for(int j = 0 ; j<2 ; j++){
if(v_family[node].parent[j].Age!=0){
cout<<" Parent(s):"<<endl;
cout<<" Name : "<<v_family[node].parent[j].FirstName<<" "<<v_family[node].parent[j].LastName<<endl;
cout<<" Age : "<<v_family[node].parent[j].Age<<endl;
}
}
for(int j = 0 ; j<10 ; j++){
if(v_family[node].children[j].Age!=0){
cout<<" Children:"<<endl;
cout<<" Name : "<<v_family[node].children[j].FirstName<<" "<<v_family[node].children[j].LastName<<endl;
cout<<" Age : "<<v_family[node].children[j].Age<<endl;
}
}
}
void Family::DisplayFamilies(){
for(int i = 0; i < size ; i++){
for(int j = 0 ; j<2 ; j++){
if(v_family[i].parent[j].Age!=0){
cout<<" Parent(s):"<<endl;
cout<<" Name : "<<v_family[i].parent[j].FirstName<<" "<<v_family[i].parent[j].LastName<<endl;
cout<<" Age : "<<v_family[i].parent[j].Age<<endl;
}
}
for(int j = 0 ; j<10 ; j++){
if(v_family[i].children[j].Age!=0){
cout<<" Children:"<<endl;
cout<<" Name : "<<v_family[i].children[j].FirstName<<" "<<v_family[i].children[j].LastName<<endl;
cout<<" Age : "<<v_family[i].children[j].Age<<endl;
}
}
}
}
void Family::Exit(){
if(v_family.size() == 0){
cout<<"Bye Bye.."<<endl;
return;
}else{
cout<<""So long from the family <"<<v_family[0].parent[0].LastName<<">family!" where <"<<v_family[0].parent[0].LastName<<"> is the first family in the list.."<<endl;
}
}