0

Actually, I am rookie, and I started to learning this language a few months age. Now, I confronted with an insoluble issue. I devised a program to get information of numerous employee's specification and store it in a vector. In order to acquire better performance, I designed a class named "Employee", and defined a function to give some options to user, like adding new employee, promotion a specific employee's salary and so on. I defined my matching criterion through operators, but there is an error in program, and that is when I want to display, the output is completely weird. it looks like a some kind of Chinese letter :-)

here is my code:

Thanks

My major problem is when it comes displaying employee's specification

#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <sstream>

using namespace std;

class Employee
{
public:
    Employee(string FirstName, string LastName, int age , double Salary, bool Status ) ://constructor
        fName(FirstName), lName(LastName), EmpAge(age), EmpSalary(Salary), RecruitmentStatus(Status) {};

    void setSalary(const double&);
    void SetRecruitmentStatus(const bool&);
    void Promote(const double&);
    //====================================//
    operator const char* ()
    {
        ostringstream formattedOutput;
        formattedOutput << this->fName.c_str() << " " << (this->lName.c_str()) << " | " << this->EmpAge << " | " << this->EmpSalary << ((RecruitmentStatus) ? "Working" : "Fired");
        string output = formattedOutput.str();
        return output.c_str();
    }

    bool operator == (const Employee& anotherEmployee)const
    {
        return (this->lName == anotherEmployee.lName);
    }

    bool operator < (const Employee& anotherEmployee)const
    {
        return (this->EmpSalary < anotherEmployee.EmpSalary);
    }

private:
    string fName, lName;
    int EmpAge;
    double EmpSalary;
    bool RecruitmentStatus;
};

void Employee::setSalary(const double& income)
{
    this->EmpSalary += income;
}

void Employee::SetRecruitmentStatus(const bool& Status)
{
    this->RecruitmentStatus = Status;
}

void Employee::Promote(const double& Promotion)
{
    this->EmpSalary += Promotion;
}

template <typename T>
void displayElements(T& input)
{
    for (auto iterator = input.begin();
        iterator != input.end(); iterator++)
    {
        cout << *iterator << endl;
    }
}

void selection(int&);
Employee getNewEmp(void);
int main()
{
    vector<Employee> Records;
    int ID;
    do
    {
        selection(ID);
        switch (ID)
        {
        case 1:
        {
            Records.push_back(getNewEmp()); 
            break;
        }
        case 2:
        {
            cout << "please enter last name of the employee you want to change his/her recruitment'status: ";
            string lastName;    cin >> lastName; cout << endl;
            auto memberLocation = find(Records.begin(), Records.end(), lastName.c_str());
            break;
        }
        case 3:
        {
            cout << "please enter last name of the employee you want to promote his/her recruitment'salary: ";
            string lastName;    cin >> lastName; cout << endl;
            auto memberLocation = find(Records.begin(), Records.end(), lastName.c_str());

            break;
        }
        default:
        {
            if (ID !=4)
            {
                cout << "Wrong selection!\nThe program will be closed after a few seconds\n";
                exit(1);
            }

        }
        }

    } while (ID != 4);
    displayElements(Records);
    cout << "Thanks for using this program\n";
    return 0;
}

void selection(int& input)
{
    system("cls");
    cout << "please Choose one of these option:\n"
        "1. add a new employee\n"
        "2. changing an employee status\n"
        "3. Promote an employee's salary\n"
        "4. exit  :  ";
    cin >> input;
}

Employee getNewEmp(void)
{
    system("cls");
    string firstName, lastName;
    cout << "first name:"; cin >> firstName; cout << endl;
    cout << "last name:"; cin >> lastName; cout << endl;
    int age;
    cout << "Age: "; cin >> age; cout << endl;
    double salary;
    cout << "Offered Salary: "; cin >> salary; cout << endl;
    bool Status;
    cout << "Is he/she working(1) or got fired(0) : "; cin >> Status; cout << endl;

    return Employee(firstName, lastName, age, salary, Status);
}
matt sh
  • 1
  • 2
  • 1
    Are we supposed to guess where in the code the error is and what the error message is? Have a look at [ask] and how to make a [mcve]. – super Apr 24 '20 at 20:14
  • I can compile this program without any errors. what errors are you getting? or could it be that you mean that there is a logic error in the code? – Stack Danny Apr 24 '20 at 20:16
  • Not immediately obvious what the problem is. You're going to have to give a few more clues. Or maybe, just maybe you could supply all necessary details without anyone having to ask. – john Apr 24 '20 at 20:19
  • Thanks , The main problem is that I cannot add new employee using push_back(). – matt sh Apr 24 '20 at 20:20
  • 1
    `operator const char *()` returns a pointer from an object that is out of scope. The proper thing would be to supply an `operator<<` function. https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading – Retired Ninja Apr 24 '20 at 21:00

0 Answers0