Question

C++ Implement Random Access Binary Files. Do Not use Arrays instead write the data directly to...

C++ Implement Random Access Binary Files. Do Not use Arrays instead write the data directly to Random Access Binary File. New program should be modified to display a menu for the user to do the following:

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.(C++).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<string>
#include<iostream>
#include<fstream>

using namespace std;

class Departments{

string departmentID,departmentName,departmentHeadName;

public:
Departments(){}

Departments(string ID,string name){
departmentID="";
departmentName="";
departmentHeadName="";
}

Departments(string ID,string name,string head){
departmentID=ID;
departmentName=name;
departmentHeadName=head;
}

void setDepartmentID(string ID){
departmentID=ID;
}

void setDepartmentName(string name){
departmentName=name;
}

void setDepartmentHeadName(string head){
departmentHeadName=head;
}

string getDepartmentID(){
return departmentID;
}

string getDepartmentName(){
return departmentName;
}

string getDepartmentHeadName(){
return departmentHeadName;
}
};

class Employee{

string employeeID, employeeName, employeeDepartmentID;  
   int employeeAge;
    double employeeSalary;

    public:
        Employee(){}
       
        Employee(string ID, string name){
            employeeID=ID;
            employeeName=name;
        }
       
      Employee(string ID, string name,int age){
            employeeID=ID;
            employeeName=name;
            employeeAge=age;
        }
       
        Employee(string ID, string name,int age, double salary){
            employeeID=ID;
            employeeName=name;
            employeeAge=age;
            employeeSalary=salary;
        }

        Employee(string ID, string name,int age, double salary,string department){
            employeeID=ID;
            employeeName=name;
            employeeAge=age;
            employeeSalary=salary;
            employeeDepartmentID=department;
       }
      
        void setEmployeeID(string ID){
            employeeID=ID;
        }
       
        void setEmployeeName(string name){
            employeeName=name;
        }
       
        void setEmployeeAge(int age){
            employeeAge=age;          
       }
      
        void setEmployeeSalary(double salary){
            employeeSalary=salary;
        }
       
        void setEmployeeDepartment(string dept){
            employeeDepartmentID=dept;
       }       

       string getEmployeeID(){
           return employeeID;
       }
      
       string getEmployeeName(){
           return employeeName;
       }      
      
       int getEmployeeAge(){
           return employeeAge;
       }
      
       double getEmployeeSalary(){
           return employeeSalary;
       }      
      
       string getEmployeeDepartment(){
           return employeeDepartmentID;
       }      
};

int main(){
  
   Employee e[7];
   Departments d[3];
   int ch=0, employeeAge, i=0, j=0;
   string departmentID,departmentName,departmentHeadName;
   string employeeID, employeeName, employeeDepartmentID;  
    double employeeSalary;

   do{
       cout<<"\n1. Create Department\n2. Create Employee";
       cout<<"\n3. Write the data to the file\n4. Retrieve data from file";
       cout<<"\n5. Display Report\n6.Exit\n\nEnter your choice : ";
       cin>>ch;
      
       switch(ch){
          
           case 1: if(i<3){
          
                       cout<<"\nEnter the Department Id : ";
                       cin.ignore();
                       getline(cin,departmentID);
                       d[i].setDepartmentID(departmentID);
                      
                       cout<<"\nEnter the Department Name : ";
                       cin.ignore();
                       getline(cin,departmentName);
                       d[i].setDepartmentName(departmentName);
                      
                       cout<<"\nEnter the Department HeadName : ";
                       cin.ignore();
                       getline(cin,departmentHeadName);
                       d[i].setDepartmentHeadName(departmentHeadName);
                       i++;
                   }
                   else
                       cout<<"\nThe array is full, you can not add any more Departments";
                   break;
                  
           case 2: if(j<7){
          
                       cout<<"\nEnter the Employee Id : ";
                       cin.ignore();
                       getline(cin,employeeID);
                       e[j].setEmployeeID(employeeID);
                      
                       cout<<"\nEnter the Employee Name : ";
                       cin.ignore();
                       getline(cin,employeeName);
                       e[j].setEmployeeName(employeeName);
                      
                       cout<<"\nEnter the Employee Department ID : ";
                       cin.ignore();
                       getline(cin,employeeDepartmentID);
                       e[j].setEmployeeDepartment(employeeDepartmentID);
                      
                       cout<<"\nEnter the Employee Age : ";
                       cin>>employeeAge;
                       e[j].setEmployeeAge(employeeAge);
                      
                       cout<<"\nEnter the Employee Salary : ";
                       cin>>employeeSalary;
                       e[j].setEmployeeSalary(employeeSalary);
                      
                       j++;
                   }
                   else
                       cout<<"\nThe array is full, you can not add any more Employees";
                   break;
          
           case 3: {
              
                   ofstream myfile;
                   myfile.open ("example.txt");
                  
                   for(int k=0;k<3;k++){
                      
                       myfile<<" "<<d[k].getDepartmentHeadName();
                       myfile<<" "<<d[k].getDepartmentID();
                       myfile<<" "<<d[k].getDepartmentName()<<"\n";
                   }
                  
                   myfile.close();
                   break;
       }
           case 5: cout<<"\nReport : \n\n";
                   for(int k=0;k<3;k++){
                      
                       cout<<" "<<d[k].getDepartmentHeadName()<<"\n";
                       cout<<" "<<d[k].getDepartmentID()<<"\n";
                       cout<<" "<<d[k].getDepartmentName()<<"\n";
                   }
                   break;

           case 6: exit(1);
                   break;
          
           default : cout<<"\nInvalid option";
                   break;      
       }
   }

while(ch != 6);
return 0;

}

