#include <iostream> // input and output code
#include<conio.h> // getch - passwd
#include<cstdio> // fileopen fscanf fileclose
#include<windows.h> // system cls and pause
using namespace std;
///////////////////////////////////////////////////CLASS EMPLOYEE /////////////////////////////////////////////////////////
class admin{
private:// variables for admin details
char name[30],id[5],position[20];
int age,Salary,experience;
//quit functions
void quit(void){
cout<<"\n\n\n Please ENTER To Go Back MAIN PAGE \n\n";
cin.get(); // get update the information key infrom user before quit
cin.get(); // get update the information key in from user before quit
}
void listEmployees(void){ //To list total employees with Name, ID and Position
system("cls"); //clear the screen
FILE *file; // to open a existed file or create a file if doesn't exist
file= fopen("data.txt", "r"); // opened file with data.txt and only able to read the data
cout<<"\n\t List of Employees of RSVV SDN.BHD\n";
cout<<"\n========================================================";
cout<<"\n= Name || Employee ID || Position =";
cout<<"\n========================================================";
//while ((fscan(file, "%[^\n]",line))!= EOF) %s//string %d//integers or numbers
// it will scan the data in the file and update the data inside the dta.txt before void quit
while(fscanf(file, "%s %s %s %d %d %d", &name[0], &id[0] , &position[0], &age, &Salary, &experience)!= EOF)
cout<<"\n"<<name<<"\t\t\t"<<id<<"\t\t\t"<<position; // update the name,id and position in the table
fclose(file); //close the created file
quit(); //calling the void quit
}
// functions to call the updated employee list
void duplistEmployees(void){ // this void to showcase the the list before edit,delete menu
system("cls"); //clear the screen
FILE *file; // to open a existed file or create a file if doesn't exist
file= fopen("data.txt", "r"); // opened file with data.txt and only able to read the data
cout<<"\n\t List of Employees in RSVV SDN.BHD\n";
cout<<"\n========================================================";
cout<<"\n= Name || Employee ID || Position =";
cout<<"\n========================================================";
//while ((fscan(file, "%[^\n]",line))!= EOF) %s//string %d//integers or numbers
// it will scan the data in the file and update the data inside the dta.txt before void quit
while(fscanf(file, "%s %s %s %d %d %d", &name[0], &id[0] , &position[0], &age, &Salary, &experience)!= EOF)
cout<<"\n"<<name<<"\t\t\t"<<id<<"\t\t\t"<<position; // update the name,id and position in the table
fclose(file); //close the created file
}
void showDetails(void){ //Displays all details according to Employee's id
duplistEmployees(); // calling the updated employee list
char checkId[5]; // declare to check if got any exist id
cout<<"\n\nPlease Enter the Employee ID: ";
cin>>checkId;
FILE *file;
file= fopen("data.txt", "r"); // open the file and read the file data
//while ((fscan(file, "%[^\n]",line))!= EOF) %s//string %d//integers or numbers
while(fscanf(file, "%s %s %s %d %d %d", &name[0], &id[0] , &position[0], &age, &Salary, &experience)!=EOF)
if(strcmp(checkId,id)==0){ //checking the inputted variable with the existing variable is yes so the permission access given
cout<<"\n---------------------";
cout<<"\nEmployee Name: "<<name;
cout<<"\n---------------------";
cout<<"\nEmployee Id: "<<id;
cout<<"\n---------------------";
cout<<"\nEmployee Position: "<<position;
cout<<"\n---------------------";
cout<<"\nEmployee Age: "<<age;
cout<<"\n---------------------";
cout<<"\nEmployee Salary: "<<Salary;
cout<<"\n---------------------";
cout<<"\nEmployee Experience: "<<experience;
cout<<"\n---------------------";
}
fclose(file); //close the opened file
quit(); //calling the quit function
}
void editExisting(void){ //edit the existing employee list
duplistEmployees(); //calling out updated employee list
char checkId[5];
cout<<"\nPlease Enter Employee ID To Edit: ";
cin>>checkId;
char newPosition[10];
cout<<"\n-----------------------------";
cout<<"\nPlease Enter The New Position Of The Employee: ";
cin>>newPosition; // key in newposition of the employee
int newSalary;
cout<<"------------------------------";
cout<<"\nPlease Enter The New Salary Of The Employee: ";
cin>>newSalary; //key in the newSalary of the employee
FILE *file, *tempfile; // to open a existed file or create a file if doesn't exist // create a temp file as well
file= fopen("data.txt", "r"); // file manipulation method
tempfile= fopen("temp.txt", "w"); //using temp file manipulation method
//while ((fscan(file, "%[^\n]",line))!= EOF) %s//string %d//integers or numbers
while(fscanf(file, "%s %s %s %d %d %d", &name[0], &id[0] , &position[0], &age, &Salary, &experience)!=EOF){
if(strcmp(checkId, id)==0)
fprintf(tempfile, "%s %s %s %d %d %d \n", name, id, newPosition, age, newSalary, experience );
else
fprintf(tempfile, "%s %s %s %d %d %d \n", name, id, position, age, Salary, experience );
}
fclose(file);
fclose(tempfile);
int isRemoved= remove("data.txt"); // remove the content of the data.txt
int isRenamed= rename("temp.txt", "data.txt"); //change the temp file data into data.txt file.
quit();
}
void addNewEmployee(void){ //adding records
system("cls"); //clear the screen
duplistEmployees(); // calling out the updated employee list
cout<<"\n----------------------------------------";
cout<<"\n PLease Enter The Name of New Joined Employee: ";
cin>>name;
cout<<"\n----------------------------------------";
cout<<"\n Please Enter The New Joined Employee ID [MAX 4 digits]: ";
cin>>id;
cout<<"\n----------------------------------------";
cout<<"\n Please Enter The New Joined Employee Position: ";
cin>>position;
cout<<"\n----------------------------------------";
cout<<"\n Please Enter The New Joined Employee Age: ";
cin>>age;
cout<<"\n----------------------------------------";
cout<<"\n Please Enter The New Joined Employee Salary: ";
cin>>Salary;
cout<<"\n----------------------------------------";
cout<<"\n Please Enter The New Joined Employee Experience (IF NO EXPERIENCE PUT '0'): ";
cin>>experience;
cout<<"\n----------------------------------------";
char ch;
cout<<"\nPlease Enter 's' TO Save The Employee's Information\n";
cin>>ch;
if(ch=='s'||ch=='S'){
FILE *file; // to open a existed file or create a file if doesn't exist
file= fopen("data.txt","a"); //open a data.txt file without changing the content
fprintf(file, "%s %s %s %d %d %d \n", name, id, position, age, Salary, experience );
fclose(file);
cout<<"\nA New Employee Has Been Added To The System\n";
}
else
addNewEmployee();
quit();
}
void deleteEmployeeDetails(void){ //removing records
duplistEmployees();
char checkId[5];
cout<<"\n----------------------------------";
cout<<"\nPlease Enter The Employee Id: ";
cin>>checkId;
char ch;
cout<<"----------------------------------";
cout<<"\n\n\n\n\n\n Do you want to remove this employee?";
cout<<"\n\n\n\n\nCONFIRMATION!!!\nEnter 'r' To Confirm Deletion \n";
cin>>ch;
if(ch=='r'||ch=='R'){
FILE *file, *tempfile; // to open a existed file or create a file if doesn't exist // create a temp file as well
file= fopen("data.txt", "r");
tempfile= fopen("temp.txt", "w");
//while ((fscan(file, "%[^\n]",line))!= EOF) %s//string %d//integers or numbers
// it will scan the data in the file and update the data inside the dta.txt before void quit
while(fscanf(file, "%s %s %s %d %d %d", &name[0], &id[0] , &position[0], &age, &Salary, &experience)!=EOF)
if(strcmp(checkId, id)!=0)
fprintf(tempfile, "%s %s %s %d %d %d \n", name, id, position, age, Salary, experience );
fclose(file); //close the create file
fclose(tempfile); //close the create temo file
int isRemoved= remove("data.txt"); //remove the data file
int isRenamed= rename("temp.txt", "data.txt"); //change temp.txt as the data.txt
cout<<"\nRemoved Successfully\n";
quit();
}
else
deleteEmployeeDetails();
}
Vinc_1 0 Newbie Poster
rproffitt 2,662 "Nothing to see here." Moderator
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.