Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to change this code from STRUCTS to CLASSES, the members have to be

ID: 3779916 • Letter: I

Question

I am trying to change this code from STRUCTS to CLASSES, the members have to be private. Well, I think I did a semi-ok job; the code doesn't run and I have no idea why. Can you please help. Platform: C++

========== C++ CODE ============


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib> //must have to use system ("pause" );

//#include "personType.h"

using namespace std;

const int MAX_EMPLOYEES = 50;
//----------------------------------

class employeeType{ //:public personType
private:
    long empID;
    string first;
    string last;
    char gender;
    double payrate;
    string jobRole;
    int years;

public:
     virtual void programmer_info() const=0;
    //Function to output employee's data
    virtual double cutBacks(employeeType let[], int listsize) const=0;
//Function to calculate and return the wages.
//Postcondition: Pay is calculated and returned
    void yourFired(employeeType let[], int& listsize , long id); //int& cuz we are restando cantidades de la lista.
//Function to set the salary. /Postcondition: personId = id
    long seqSearch(employeeType let[], int listLength, int searchItem)const;
//Function to retrieve the id. /Postcondition: returns personID
    employeeType (long id = 0, string first = "", string last = "", char gender = "",
                  double payrate = 0, string jobRole = "",int years = 0);
//Ibefore it was: userinput();
//Constructor with parameters //Sets the first name, last name, payRate, and
//hoursWorked according to the parameters. If no value is specified, the default
//values are assumed. //Postcondition: firstName = first;

///==============================================================
    void getData(ifstream& inFile, class employeeType let[], int& listSize);
    void printOne ( employeeType one);
    void hireOne(employeeType let[], int& listsize); //int& cuz we are adding or restyando cantidades de la lista.
    void selectionSort( employeeType let[], int length);
    void printList(employeeType let[], int listSize);
    employeeType getOne ( ifstream& dataIn );
};

///===============================================================
void employeeType::yourFired(long id)
{
    empID = id;
}
long employeeType::seqSearch() const
{
    return empID;
}
employeeType::employeeType(long id, string first, string last, char gender,
                           double, string jobRole,int years)
             : personType(first, last)
{
    empID = id;
}

//----------------------------------

int main ()
{
    int number;      // number of employees in the file
    int id;
    char choice;
    class employeeType [MAX_EMPLOYEES], newrecord;
    ifstream dataFile;

      dataFile.open ( "newEmployees.txt");
      if (!dataFile){
         cout << " Error with input file!! ";
         //system ("pause");        // must #include<cstdlib>
         return 1;
      }

      getData (dataFile, employeeType, number);
      cout <<endl<< "There were " << number << " employees input. ";
      printList( employeeType, number) ;

/// TASK 2 =======================================================
    newrecord = userinput();

   cout << " The new record details "<<endl;
   printOne(newrecord); //call pirntOne to print 'employeeType userinput()'
   system ("pause" ); // #include <cstdlib> to be used


/// TASK 3 =======================================================

for(int i =0; i<3 ; i++){ //calling hireOne 3 times, you can insert 3 new employees on a roll

     hireOne(employeeType, number); // the list increases by up to 3 new records each time

}

cout << "After hiring employees, new list: "<<endl;
printList(employeeType, number);
system ("pause" );

/// TASK 4: Use selection sort to sort the array in ascending order by pay rate. Print out the sorted array.

    selectionSort( employeeType, number);

cout << " The list after sorting :" << endl;
printList(employeeType, number);
system ("pause" );

/// TASK 5 =======================================================

cutBacks(employeeType, number);
cout << " The list with hew salary after CUTBACKS: ";
printList(employeeType, number);
system ("pause" );

/// TASK 6 =======================================================

cout << " Enter the mployee ID of the employee to be fired "<<endl;
cin >> id;
yourFired(employeeType, number, id);

cout << " The new employee list after firing employee ID: "
<<id<<" is: "<<endl;
printList(employeeType, number);
system ("pause" );

      programmer_info();
      return 0;
     }                //end of main
//----------------------------------------------
   void getData(ifstream& inFile, class employeeType let[], int& listSize){
// Input employees from the input file
       employeeType item ;
       listSize = 0;

       item = getOne (inFile) ;
       //cout <<" Got the first one! ";

       while (inFile && listSize < MAX_EMPLOYEES )
      {
         // cout <<" top of the looop to ya! ";
        let [listSize ] = item ;
        listSize++;
        cout <<" Current Size = "<< listSize<<endl;

        item = getOne (inFile) ;
      }
      if (inFile )
         cout << "not all data items were input;array too small ";
      inFile.close () ;
   }
//----------------------------------------------
   void printList(class employeeType let[], int listSize){
// Print out the list of employeeType structs
         int looper ;

         for ( looper = 0; looper < listSize ; looper++)
         {
               printOne ( let [looper] ); // whatever you read, it will be printed into printOne.
               cout << endl ;
         }
   }
//----------------------------------------------
   employeeType getOne ( ifstream& dataIn ){
// Input one employee from the CS input file
    employeeType one;

      dataIn >> one.first >> one.last >> one.gender
        >> one.id >> one.payrate >> one.jobRole >> one.years;

    printOne(one);
      return one;
}
//----------------------------------------------
   void printOne ( employeeType one){   //printOne is keep on a loop, use in the structure on the array printList.
//print out one menuItemType struct
      cout << fixed << showpoint << setprecision (2);
      cout << "Emp ID Num: " <<setw(12)<<left<< one.id;   //one is my sctructure
      cout << " Full Name: " <<one.first << " " << one.last;
      cout << " Gender: " <<one.gender;
      cout << " Job Role: " << setw(12)<<left<<one.jobRole;
      cout << " Pay Rate:" << setw(12)<<left<<one.payrate;
      cout << " Yrs Srvc: " << setw(12)<<left<<one.years;
      cout <<" ";
}
///-----------------------------------------------


/// TASK 2
employeeType userinput(){ //reading employeeType data, with variable data

employeeType data;
cout << " Enter a new employee details: "<<endl;

cout << " Enter employee ID: "<<endl;
cin >> data.id;
cout<< " Enter employee First and Last Name: "<<endl;
cin >> data.first>>data.last;
cout << " Enter employee gender: "<<endl;
cin >> data.gender;
cout << " Enter employee Job role: "<<endl;
cin >> data.jobRole;/// TASK 3 =======================================================

cout << " Enter employee Pay rate: "<<endl;
cin >> data.payrate;
cout << " Enter employee years: "<<endl;
cin >> data.years;
cout << " ";

//data.blabla .... the diff variables after the 'dot' get consolidated into 'data'

return data; //consolidates all (name, last payrate, gender,etc) together into 'data' as a whole pack of info.
}

/// TASK 3
void hireOne(employeeType let[], int& listsize){

int location;
employeeType newrecord;

cout <<" Enter the location of the new employee should be inserted"<<endl;
cin >>location;

if(location >= 0 && location < listsize){
newrecord = userinput(); //calling userinput to read/store the newrecord values

for( int i = listsize -1 ; i>= location ; i--)
    let[i+1] = let [i];

    let[location] = newrecord;
    listsize++; //adding a new element to the list, the size of the array increases
    } //end of if

    else
        cout <<" You entered a wrong location"<<endl;
}

/// TASK 4
void selectionSort( employeeType let[], int length)
{
    int index;
    int smallestIndex;
    int minIndex;
    employeeType temp;

    for (index = 0; index < length - 1; index++)
    {
            //Step a
        smallestIndex = index;

        for (minIndex = index + 1; minIndex < length; minIndex++)
            if (let[minIndex].payrate < let[smallestIndex].payrate) //original ""if (list[minIndex] < list[smallestIndex])"" added payrate cuz we aren't comparing the whole list, only the payrate
                smallestIndex = minIndex;

            //Step b
        temp = let[smallestIndex];
        let[smallestIndex] = let[index];
        let[index] = temp;
    }
}

/// TASK 5
void cutBacks(employeeType let[], int listsize){

for(int i = 0 ; i< listsize ; i++) //runs from 0 to end and every time changes the payrate by .90
    let[i].payrate = let[i].payrate * 0.90;

}

/// TASK 6

void yourFired(employeeType let[], int& listsize , int id){

    int location;

location = seqSearch(let, listsize, id); //calling

    if(location == -1)
        cout <<" The employee ID is not found "<<endl;
    else{
        for(int i = location+1 ; i< listsize ; i++)
        let[i-1] = let[i];
        --listsize; // removing an element from a list, the size of the array decreases.
    }


} //end of yourFired
int seqSearch(employeeType let[], int listLength, int searchItem) //we are sending the employee id from 'void yourFired'
{
    int loc;
    bool found = false;

    loc = 0;

    while (loc < listLength && !found)
        if (let[loc].id == searchItem) //searching for id only
            found = true;
        else
              loc++;

    if (found)
        return loc;
    else
        return -1;

} //end of seqSearch


/// PROGRAMER INFO
void programmer_info()
{
    cout << " *************************************** ";
}

========= input .txt file ============

Jasob Born   m 8088 81.30 Cyber-Security 2
Bob Beezer       m 1234   12.91 ComputerScience 3

Explanation / Answer

Hii,

I am replying late because the correction took a long time. The major problem with your code was accessing the private members of employee class by functions which were global. So I had to modify some lines for the function and the structure of the program accordingly.

Just see if its serving your purpose of what you intended. One good thing it would be if the employee data are also written to the file before closing the program, thats a good practice of writing program. That implementation will make the program better, although not necessary now.

Also make a new text file called "newEmployees.txt" under the same directory where the program resides and paste these lines. Remember don't press enter after the last line:

Jason Bourne M 8088 81.30 Cyber-Security 2
Bob Beezer M 1234 12.91 ComputerScience 3
Ron Cook M 7841 30.23 Web-Design 4
Alex Musk M 4561 56.15 Electronics 3

The edited program is here(you can copy paste it directly). Please comment if there are any issues.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib> //must have to use system ("pause" );
//#include "personType.h"

using namespace std;
const int MAX_EMPLOYEES = 50;

// class definition
// class employeeType is misleading, just employee is better
class employee{
private:
long emp_id;
string first_name;
string last_name;
char gender;
double payrate;
string jobrole;
int years;
public:
// constructor with default arguments
employee(long id = 0, string f_name = "", string l_name = "", char gen = ' ',double rate = 0, string role = "",int yr = 0)
{
emp_id = id;
first_name = f_name;
last_name = l_name;
gender = gen;
payrate = rate;
jobrole = role;
years = yr;
}

// class methods that are required to access the private members
void print_emp();
void askinput();
double get_payrate();
void modify_payrate(double);
long get_emp_id();
};


// global function declaration i.e., these doesn't come under class employee as was in the given program
// global functions can't access employee private members
void cutBacks(employee let[], int listsize);
void selectionSort( employee e[], int length);
void hireOne(employee e[], int& listsize);
employee userinput();
void printOne(employee one);
void printList(employee e[],int n);
employee getOne(ifstream& dataIn);
void getData(ifstream& inFile, employee e[], int* listSize);
void yourFired(employee e_list[], int& listsize , int id);
int seqSearch(employee e[], int listLength, int searchItem);
void programmer_info();

// class function definitions
double employee::get_payrate(){
return payrate;
}

void employee::modify_payrate(double m_factor){
payrate = payrate * m_factor;
return;
}

long employee::get_emp_id(){
return emp_id;
}

void employee::print_emp(){
// This will print the information of one employee data
cout << fixed << showpoint << setprecision (2);
cout << " Emp ID Num: " <<setw(12)<<left<< emp_id; //one is my sctructure
cout << " Full Name: " << first_name << " " << last_name;
cout << " Gender: " << gender;
cout << " Job Role: " << setw(12)<<left<<jobrole;
cout << " Pay Rate:" << setw(12)<<left<<payrate;
cout << " Yrs Srvc: " << setw(12)<<left<<years;
//cout <<" ";
}

void employee::askinput(){
// ask for input details and assign to the employee data
cout << " Enter a new employee details: "<<endl;
cout << " Enter employee ID: "<<endl;
cin >> emp_id;
cout<< " Enter employee First and Last Name: "<<endl;
cin >> first_name>>last_name;
cout << " Enter employee gender (M or F): "<<endl;
cin >> gender;
cout << " Enter employee Job role: "<<endl;
cin >> jobrole;
cout << " Enter employee Pay rate: "<<endl;
cin >> payrate;
cout << " Enter employee years: "<<endl;
cin >> years;
cout << " ";
return;
}

int main ()
{
int number; // number of employees in the file
int id;
char choice;
employee emp_list[MAX_EMPLOYEES], newrecord; // declaring two variables of class employee, one is array and other is a variable
ifstream dataFile;
dataFile.open("newEmployees.txt");
if (!dataFile){
cout << " Error with input file!! ";
return 1;
}
getData(dataFile, emp_list, &number);
printList(emp_list, number);
cout <<endl<< " There were " << number << " employees input. ";
//dataFile.close();


// TASK 2 =======================================================
newrecord = userinput();
cout << " The new record details "<<endl;
printOne(newrecord); //call pirntOne to print 'employeeType userinput()'
system ("pause" ); // #include <cstdlib> to be used


// TASK 3 =======================================================
for(int i =0; i<1 ; i++){ //calling hireOne 3 times, you can insert 3 new employees on a roll
hireOne(emp_list, number); // the list increases by up to 3 new records each time
}
cout << "After hiring employees, new list: "<<endl;
printList(emp_list, number);
system ("pause" );


// TASK 4: Use selection sort to sort the array in ascending order by pay rate. Print out the sorted array.
selectionSort(emp_list, number);
cout << " The list after sorting: " << endl;
printList(emp_list, number);
system ("pause" );


// TASK 5 =======================================================
cutBacks(emp_list, number);
cout << " The list with hew salary after CUTBACKS: ";
printList(emp_list, number);
system ("pause" );


// TASK 6 =======================================================
cout << " Enter the employee ID of the employee to be fired "<<endl;
cin >> id;
yourFired(emp_list, number, id);
cout << " The new employee list after firing employee ID "<<id<<" is: "<<endl;
printList(emp_list, number);
system ("pause" );

programmer_info();

return 0;
}


// TASK 5
void cutBacks(employee e[], int listsize){
double mult_factor = 0.90;
for(int i = 0 ; i < listsize ; i++) //runs from 0 to end and every time changes the payrate by .90
e[i].modify_payrate(mult_factor);
}

// TASK 4
void selectionSort( employee e[], int length)
{
int index;
int smallestIndex;
int minIndex;
employee temp;
for (index = 0; index < length - 1; index++)
{
//Step a
smallestIndex = index;
for (minIndex = index + 1; minIndex < length; minIndex++)
if (e[minIndex].get_payrate() < e[smallestIndex].get_payrate()) //original ""if (list[minIndex] < list[smallestIndex])"" added payrate cuz we aren't comparing the whole list, only the payrate
smallestIndex = minIndex;
//Step b
temp = e[smallestIndex];
e[smallestIndex] = e[index];
e[index] = temp;
}
}

// TASK 3
void hireOne(employee e[], int& listsize){
int location;
employee newrecord;
cout <<" Enter the location of the new employee to be inserted:"<<endl;
cin >>location;
if(location > 0 && location < listsize){
newrecord = userinput(); //calling userinput to read/store the newrecord values
for( int i = listsize-1 ; i>= location-1 ; i--)
e[i+1] = e[i];
e[location-1] = newrecord;
listsize++; //adding a new element to the list, the size of the array increases
} //end of if
else
cout <<" You entered a wrong location"<<endl;
return;
}

// TASK 2
employee userinput(){ //reading employeeType data, with variable data
employee data;
data.askinput();
//data.blabla .... the diff variables after the 'dot' get consolidated into 'data'
return data; //consolidates all (name, last payrate, gender,etc) together into 'data' as a whole pack of info.
}


void printOne(employee one){ //printOne is keep on a loop, use in the structure on the array printList.
//print out one menuItemType struct
one.print_emp();
}

void printList(employee e[],int n){
int looper;
for(looper = 0;looper < n;looper++)
{
printOne(e[looper]); // whatever you read, it will be printed into printOne.
cout << endl ;
}
}


employee getOne(ifstream& dataIn){
// Input one employee from the CS input file
string f_name = "";
string l_name = "";
char gen = ' ';
long id = 0;
double rate = 0;
string role = "";
int yr = 0;
dataIn >> f_name >> l_name >> gen >> id >> rate >> role >> yr;
employee e(id, f_name, l_name, gen, rate, role, yr);
//e.print_emp();
return e;
}

void getData(ifstream& inFile, employee e[], int* listSize){
// Input employees from the input file
employee item ;
*listSize = 0;


while (!inFile.eof() && (*listSize < MAX_EMPLOYEES))
{
item = getOne(inFile);
e[*listSize] = item ;
(*listSize)++;
cout <<" Current Size = "<< *listSize <<endl;
}
if(!inFile.eof())
cout << " Not all data items were input;array too small for the file ";
inFile.close();
return;
}

void yourFired(employee e_list[], int& listsize , int id){
int location;
location = seqSearch(e_list, listsize, id); //calling
if(location == -1)
cout <<" The employee ID is not found "<<endl;
else{
for(int i = location+1 ; i< listsize ; i++)
e_list[i-1] = e_list[i];
--listsize; // removing an element from a list, the size of the array decreases.
}
return;
} //end of yourFired

int seqSearch(employee e[], int listLength, int searchItem) //we are sending the employee id from 'void yourFired'
{
int loc;
bool found = false;
loc = 0;
while (loc < listLength && !found){
if (e[loc].get_emp_id() == searchItem) //searching for id only
found = true;
else
loc++;
}
if (found)
return loc;
else
return -1;
} //end of seqSearch

void programmer_info()
{
cout << " **********************Programmer Info************************* ";
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote