0

This is what i'm trying to do:

Implement a structure Employee. An employee has a name (a char ) and a salary (a double). Write a default constructor, a constructor with two parameters (name and salary), and methods char getName() double getSalary() to return the name and salary. Write a small global function TestEmployee() to test your structure.

And this is what i've done so far:

#include<iostream>
#include<cstring>
using namespace std;

//Implement a structure Employee.
struct Employee
{
private:
    //An employee has a name (a char *) and a salary (a double).
    char *EmpName;
    double EmpSalary;
public:
    //default constructor
    Employee()
    {
        this->EmpName = new char[100];
        this->EmpSalary = 0.00;
    }
    //a constructor with two parameters (name and salary),
    Employee(char* name_, double salary_)
    {
        do
        {
            this->EmpName = name_;
            this->EmpSalary = salary_;
        
        }while(salary_<0);
    }
    //char* getName() to return the name
    char* getName()
    {
        return this->EmpName;
    }
    //double getSalary() to return the salary.
    double getSalary()
    {
        return this->EmpSalary;
    }
};

//Write a small global function TestEmployee() to test your structure.
void TestEmployee()
{
    //Employee employee_;
    char* empName = new char[100];
    double empSalary;

    cout<<"Creating a new employee.\nPlease type the name:"<<endl;
    cin>>empName;
    cout<<"Please specify the salary:"<<endl;
    cin>>empSalary;
    cout<<"New employee has been created."<<endl;

    Employee employee_(empName, empSalary);

    cout<<"Name of employee: "<<endl;
    cout<<employee_.getName()<<endl;
    cout<<"Salary: "<<endl;
    cout<<employee_.getSalary()<<endl;
    cout<<"Thank you for testing structure Employee."<<endl;
}
int main()
{
    TestEmployee();
}

I can't seem to find the error, this program isn't working. This is the output it's giving:

enter image description here

When this is the output it's supposed to have:

enter image description here

  • 3
    why are you not using `std::string` ? – 463035818_is_not_an_ai Nov 14 '20 at 17:28
  • Here `this->EmpName = name_;`, you are not copying the name, just the pointer to the name – Damien Nov 14 '20 at 17:29
  • @Damien How should i to correct this? – Mahreen Athar Nov 14 '20 at 17:34
  • 1
    Another issue: When you type *Larry Bird*, Larry is taken as the name and Bird as the sallary ! You should use `getline`, in conjonction with `std::string`. In addition, you have a memory leak in your function, solved with `std::string` – Damien Nov 14 '20 at 17:34
  • @idclev463035818 we've been instructed to use char and not string, it would've been easier with string though. – Mahreen Athar Nov 14 '20 at 17:35
  • @MahreenAthar _we've been instructed to use char and not string_ Then you need either fixed size char arrays in your struct, or allocate and manage dynamic memory yourself. – πάντα ῥεῖ Nov 14 '20 at 17:36
  • you must have been told **how** to use char arrays also, otherwise the assignment makes no sense at all. You should have been told how to copy a c-string for example. Maybe review your lecture material – 463035818_is_not_an_ai Nov 14 '20 at 17:36
  • @Damien yes i used string instead of char. It worked thanks. – Mahreen Athar Nov 14 '20 at 17:44
  • @Damien Also how do i fix this program with using char and not string? Just curious. – Mahreen Athar Nov 14 '20 at 17:45
  • Most of the hints have been given: 1. Use [getline](https://en.cppreference.com/w/cpp/io/basic_istream/getline) to read the name 2. Avoid memory leak, by using a fixed size for the array `char empName[100]` (or `delete[]` at the end) 3. Check for positivity of the salary when reading, not when creating the struct instance. 4. Use the proper function to copy a `char *`: [strcmp](https://en.cppreference.com/w/cpp/io/basic_istream/getline). I hope avoiding nothing, not tested – Damien Nov 14 '20 at 17:50

0 Answers0