Как я могу получить доступ к элементам вектора в C ++ - PullRequest
0 голосов
/ 24 апреля 2020

На самом деле, я ладья ie, и я начал изучать этот язык в возрасте нескольких месяцев. Теперь я столкнулся с неразрешимой проблемой. Я разработал программу для сбора информации о спецификациях многочисленных сотрудников и сохранения ее в векторе. Чтобы повысить производительность, я разработал класс с именем «Сотрудник» и определил функцию, которая дает пользователю несколько опций, таких как добавление нового сотрудника, повышение определенной зарплаты c сотрудника и так далее. Я определил свой критерий соответствия с помощью операторов, но в программе есть ошибка, и именно тогда, когда я хочу отобразить, вывод совершенно странный. это выглядит как какая-то китайская буква: -)

вот мой код:

Спасибо

Моя главная проблема заключается в том, что, когда дело доходит до отображения спецификации сотрудника

#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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...