6. cout << "nnn";
cout << "1-Create Departmentn";
cout << "2-Creat employeen";
cout << "3-Write the data to the filen";
cout << "4-Retrive the data from the filen";
cout << "5-Display Reportn";
cout << "6-Exitn n n";
}
// Choice function
void choices()
{
// Creating array objects
Department dep[3];
Employee emp[7];
string depid, empid, empdepid, depname, dephead, empname,
depnamefile, depdeadfile;
int empage;
double empsalary;
int choice;
ifstream file_in, file_in2;
ofstream file_out, file_out2;
char again; //Used to loop the entire program
int counter, empcounter, depcounter;
bool id3 = false;
int a; //used for getting age from file
double s; //used for getting salarry from file
string info; // getting lines from file
size_t pos; //position
double sal1, sal2, sal3; //Salaries for calculation
do {
cout << "Please enter a number from the menu:";
cin >> choice;
7. switch (choice)
{
case 1:
cout << "Please enter Department ID:";
cin >> depid;
//Loop to not get a duplicate
for (int i = 0; i < 3; i++)
{
while (dep[i].getDepId() == depid)
{
cout << "The Department ID that you entered exist. Try again:";
cin >> depid;
}
}
//Condition to make sure not over arrays
if (depcounter < 3)
{
cout << "Please enter the department name:";
cin >> depname;
cout << "Please enter the department head:";
cin >> dephead;
dep[depcounter].setDepId(depid);
dep[depcounter].setDepName(depname);
dep[depcounter].setDepHead(dephead);
depcounter++;
}
else
cout << "Array is full.n";
8. ShowMenu();
break;
case 2:
cout << "Please enter the employee ID:";
cin >> empid;
//Loop to not get duplicate
for (int i = 0; i < 7; i++)
{
while (emp[i].getEmpID() == empid)
{
cout << "The Employee ID that you entered exist. Try again:";
cin >> empid;
}
}
//condition to not go over arrays sizes
if (empcounter < 7) {
cout << "Please enter the employee department ID:";
cin >> empdepid;
for (int i = 0; i < 7; i++)
{
if (dep[i].getDepId() == empdepid)
{
id3 = true;
break;
}
}
if (id3)
9. {
cout << "Please enter the employee Name:";
cin >> empname;
cout << "Please enter the employee Age:";
cin >> empage;
cout << "Please enter the employee salary:";
cin >> empsalary;
emp[empcounter].setEmpID(empid);
emp[empcounter].setEmpName(empname);
emp[empcounter].setEmpDepID(empdepid);
emp[empcounter].setEmpAge(empage);
emp[empcounter].setEmpSalary(empsalary);
empcounter++;
}
else
cout << "Department not foundn";
}
else
cout << "Array is full.n";
ShowMenu();
break;
case 3:
//condition to mmake sure objects are compelted, the
write to file
if (depcounter == 3 && empcounter == 7)
{
file_out.open("dep.txt");
for (int counter = 0; counter < 3; counter++)
10. {
file_out << "Department ID: " << dep[counter].getDepId() <<
endl;
file_out << "Department Name: " <<
dep[counter].getDepartmentName() << endl;
file_out << "Department Head Name: " <<
dep[counter].getDepHeadName() << endl;
file_out << endl << endl;
}
file_out.close();
file_out2.open("emp.txt");
for (int counter = 0; counter < 7; counter++)
{
file_out2 << "Employee ID: " << emp[counter].getEmpID() <<
endl;
file_out2 << "Employee Name: " <<
emp[counter].getEmpName() << endl;
file_out2 << "Employee Age: " << emp[counter].getEmpAge()
<< endl;
file_out2 << "Employee Salary: " << emp[counter].getEmpSal()
<< endl;
file_out2 << "Employee Department ID: " <<
emp[counter].getEmpDepID() << endl;
file_out2 << endl << endl;
}
//Making sure user doesn't close the program without
saving data
cout << "File not saved. Do you want to save the file? (Y/N)n";
cin >> again;
11. if (toupper(again) == 'Y')
file_out.close();
file_out2.close();
}
else
cout << "Arrays not complete. Can't write into files.n" << endl
<< endl;
ShowMenu();
break;
case 4:
file_in.open("dep.txt");
if (file_in)
{
int counter;
// getting department data from file
while (getline(file_in, info) && counter < 3)
{
pos = info.find(": ");
dep[counter].setDepId(info.substr(pos + 2));
getline(file_in, info);
pos = info.find(": ");
dep[counter].setDepName(info.substr(pos + 2));
getline(file_in, info);
pos = info.find(": ");
dep[counter].setDepHead(info.substr(pos + 2));
getline(file_in, info);
getline(file_in, info);
counter++;
}
14. ShowMenu();
break;
case 6:
cout << "Exiting the program. Thank you." << endl;
break;
default:
cout << "Invalid selection. Please Try again" << endl;
ShowMenu();
break;
}
} while (choice != 6);
}
The purpose of this project is to take your Midterm project and
implement it using Random Access Binary Files.
As you recall, the Midterm project used text files to store the
data. Here in the final exam project,
you will be storing the data in Random Access Binary File.
Also, in the Midterm project you used Arrays to temporarily
hold the data in the memory until the user
decides to write the data to file. Here you will not be using
Arrays and instead writing the
15. data directly to Random Access Binary File. Please read the
chapter Advance File and I/O operations
before attempting this.
Here is the full description of the Final exam project.
Modify your Midterm Exam Project to:
1. Replace Employee and Department classes with Employee
and Department Structures.
2. Inside each structure, replace all string variables with array
of characters.
3. Make Employee and Department editable. That means, the
user should be able to edit a given Employee and Department.
4. Do not allow the user to edit the Employee ID and
Department ID.
5. Use Random Access Files to store the data in Binary Form.
This means, you should not use an Arrays to
store the data in the memory. Instead, when the user wants to
create a new Employee/Department,
you write it to the file right away. Also when the user says
he/she wants to edit
an Employee/Department, ask the user to enter
EmployeeID/DepartmentID.
Read the data from the file and display it to the user.
Allow the user to enter new data and write it back to the file in
the same position inside the file.
Please read the chapter . Advance File/IO operations which has
examples on how to do this.