1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
//****************************Start of Class declaration*************************
class employee
{
private:
char name[20];
long int empCode;
char designation[20];
float salary;
char email[50];
public:
//Gets data from the user
void getdata()
{
cout << "Enter the name of employee: "; gets(name);
cout << "Enter the email of employee: "; gets(email);
cout << "Enter the designation of employee: "; gets(designation);
cout << "Enter the code of employee: "; cin >> empCode ;
cout << "Enter the salary of employee: "; cin >> salary ;
}
//Shows data of the employee
void showdata()
{
cout << "Name of the employee: " << name << endl ;
cout << "Email of the employee: " << email << endl ;
cout << "Designation of the employee: " << designation << endl ;
cout << "Employee Code : " << empCode << endl ;
cout << "Salary of the employee: " << salary << endl ;
}
int search_for_employee(int choice, char name_temp[20], char email_temp[50] );
void modify_employee();
};
//Searches for employee in file
int employee::search_for_employee( int choice, name_temp[20] , email_temp[50])
{
int return_value = 0;
//Comparison Process starts
switch(choice)
{
case 1: if (strcmp(name_temp,name) == 0)
return_value = 1;
break;
case 2: if ( empCode_temp == empCode)
return_value = 1;
break;
case 3: if (strcmp(email_temp,email) == 0)
return_value = 1;;
break;
default: cout << "Wrong choice entered! Program will now terminate!";
}
return return_value;
}
//Changes value of data in employee
void employee::modify_employee()
{
int choice;
cout<<"Here is the original record :" << endl;
showdata();
cout << endl;
cout<<"Press 1 to change Name " << endl;
cout<<"Press 2 to change Employee code" << endl;
cout<<"Press 3 to change Email ID " << endl;
cout<<"Press 4 to chane salary of employee " << endl;
cout<<"Press 5 to change designation " << endl;
cout<<"Enter your option :";
cin >> choice;
switch(choice)
{
case 1: cout<<"Enter new Name : ";
gets(name);
break;
case 2: cout<<"Enter new Employee code : ";
cin >> empCode;
break;
case 3: cout<<"Enter new Email ID : ";
gets(email);
break;
case 4: cout<<"Enter new Designation: ";
gets(designation);
default : cout<<"Wrong Option !!";
}
}
//*************************End of class declaration****************************
//Search for employee
void employee_search()
{
cout << endl;
employee emp;
char name_temp[20], email_temp[50];
int empCode_temp;
fstream f1;
f1.open("EMPLOYEE.DAT",ios::binary|ios::in);
cout << "Press 1 to search employee by name" << endl;
cout << "Press 2 to search employee by Employee Code" << endl ;
cout << "Press 3 to search employee by email" << endl ;
int choice1; cin >> choice1;
switch(choice1)
{
case 1: cout<<"Enter Name : ";
gets(name_temp);
break;
case 2: cout<<"Enter Employee code : ";
cin >> empCode_temp ;
break;
case 3: cout<<"Enter Email ID : ";
gets(email_temp);
break;
default : cout<<"Wrong Option !!";
}
while( f1.read((char*) &emp, sizeof(emp)) )
{
if(emp.search_for_employee(choice1, name_temp, empCode_temp) == 1)
{
emp.showdata();
}
}
}
//Adds a new employee to a file
void add_employee()
{
employee emp;
fstream f1;
f1.open("EMPLOYEE.DAT",ios::binary|ios::app);
cout<<"\nEnter the details of the employee : \n";
emp.getdata();
f1.write((char*) &emp, sizeof(emp));
f1.close();
}
//Displays list of all employees present in file
void display_all_employee()
{
employee emp;
fstream f1;
int empNum = 1;
f1.open("EMPLOYEE.DAT",ios::binary|ios::in);
while( f1.read((char*) &emp, sizeof(emp)) )
{
cout << endl;
cout << empNum << ". " ;
emp.showdata();
empNum++;
}
f1.close();
}
//Edits employee
void edit_employee()
{
cout << endl;
employee emp;
char name_temp[20], email_temp[50];
int empCode_temp;
int file_modified = 0;
fstream f1,f2;
f1.open("EMPLOYEE.DAT",ios::binary|ios::in);
f2.open("TEMP.DAT", ios::binary|ios::out);
cout << "Press 1 to search employee by name" << endl;
cout << "Press 2 to search employee by Employee Code" << endl ;
cout << "Press 3 to search employee by email" << endl ;
int choice1; cin >> choice1;
switch(choice1)
{
case 1: cout<<"Enter Name : ";
gets(name_temp);
break;
case 2: cout<<"Enter Employee code : ";
cin >> empCode_temp ;
break;
case 3: cout<<"Enter Email ID : ";
gets(email_temp);
break;
default : cout<<"Wrong Option !!";
}
while( f1.read((char*) &emp, sizeof(emp)) )
{
if( emp.search_for_employee(choice1, name_temp, empCode_temp) == 1 )
{
file_modified++ ;
emp.modify_employee();
}
f2.write((char *) &emp, sizeof(emp));
}
if ( file_modified == 0 )
cout << "No records found!";
}
void main()
{
cout << "Welcome to employee management program!" ;
cout << endl;
int choice;
clrscr();
cout<<"Main Menu:\n";
cout<<"Press 1 to add a new employee " << endl;
cout<<"Press 2 to display all employees " << endl;
cout<<"Press 3 to search employee " << endl;
cout<<"Press 4 to modify employee " << endl;
cout<<"Enter your choice : ";
cin >> choice;
switch(choice)
{
case 1: add_employee(); break;
case 2: display_all_employee(); break;
case 3: employee_search(); break;
case 4: edit_employee(); break;
default: cout<<"\nWrong Choice Entered !!Please try again!\n\n";
}
getch();
}
|