c++ code to remove errors in 8 hours

profilesharp122
codec_1.zip

department.h

#ifndef DEPARTMENT_H #define DEPARTMENT_H #include <string> using namespace std; class Department{ private: //Attributes int departmentID; string departmentName; string departmentHeadName; public: Department() {}; //Default constructor //Constructor with arguments Department(int dID, string Name, string HeadName){ departmentID=dID; departmentName=Name; departmentHeadName=HeadName; } //Accesors int getDepartmentID() {return departmentID;} string getDepartmentName() {return departmentName;} string getDepartmentHeadName() {return departmentHeadName;} //Setters void setDepartmentID(int dID) {departmentID = dID;} void setDepartmentName(string dName) {departmentName = dName;} void setDepartmentHeadName(string hName) {departmentHeadName = hName;} }; #endif

employee.h

#ifndef EMPLOYEE_H #define EMPLOYEE_H #include <string> using namespace std; class Employee{ private: //Attributes int employeeID; string employeeName; double employeeSalary; int employeeAge; int employeeDepartmentID; public: Employee() {} //Default constructor //Constructor Employee(int eID, string eName, double S, int A, int dID) { employeeID = eID; employeeName= eName; employeeSalary=S; employeeAge=A; employeeDepartmentID= dID; } //Acessors int getEmployeeID() {return employeeID;} string getEmployeeName() {return employeeName;} double getEmployeeSalary() {return employeeSalary;} int getEmployeeAge() {return employeeAge;} int getEmployeeDepartmentID() {return employeeDepartmentID;} //Setters void setEmployeeID(int eID) {employeeID = eID;} void setEmployeeName(string eName) {employeeName = eName;} void setEmployeeSalary(double eSalary) {employeeSalary = eSalary;} void setEmployeeAge(int eAge) {employeeAge = eAge;} void setEmployeeDepartmentID(int dID) {employeeDepartmentID = dID;} }; #endif

main.cpp

main.cpp


#include   "employee.h"
#include   "department.h"

#include   < iostream >
#include   < fstream >
#include   < string >
#include   < stdio . h >

using   namespace  std ;

//Defining global values to use during the code for simplification
#define  SIZE_ARRAY_DEPARTMENTS  3
#define  SIZE_ARRAY_EMPLOYEES  7
#define  CREATE_DEPARTMENT  1
#define  CREATE_EMPLOYEE  2
#define  SAVE_TO_FILE  3
#define  READ_FROM_FILE  4
#define  DISPLAY_REPORT  5
#define  EXIT  6

//Every function is getting the array by reference instead of by copy so that it changes the original instead of creating a copy

//Will get everything from the array that I am passing to the function (departmentArray) and write it to the file
void  saveToFile ( Department   ( & departmentArray )[ SIZE_ARRAY_DEPARTMENTS ],   Employee   ( & employeeArray )[ SIZE_ARRAY_EMPLOYEES ],  string filePath ,   int  numOfDepartments ,   int  numOfEmployees ){
  ofstream f ;    //Output stream
  f . open ( filePath . c_str ());   //Open file indicated by string filePath

   if   ( ! f . is_open ()){   //Error opening file
    cout  <<   "Error opening the file\n\n" ;
     return ;
   }

   //Check if both arrays a full. If they are not we are not allowed to print it
   if ( numOfDepartments  <  SIZE_ARRAY_DEPARTMENTS  ||  numOfEmployees  <  SIZE_ARRAY_EMPLOYEES ){
    cout  <<   "One of the arrays is not full yet. Data not saved to file!\n\n" ;
     return ;
   }


   //First I am printing every department, one per line. Then I am printing every employee, one per line

   //Goes through every department and prints each attribute of current department separated by a space
   for ( int  i  =   0 ;  i  <  SIZE_ARRAY_DEPARTMENTS ;  i ++ ){
    f  <<  departmentArray [ i ]. getDepartmentID ()   <<   " "   <<  departmentArray [ i ]. getDepartmentName ()   <<   " "   <<  departmentArray [ i ]. getDepartmentHeadName ()   <<  endl ;
   }

   //Goes through every employee and prints each attribute of current employee separated by a space
   for ( int  i  =   0 ;  i  <  SIZE_ARRAY_EMPLOYEES ;  i ++ ){
    f  <<  employeeArray [ i ]. getEmployeeID ()   <<   " "   <<  employeeArray [ i ]. getEmployeeName ()   <<   " "   <<  employeeArray [ i ]. getEmployeeSalary ()   <<   " "   <<  employeeArray [ i ]. getEmployeeAge ()   <<   " "   <<  employeeArray [ i ]. getEmployeeDepartmentID ()   <<  endl ;
   }

   cout  <<   "Departments and employees saved to file!\n\n" ;
  f . close ();   //Closing the file
}

void  loadFromFile ( Department   ( & departmentArray )[ SIZE_ARRAY_DEPARTMENTS ],   Employee   ( & employeeArray )[ SIZE_ARRAY_EMPLOYEES ],  string filePath ,   int   &  numOfDepartments ,   int   &  numOfEmployees ){
  ifstream f ;   //Input stream
  f . open ( filePath . c_str ());   //Open file indicated by string filePath

   if   ( ! f . is_open ()){   //Error opening file
    cout  <<   "Error opening the file"   <<  endl ;
     return ;
   }

   //Variables to store data we are taking from file
   int  dID  =   0 ;
  string dName  =   "" ;
  string dHeadName  =   "" ;
   //Goes through every department and prints each attribute of current department separated by a space
   for ( int  i  =   0 ;  i  <  SIZE_ARRAY_DEPARTMENTS ;  i ++ ){
      //Take data from f (File stream) and store them in the variables
     f  >>  dID ;
     f  >>  dName ;
     f  >>  dHeadName ;
     f . ignore ();   //Ignore new line

       //Take data from the variables and change each member variable to be the variables we populated from the file
     departmentArray [ i ]. setDepartmentID ( dID );
     departmentArray [ i ]. setDepartmentName ( dName );
     departmentArray [ i ]. setDepartmentHeadName ( dHeadName );

     numOfDepartments ++ ;
   }

   //Variables to store data we are taking from file
   int  eID  =   0 ;
  string eName  =   "" ;
   double  eSalary  =   0 ;
   int  eAge  =   0 ;
   int  eDepartmentID  =   0 ;
   //Goes through every employee and prints each attribute of current employee separated by a space
   for ( int  i  =   0 ;  i  <  SIZE_ARRAY_EMPLOYEES ;  i ++ ){
     //Take data from f (File stream) and store them in the variables
    f  >>  eID ;
    f  >>  eName ;
    f  >>  eSalary ;
    f  >>  eAge ;
    f  >>  eDepartmentID ;

     //Take data from the variables and change each member variable to be the variables we populated from the file
    employeeArray [ i ]. setEmployeeID ( eID );
    employeeArray [ i ]. setEmployeeName ( eName );
    employeeArray [ i ]. setEmployeeSalary ( eSalary );
    employeeArray [ i ]. setEmployeeAge ( eAge );
    employeeArray [ i ]. setEmployeeDepartmentID ( eDepartmentID );

    numOfEmployees ++ ;
   }

  cout  <<   "Departments and employees loaded from file!\n\n" ;

  f . close ();
}

//Print choices to user and get choice user selected
int  getChoiceFromUser (){

  cout <<   "++++Type choice number and press ENTER++++\n1)Create department\t2)Create employee\n3)Save to file\t4)Retrieve from file\n5)Display Report\t6)Exit\nChoice: " ;

   int  choice  =   0 ;

   //Get a choice from the user. If choice is not 1-6, ask for another coice
   while ( choice  <   1   ||  choice  >   6 ){
    cin >> choice ;

     if ( choice  <   1   ||  choice  >   6 )   //invalid choice
      cout  <<   "Invalid choice, please pick new choice!\nChoice: " ;
   }

  cout  <<  endl ;
   return  choice ;
}

void  addDepartment ( Department   ( & departmentArray )[ SIZE_ARRAY_DEPARTMENTS ],   int   &  departmentsEntered )   {

   //First check if array of departments is full
   if ( departmentsEntered  >=  SIZE_ARRAY_DEPARTMENTS ){
    cout  <<   "The array is full, you can not add any more departments!\n\n" ;
     return ;   //Exit the function
   }

   //Get from user data to fill the

   //Variables to hold the data we are getting from user
   int  dID  =   0 ;
  string dName  =   "" ;
  string dHeadName  =   "" ;

   //Take data from command line and store them in the variables
  cout  <<   "Department ID: " ;
  cin  >>  dID ;
  cout  <<   "Department Name: " ;
  cin  >>  dName ;
  cout  <<   "Department Head Name: " ;
  cin  >>  dHeadName ;

   //Check if department already exists by ID
   for ( int  i  =   0 ;  i  <  departmentsEntered ;  i ++ ){
     if ( departmentArray [ i ]. getDepartmentID ()   ==  dID ){
        cout  <<   "Department with this ID already exists. Department not added! \n\n" ;
         return ;   //Exit the function if we are entering a department that already exists
     }
   }

   //Take data from the variables and change each member variable to be the variables we populated from user
  departmentArray [ departmentsEntered ]. setDepartmentID ( dID );
  departmentArray [ departmentsEntered ]. setDepartmentName ( dName );
  departmentArray [ departmentsEntered ]. setDepartmentHeadName ( dHeadName );

  departmentsEntered ++ ;   //We added a new department, so update variable we were using to keep track of how many departments were entered so far

  cout  <<   "Department entered\n\n" ;
}

void  addEmployee ( Employee   ( & employeeArray )[ SIZE_ARRAY_EMPLOYEES ],   int   &  employeesEntered ,   Department   ( & departmentArray )[ SIZE_ARRAY_DEPARTMENTS ],   int   &  departmentsEntered )   {

   //First check if array of employees is full
   if ( employeesEntered  >=  SIZE_ARRAY_EMPLOYEES ){
    cout  <<   "The array is full, you can not add any more Employees!\n\n" ;
     return ;   //Exit the function
   }

   //Create variables to store data we are getting from user
   int  eID  =   0 ;
  string eName  =   "" ;
   double  eSalary  =   0 ;
   int  eAge  =   0 ;
   int  eDepartmentID  =   0 ;

   //Take data from command line and store in variables
  cout <<   "Employee ID: " ;
  cin  >>  eID ;
  cout <<   "Employee Name: " ;
  cin  >>  eName ;
  cout <<   "Employee Salary: " ;
  cin  >>  eSalary ;
  cout <<   "Employee Age: " ;
  cin  >>  eAge ;
  cout <<   "Employee Department ID: " ;
  cin  >>  eDepartmentID ;

   //Check if employee already exists by ID
   for ( int  i  =   0 ;  i  <  employeesEntered ;  i ++ ){
     if ( employeeArray [ i ]. getEmployeeID ()   ==  eID ){
        cout  <<   "Employee with this ID already exists. Employee not added!\n\n" ;
         return ;   //Exit the function if we are entering a department that already exists
     }
   }

   //Go through every deparment and check if the department ID exists
   bool  departmentExists  =   false ;
   for ( int  j  =   0 ;  j  <  departmentsEntered ;  j ++ ){
     if ( departmentArray [ j ]. getDepartmentID ()   ==  eDepartmentID ){
      departmentExists  =   true ;
       break ;
     }
   }

   //Department does not exist, leave
   if ( departmentExists  ==   false ){
      cout  <<   "Department ID does not exist. Employee not added!\n\n" ;
       return ;
   }

   //Take data from the variables and change each member variable to be the variables we populated from the file
  employeeArray [ employeesEntered ]. setEmployeeID ( eID );
  employeeArray [ employeesEntered ]. setEmployeeName ( eName );
  employeeArray [ employeesEntered ]. setEmployeeSalary ( eSalary );
  employeeArray [ employeesEntered ]. setEmployeeAge ( eAge );
  employeeArray [ employeesEntered ]. setEmployeeDepartmentID ( eDepartmentID );

  employeesEntered ++ ;   //Succeed at entering new employee, update total number of employees entered so far

  cout  <<   "Employee entered\n\n" ;

}

void  displayReport ( Department   ( & departmentArray )[ SIZE_ARRAY_DEPARTMENTS ],   int   &  departmentsEntered ,   Employee   ( & employeeArray )[ SIZE_ARRAY_EMPLOYEES ],   int   &  employeesEntered )   {

     //For each department
     for ( int  d  =   0 ;  d  <  departmentsEntered ;  d ++ ){
       //Go though each employee
       double  sumOfSalaries  =   0 ;
       for ( int  e  =   0 ;  e  <  employeesEntered ;  e ++ ){
         if ( employeeArray [ e ]. getEmployeeDepartmentID ()   ==  departmentArray [ d ]. getDepartmentID ())
          sumOfSalaries  +=  employeeArray [ e ]. getEmployeeSalary ();
       }

      cout  <<   "Salary of department ( ID | NAME | TOTAL SALARY): "   <<  departmentArray [ d ]. getDepartmentID ()   +   " | "   +  departmentArray [ d ]. getDepartmentName ()   <<   " | "   <<  sumOfSalaries  <<  endl ;
     }

    cout  <<  endl ;
}

//My main function
int  main ( int  argc ,   char   * argv [])
{
   //Create the array of departments with size 3
   Department  arrayOfDepartments  [ SIZE_ARRAY_DEPARTMENTS ];   //Array will have SIZE_ARRAY_DEPARTMENTS objetcs that were created from default constructor
   int  numOfDepartments  =   0 ;

   //Create the array of employees with size 7
   Employee  arrayOfEmployees  [ SIZE_ARRAY_EMPLOYEES ];   //Array will have SIZE_ARRAY_EMPLOYEES objetcs that were created from default constructor
   int  numOfEmployees  =   0 ;

   //Get initial choice  from user
   int  userChoice  =  getChoiceFromUser ();

   //While loop that runs until user enters 6 (EXIT)
   //Inside of each else if I will call the function that does what the user wants based on his choice
   while ( userChoice  !=  EXIT ){
     if ( userChoice  ==  CREATE_DEPARTMENT )  addDepartment ( arrayOfDepartments ,  numOfDepartments );
     else   if ( userChoice  ==  CREATE_EMPLOYEE )  addEmployee ( arrayOfEmployees ,  numOfEmployees ,  arrayOfDepartments ,  numOfDepartments );
     else   if ( userChoice  ==  SAVE_TO_FILE )  saveToFile ( arrayOfDepartments ,  arrayOfEmployees ,   "data.txt" ,  numOfDepartments ,  numOfEmployees );
     else   if ( userChoice  ==  READ_FROM_FILE )  loadFromFile ( arrayOfDepartments ,  arrayOfEmployees ,   "data.txt" ,  numOfDepartments ,  numOfEmployees );
     else   if ( userChoice  ==  DISPLAY_REPORT )  displayReport ( arrayOfDepartments ,  numOfDepartments ,  arrayOfEmployees ,  numOfEmployees );

    userChoice  =  getChoiceFromUser ();
   }

  cout  <<   "Exiting from program\n\n" ;
}