Answer to Question #312565 in C++ for Alif khan

Question #312565

Mr. Tunan needs a directory that contains the following information.

(a) Name of a person

(b) Address

(c) Telephone Number

(d) Mobile Number

(e) Head of the family

He also needs a functionality to search the information of any person. Now develop a solution

using C++ for Tunan where you need to use a constructor to insert the information and a friend

function to show the output.


1
Expert's answer
2022-03-16T10:23:49-0400
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;


class Information {
public:
    Information(string name, string address, string phone,
                string mobile, string head);
    void print() const;
    string getName() const { return name; }


private:
    string name;
    string address;
    string telephone;
    string mobile;
    string head_of_family;
};


Information::Information(string name, string address, string phone,
                         string mobile, string head)
: name(name), address(address), telephone(phone), mobile(mobile), head_of_family(head)
{}


void Information::print() const
{
    cout << "Name: " << name << endl;
    cout << "Address: " << address << endl;
    cout << "Telephone: " << telephone << endl;
    cout << "Mobile: " << mobile << endl;
    cout << "Head of family: " << head_of_family << endl;
    cout << endl;
}


void insert(vector<Information>& directory) {
    string name;
    string address;
    string phone;
    string mobile;
    string head;


    cout << "Enter a name: ";
    cin >> name;
    cout << "Address: ";
    cin >> address;
    cout << "Telephone: ";
    cin >> phone;
    cout << "Mobile: ";
    cin >> mobile;
    cout << "Head of family: ";
    cin >> head;
    
    Information inf(name, address, phone, mobile, head);
    directory.push_back(inf);
}


void find(vector<Information>& directory, string& name) {
    int i;
    for (i=0; i<directory.size(); i++) {
        if (directory[i].getName() == name) {
            directory[i].print();
            break;
        }
    }
    if ( i == directory.size()) {
        cout << name << " not found" << endl;
    }
}


int main() {
    vector<Information> direcory;
    string name;
    char ch;


    do {
        insert(direcory);
        cout << "Add another record? ";
        cin >> ch;
        ch = tolower(ch);
    } while (ch == 'y');


    do {
        cout << "Enter a name:";
        cin >> name;
        find(direcory, name);
        cout << "Search again? ";
        cin >> ch;
        ch = tolower(ch);
    } while (ch == 'y');


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment