0

I try to write an employee main file and met with one problem. I can not add an answer for the second question which I try to promt from user. If I change the places between the first question: cout << "\nEnter a percent raise: "; and the second question: cout << "\nEnter a new surname: "; everything works fine, but it not what I need. Could you please help me ?

#include <iostream>
#include "Employee.h"
#include <string>

using namespace std;

int main()
{
    //Create two employees
    Employee e1{ "John", "Doe", 1234.56 };
    Employee e2{ "Jane", "Jones", 7890.12 };

    //Display employee's info
    cout << "Employee e1: " << e1.getName() << ", " << e1.getSurname() << ", " << e1.getSalary() << endl;
    cout << "Employee e2: " << e2.getName() << ", " << e2.getSurname() << ", " << e2.getSalary() << endl;


    //Give a permanent raise to both employee
    cout << "\nEnter a percent raise: ";
    double annualraise;
    cin >> annualraise;
    e1.setRaise(annualraise), e2.setRaise(annualraise);
    cout << "Employee e1: " << e1.getName() << ", " << e1.getSurname() << ", " << e1.getSalary() << endl;
    cout << "Employee e2: " << e2.getName() << ", " << e2.getSurname() << ", " << e2.getSalary() << endl;
    

    //Change Jane's last name to "Doe"
    cout << "\nEnter a new surname: ";
    string theSurname;
    getline(cin, theSurname);
    e2.setSurname(theSurname);
    cout << "Employee e2: " << e2.getName() << ", " << e2.getSurname() << ", " << e2.getSalary() << endl;
    
    return 0;
}

//IMPLEMENTATION FILE (Employee.cpp)

#include "Employee.h"
#include <string>
using namespace std;
#include <iostream>


Employee::Employee(std::string employeeName, std::string employeeSurname, double monthlySalary)
    :name{employeeName}, surname{employeeSurname}, salary{monthlySalary}
{
}

void Employee::setName(std::string employeeName)
{
    name = employeeName;
}

string Employee::getName() const
{
    return name;
}

void Employee::setSurname(std::string employeeSurname)
{
    surname = employeeSurname;
}

string Employee::getSurname() const
{
    return surname;
}

void Employee::setRaise(double raise)
{
    if (raise >= 0)
    {
        salary = (salary * raise) + salary;
    }
    else
        cout << "Wrong integer";
}

double Employee::getSalary() const
{
    return salary * 12;
}
Julia
  • 1
  • 2
  • When the program reaches `cout << "Employee e1: " << e1.getName() << ", " << e1.getSurname() << ", " << e1.getSalary() << endl;` has `e1` been given a name that can be safely printed? We don't know because we have not been given `Employee` code. – user4581301 Oct 06 '21 at 18:44
  • I think we'd need to see how `Employee` is defined. How do `setRaise` and `setSurname` work? Also, given that you repeatedly print the employee info, you may want to break this out into a member function of `Employee`. – Chris Oct 06 '21 at 18:45
  • Yes, e1 and e2 both have names and surnames in the constructor which is located above. – Julia Oct 06 '21 at 18:48
  • I add Employee.cpp file. – Julia Oct 06 '21 at 18:52

0 Answers0