Guest User

Untitled

a guest
Feb 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #include<iostream> //pgm on employee using files
  2. #include<string.h>
  3. #include<fstream>
  4. using namespace std;
  5. class employee
  6. {
  7. int no;
  8. char name[20],des[20],dep[20];
  9. float sal;
  10. public:
  11. void input()
  12. {
  13. cout<<"\nEnter employee no., name, designation, department and salary: ";
  14. cin>>no>>name>>des>>dep>>sal;
  15. }
  16. void display()
  17. {
  18. cout<<no<<"\t"<<name<<"\t"<<des<<"\t\t"<<dep<<"\t"<<sal<<"\n";
  19. }
  20. char* getdep()
  21. {
  22. return dep;
  23. }
  24. int getno()
  25. {
  26. return no;
  27. }
  28. char* getdes()
  29. {
  30. return des;
  31. }
  32. void modsal()
  33. {
  34. cout<<"\nEnter salary: ";
  35. cin>>sal;
  36. }
  37. };
  38. int main()
  39. {
  40. int opt, n, i, j, empno, flag=0;
  41. employee e, t[10], temp;
  42. char ch, d[20];
  43. cout<<"\nEnter number of employees: ";
  44. cin>>n;
  45. fstream f;
  46. f.open("Employee.dat",ios::out|ios::binary);
  47. for(i=0;i<n;i++)
  48. {
  49. e.input();
  50. f.write((char*)&e,sizeof(e));
  51. }
  52. f.close();
  53. do
  54. {
  55. cout<<"\n1.Search for employee"
  56. "\n2.display employees in a specific department"
  57. "\n3.Sort employees according to designation"
  58. "\n4.Modify salary of an employee"
  59. "\nEnter choice: ";
  60. cin>>opt;
  61. switch(opt)
  62. {
  63. case 1:
  64. cout<<"\nEnter employee no.: ";
  65. cin>>empno;
  66. f.open("Employee.dat",ios::in|ios::binary);
  67. for(i=0;i<n;i++)
  68. {
  69. f.read((char*)&e,sizeof(e));
  70. if(empno==e.getno())
  71. {
  72. flag=1;
  73. cout<<"\nE.no\tName\tDesignation\tDepartment\tSalary\n";
  74. e.display();
  75. break;
  76. }
  77. }
  78. f.close();
  79. if(flag==0)
  80. cout<<"\nRecord not found!!";
  81. break;
  82. case 2:
  83. cout<<"\nEnter department: ";
  84. cin>>d;
  85. f.open("Employee.dat",ios::in|ios::binary);
  86. for(i=0;i<n;i++)
  87. {
  88. f.read((char*)&e,sizeof(e));
  89. if(strcmp(d,e.getdep())==0)
  90. {
  91. flag=1;
  92. e.display();
  93. }
  94. }
  95. f.close();
  96. if(flag==0)
  97. cout<<"\nRecord not found!!";
  98. break;
  99. case 3:
  100. f.open("Employee.dat",ios::in|ios::binary);
  101. for(i=0;i<n;i++)
  102. {
  103. f.read((char*)&e,sizeof(e));
  104. t[i]=e;
  105. }
  106. for(i=0;i<n;i++)
  107. for(j=0;j<n-1-i;j++)
  108. if(strcmp(t[j].getdes(),t[j+1].getdes())>0)
  109. {
  110. temp=t[j];
  111. t[j]=t[j+1];
  112. t[j+1]=temp;
  113. }
  114. cout<<"\nE.no\tName\tDesignation\tDepartment\tSalary\n";
  115. for(i=0;i<n;i++)
  116. t[i].display();
  117. f.close();
  118. break;
  119. case 4:
  120. cout<<"\nEnter employee no.: ";
  121. cin>>empno;
  122. f.open("Employee.dat",ios::in|ios::out|ios::binary);
  123. for(i=0;i<n;i++)
  124. {
  125. f.read((char*)&e,sizeof(e));
  126. if(empno==e.getno())
  127. {
  128. e.modsal();
  129. f.write((char*)&e,sizeof(e));
  130. cout<<"\nE.no\tName\tDesignation\tDepartment\tSalary\n";
  131. e.display();
  132. break;
  133. }
  134. }
  135. f.close();
  136. break;
  137. default: cout<<"\nInvalid option!";
  138. }
  139. cout<<"\nDo you want to continue?[y/n]: ";
  140. cin>>ch;
  141. }
  142. while(ch=='y');
  143. }
Add Comment
Please, Sign In to add comment