Answer to Question #216818 in C++ for Ajay

Question #216818
Create a class called DIRECTORY with data members, name and phone number. Use member
functions, parameterized constructor to set the details and display() to print the details. In the
main function, create minimum of 5 objects to get the input and store it. Then, write the details
into the binary file called DIRECTORY.TXT. Fetch the data from the file and display it. Write
a function to search for a record using phone number. Use appropriate functions to display the
result.
1
Expert's answer
2021-07-13T13:38:37-0400
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class DIRECTORY{
		private:
			string name;
			int phoneNumber;
		public:
			DIRECTORY(string na, int p){
				name = na;
				phoneNumber = p;
			}
			string getName(){
				return name;
			}
			int getPhone(){
				return phoneNumber;
			}
			void display(){
				cout<<"The name is \t" <<getName()<<"\nThe phone number is\t"<<getPhone()<<endl;
			}
};


int main(){
	DIRECTORY d1("Johnson",902);
	DIRECTORY d2("Victor",90223);
	DIRECTORY d3("Sconfield",90212);
	DIRECTORY d4("Harun",90223);
	DIRECTORY d5("Benter",9022);
	DIRECTORY d6("Alaba",9025);
	ofstream fs("DIRECTORY.TXT", ios::out | ios::binary);
	//Writing to a file 
	fs<<d1.getName()<<"\t"<<d1.getPhone()<<"\n";
	fs<<d2.getName()<<"\t"<<d2.getPhone()<<"\n";
	fs<<d3.getName()<<"\t"<<d3.getPhone()<<"\n";
	fs<<d4.getName()<<"\t"<<d4.getPhone()<<"\n";
	fs<<d5.getName()<<"\t"<<d5.getPhone()<<"\n";
	fs.close();
	char buffer[100];
	//Reading and displaying from a file
	ifstream file("DIRECTORY.TXT", ios::in | ios::binary);
	char ch;
	while(!file.eof()){
		file.get(ch);
		cout<<ch;
	}
	
	
	
	
}

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