Answer to Question #100099 in C++ for Arsh

Question #100099
Declare a class containing:
Name,Address,Telephone no.
Write a menu driven program
Append record in file
Display the name and address for a given telephone no.
1
Expert's answer
2019-12-12T07:30:27-0500

#include "iostream"

#include "fstream"

#include "string"

#include "vector"


using namespace std;


class List

{

public:

List()

{

this->number = int();

this->name = string();

this->adress = string();

}

List(int num, string name, string adr)

{

this->number = num;

this->name = name;

this->adress = adr;

}

int GetNumber()

{

return number;

}

string GetName()

{

return name;

}

string GetAdress()

{

return adress;

}

void setNumber(int num)

{

this->number = num;

}

void setName(string name)

{

this->name = name;

}

void setAdress(string adr)

{

this->adress = adr;

}


private:

int number;

string name;

string adress;

};


void Find(vector<List> vect, int num)

{

for (auto elem : vect)

{

if (elem.GetNumber() == num)

{

cout << "Person's " << elem.GetNumber() << " name is " << elem.GetName() << " and adress is " << elem.GetAdress() << endl;

}

}

}


vector<List> GetData()

{

vector<List> Data;

ifstream fin("List.txt");

List obj;

int number;

while (fin >> number)

{

string name, adress;

fin >> name;

fin >> adress;

obj.setName(name);

obj.setAdress(adress);

obj.setNumber(number);

Data.push_back(obj);

}

fin.close();

return Data;

}


int main()

{

int choise = 0;

do

{

cout << " 1. Add new." << endl;

cout << " 2. Find." << endl;

cout << " 3. Exit." << endl;

cin >> choise;

switch (choise)

{

case 1:

{

vector<List> Data = GetData();

int number;

string name, adress;

cout << "Enter a number ";

cin >> number;

cout << "Enter a name ";

cin >> name;

cout << "Enter an adress ";

cin >> adress;

List obj(number, name, adress);

Data.push_back(obj);

ofstream fout("List.txt");

for (auto elem : Data)

{

fout << elem.GetNumber() << endl;

fout << elem.GetName() << endl;

fout << elem.GetAdress() << endl;

}

fout.close();

break;

}

case 2:

{

vector<List> Data = GetData();

int number;

cout << "Enter a number ";

cin >> number;

Find(Data, number);

break;

}

case 3:

{

break;

}

default:

{

cout << endl;

break;

}

}


} while (choise != 3);

system("pause");

exit (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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS