#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char choice;
do{
cout<<" ****Employee system****";
cout<<endl<<"Press ‘S’: Add more 5 records in the file with existing file
format";
cout<<endl<<"Press ‘P’: Print all records on console";
cout<<endl<<"Press ‘E’: Find the employee which has max salary on the console";
cout<<endl<<"Press ‘D’: Department wise searching";
cout<<endl<<"Press ‘R’: Write all the record into another file except the one
who is going to leave the department";
cout<<endl<<"Press ‘X’ : exit";
cout<<endl<<"Enter choice:";
cin>>choice;
if(choice=='s'||choice=='S')
{
ofstream write("source.txt",ios::app);
if(!write)
{
cout << "Error" << endl;
return 0;
}
cin.ignore();
for(int i=1;i<=5;i++)
{
char name[50], department[50];
int salary;
cout<<"Enter name: ";
cin.getline(name,50);
cout<<"Enter salary: ";
cin>>salary;
cin.ignore();
cout<<"Enter department: ";
cin.getline(department,50);
write<<name<<", "<<salary<<", "<<department<<endl;
}
write.close();
}
else if(choice=='p'||choice=='P')
{
cout<<endl<<"Your results: "<<endl;
ifstream read("source.txt");
if(!read)
{
cout<<"Error"<<endl;
return 0;
}
char line[100], department[100];
int salary;
while(read.getline(line,100,','))
{
read>>salary;
read.ignore();
read.getline(department,100);
cout<<line<<", "<<salary<<", "<<department;
cout<<endl;
}
read.close();
}
else if(choice=='E'||choice=='e')
{
char employee[100], max_employee[100], department[100],
max_department[100];
int salary, max_salary=0;
ifstream read("source.txt");
if(!read)
{
cout<<"Error"<<endl;
return 0;
}
while(read.getline(employee,100,','))
{
read>>salary;
read.ignore();
read.getline(department,100);
if(salary > max_salary)
{
max_salary = salary;
int s = 0;
while(employee[s] != '\0')
{
max_employee[s] = employee[s];
s++;
}
max_employee[s] = '\0';
int k = 0;
while(department[k] != '\0')
{
max_department[k] = department[k];
k++;
}
max_department[k] = '\0';
}
}
read.close();
cout<<max_employee<<", "<<max_salary<<", "<<max_department<<endl;
}
if(choice=='d'||choice=='D')
{
cout<<"Enter the departments thats to be found"<<endl;
cin.ignore();
char search_department[100];
cin.getline(search_department,100);
ifstream read("source.txt");
if(!read)
{
cout<<"Error"<<endl;
}
char name[100],department[100];
int salary;
bool found=false;
while(read.getline(name,100,','))
{
read>>salary;
read.ignore();
read.getline(department,100);
int i=0,j=0;
while(department[i]==' ')
{
i++;
}
while(search_department[j]==' ')
{
j++;
}
bool match=true;
for(; department[i]!='\0'&&search_department[j]!='\0'; i++, j++)
{
if(search_department[j]!=department[i])
{
match=false;
break;
}
}
if(match)
{
cout<<name<<", "<<salary<<", "<<department<<endl;
found=true;
}
if(!found)
{
cout<<"No match found";
}
}
read.close();
}
else if(choice=='r'||choice=='R')
{
cout<<"Enter the name of the employee who is leaving: ";
cin.ignore();
char leaving_employee[100];
cin.getline(leaving_employee,100);
ifstream read("source.txt");
ofstream write("result.txt");
if (!read)
{
cout<<"Error"<<endl;
return 1;
}
char name[100], department[100];
int salary;
while(read.getline(name,100,','))
{
read>>salary;
read.ignore();
read.getline(department,100);
int i = 0;
while(name[i] == ' ')
{
i++;
}
int j = 0;
while(leaving_employee[j] == ' ')
{
j++;
}
bool match = true;
for(;name[i] != '\0'&&leaving_employee[j]!='\0';i++,j++)
{
if(leaving_employee[j]!=name[i])
{
match=false;
break;
}
}
if(!match)
{
write<<name<<", "<<salary<<", "<<department<<endl;
write<<name<<", "<<salary<<", "<<department<<endl;
}
}
read.close();
write.close();
}
cout<<endl;
}while(choice!='x'&&choice!='X');