Add a comment
Know the answer?
Add Answer to:
C++ Implement Random Access Binary Files. Do Not use Arrays instead write the data directly to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Implement Random Access Binary Files. Do Not use Arrays instead write the data directly to...

    C++ Implement Random Access Binary Files. Do Not use Arrays instead write the data directly to Random Access Binary File. New program should be modified to display a menu for the user to do the following: 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....

  • In JAVA please! Write program for exercises You will write a Graphical User Interface(GUI) application to manage student...

    In JAVA please! Write program for exercises You will write a Graphical User Interface(GUI) application to manage student information system. The application should allow: 1. Collect student information and store it in a binary data file. Each student is identified by an unique ID. The user should be able to view/edit an existing student. Do not allow student to edit ID. 2. Collect Course information and store it in a separate data file. Each course is identified by an unique...

  • Please solve using c++ plus There are two text files with the following information stored in...

    Please solve using c++ plus There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a C++ program that reads these text files and provides user with the following menu: 1. Enter the instructor ID...

  • Use the Northwind and Hospital data models diagrams to answer the following questions 1.) HOSPITAL: Write...

    Use the Northwind and Hospital data models diagrams to answer the following questions 1.) HOSPITAL: Write a query to list the name and assignment History for employee #12345. The assigmnet history is to include the department name and also the number of months the employee served in that assignment. (Do not worry about the fact that this cannot be calculated for the current assignment, at least with what we know now). For one bonus point describe what logic might allow...

  • The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and...

    The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a...

  • Consider a system where a data files (F_i, and i denotes the file ID) is distributed...

    Consider a system where a data files (F_i, and i denotes the file ID) is distributed over a cloud. A data file is generated by an author (AU_k, and k, denotes the author ID) and stored on a distribution server (DS). Only authorized users (US_1, and denotes the user ID) previously registered on the system using their private keys (KPR_1) are allowed to download the data. Users' public certificates (KPU_1) and revocation lists (CRL) are available on a trusted Certificate...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • C++ Programming

    PROGRAM DESCRIPTIONIn this project, you have to write a C++ program to keep track of grades of students using structures and files.You are provided a data file named student.dat. Open the file to view it. Keep a backup of this file all the time since you will be editing this file in the program and may lose the content.The file has multiple rows—each row represents a student. The data items are in order: last name, first name including any middle...

  • ****THIS IS A 2 PART QUESTION! I ONLY NEED THE ANSWER TO PART 2 ON HOW...

    ****THIS IS A 2 PART QUESTION! I ONLY NEED THE ANSWER TO PART 2 ON HOW TO SEND THE DATA SERIALIZED**** Write a c# program that stores student grades in a text file and read grades from a text file.  The program has the following GUI: There are four buttons: Create File, Save Case, Close File and Load File.  Initially, only the Create File and Load File buttons are enabled.  If the user clicks the Create File button, a Save File Dialog window...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT