9. Department maintains a student information. The file containsrollnumber, name, division and address. Allow user to add, delete information of student. Display information of particular employee. If record of student does not exist an appropriate message is displayed. If it is, then the system displays the student details.Use sequential file to main the data.

#include<iostream>
#include<fstream>
using namespace std;

class student
{
	int roll_no;
	string name,div,address;
	
public:
	
	student();
	void add_record();
	void delete_data(string);
	void modify_record();
	void search_data(string);
	void input_data();
	void write_data();
	void read_data();
	
};

student :: student() : roll_no(0) , name(""), address(""),div (""){}

void student :: input_data()
{
	cout<<": ENTER STUDENTS DETAILS :"<<endl<<endl;
	cout<<": ROLL NO  = ";
	cin>>roll_no;
	
	cin.ignore();
	
	cout<<": NAME     = ";
	getline(cin,name);
	cout<<": DIVISION = ";
	getline(cin , div);
	cout<<": ADDRESS  = ";
	getline(cin , address);
	write_data();
}

// FUNCTION TO WRITE DATA INTO THE FILE
void student :: write_data()
{
	fstream obj;
	obj.open("student_data.txt" , ios::out | ios::app);
	obj << roll_no <<" "<<name<<" "<<div<<" "<<address<<endl;
	cout<<endl<<"FILE UPDATED SUCCESSFULLY !!"<<endl;
	obj.close();
}

// FUNCTION TO READ DATA FROM THE FILE
void student :: read_data()
{
	fstream obj;
	string file_line;						// to accept record from file line by line
	obj.open("student_data.txt" , ios :: in);
	cout<<"=========== FILE DATA =========== "<<endl;
	while( obj )
	{
		getline( obj , file_line );
		cout<<endl<<file_line<<endl;
	}
	obj.close();
}	

// -- searching fuction -- 
void student :: search_data(string search_name)
{
	fstream obj;
	string file_line;
	obj.open("student_data.txt" , ios :: in );
	int found = 0;
	while( obj )
	{
		getline( obj , file_line );
		   							
		size_t pos = file_line.find(search_name);		
		
		if( pos != string :: npos)
		{
			cout<<endl<<": RECORD FOUND :"<<endl;
			cout<<endl<<file_line<<endl;
			found = 1;
			break;
		}
	}
	if(found == 0)
	{
		cout<<"-- RECORD DOES NOT EXIST --"<<endl;
	} 
	obj.close();
}

void student :: delete_data(string search_name)
{
	fstream obj,obj1;
	obj.open("student_data.txt" , ios :: in);
	obj1.open("temp.txt" , ios :: out);
	string file_line;
	cout<<endl<<"========== DELETION OF RECORDS ============"<<endl;
	while(obj)
	{
		getline(obj , file_line);
		
		size_t pos = file_line.find(search_name);
		
		if( pos == string :: npos)
		{
			obj1 << file_line << endl;
		}
	}
	obj.close();
	obj1.close();
	
	remove("student_data.txt");
	rename("temp.txt","student_data.txt");
	cout<<endl<<"........ FILE UPDATED SUCCESSFULLY......."<<endl;
	
}
			
int main()
{
	student sobj;
	sobj.input_data();
	sobj.input_data();
	sobj.input_data();
	sobj.read_data();
	sobj.search_data("shreyas");
	sobj.delete_data("shreyas");
	sobj.read_data();
	return 0;
}

Comments

Popular posts from this blog

2. Beginning with an empty binary search tree, Construct binary search tree by inserting the values in the order given. After constructing a binary tree -i.Insert new nodeii.Find number of nodes in longest path iii.Minimum data value found inthe tree iv.Change a tree so that the roles of the left and right pointers are swapped at every node v.Search a value

1. A book consists of chapters, chapters consist of sections and sections consist of subsections. Construct a tree and print the nodes. Find the time and space requirements of your method.

6. Implement all the functions of a dictionary (ADT) using hashing.Data: Set of (key, value) pairs, Keys are mapped to values, Keys must be comparable, Keys must be uniqueStandard Operations: Insert(key, value), Find(key), Delete(key)