• Source
    1. #include <iostream>
    2. #include <fstream>
    3. #include <cstring>
    4. using namespace std;
    5. char ch, c, cs;
    6. int id;
    7. class docnur //class containing staff's info
    8. {
    9. char name[40];
    10. int age;
    11. char gender[10];
    12. unsigned long long phoneno;
    13. int exp;
    14. char dept[40];
    15. float salary;
    16. char nd;
    17. public:
    18. int dnid;
    19. void dadd();
    20. };
    21. docnur dn;
    22. void docnur::dadd() //funtion to add a staff record
    23. {
    24. cout<<"\nEnter staff ID:"; cin>>dnid;
    25. cin.get(ch);
    26. cout<<"\nEnter name of the staff member:"; cin.getline(name, 40);
    27. cout<<"\nEnter age of the staff member:"; cin>>age;
    28. cin.get(ch);
    29. cout<<"\nGender of staff member:"; cin.getline(gender, 10);
    30. cout<<"\nEnter phone number:"; cin>>phoneno;
    31. cout<<"\nEnter experience:"; cin>>exp;
    32. cin.get(ch);
    33. cout<<"\nEnter department:"; cin.getline(dept, 40);
    34. cout<<"\nEnter salary:"; cin>>salary;
    35. label:
    36. cout<<"\nEnter job: \n\t1. Press 'n' for nusrse. \n\t2. Press 'd' for doctor.";
    37. cin>>ch;
    38. switch (ch)
    39. {
    40. case 'n': nd='n';
    41. break;
    42. case 'd': nd='d';
    43. break;
    44. default:cout<<"\nWrong Choice! \nEnter Again!!";
    45. goto label;
    46. break;
    47. }
    48. ofstream fout;
    49. fout.open("Staff.txt", ios::app);
    50. fout.write((char *)&dn, sizeof(dn));
    51. fout.close();
    52. cout<<"\nRecord added to file.";
    53. cout<<"\nWant to add more staff records?";
    54. cin>>c;
    55. cin.get(ch);
    56. if (c=='y'||c=='Y')
    57. dn.dadd();
    58. }
    59. void docnur::dnview()
    60. {
    61. char f='f';
    62. cout<<"\n\nEnter ID of the staff member to be viewed:"; cin>>id;
    63. ifstream fin;
    64. fin.open("Staff.txt", ios::in);
    65. if (fin.fail())
    66. {
    67. cout<<"\n";
    68. for (int x=0;x<40;x++)
    69. cout<<"-";
    70. cout<<"\n";
    71. for (int x=0;x<10;x++)
    72. cout<<"-";
    73. cout<<"File Empty";
    74. for (int x=0;x<10;x++)
    75. cout<<"-";
    76. return;
    77. }
    78. while (!fin.eof())
    79. {
    80. fin.read((char *)&dn, sizeof(dn));
    81. if (id==dnid)
    82. {
    83. f='t';
    84. cout<<"\nStaff member ID:"<<dnid;
    85. cout<<"\nName: ";
    86. if ('d'==nd) cout<<"Dr. "<<name;
    87. if ('n'==nd) cout<<"Nurse "<<name;
    88. cout<<"\nAge:"<<age;
    89. cout<<"\nGender:"<<gender;
    90. cout<<"\nPhone number:"<<phoneno;
    91. cout<<"\nExperience:"<<exp;
    92. cout<<"\nSalary per month:"<<salary;
    93. cout<<"\nDepartment in which admitted:"<<dept;
    94. fin.close();
    95. }
    96. if (f=='f')
    97. {
    98. cout<<"\n";
    99. for (int A=0; A<30; A++)
    100. cout<<"-";
    101. cout<<"ID not found!!";
    102. for (int A=0; A<30; A++)
    103. cout<<"-";
    104. }
    105. }