-4

I am building a program for employee data, and for some reason my code will not run, I have searched this forum and others, and I can't figure out the problem with my code.

#include <cstdlib>
#include <iomanip>
#include <iostream>

using namespace std;


class Employee{
    public:

        int idNumber;
        float SalaryRate;
        char * name;
        int BaseSalary;
        char * hisname;
        float salary;
        float bonus;
        float finalSalary;

        Employee(int idNum) //default constructor function
        {
            SalaryRate=0;
            BaseSalary=0;
            idNumber=idNum;
            BaseSalary=0;
            salary=0;
            bonus=0;        
        }
        //constructor function with parameters
        Employee(char * name, int SalaryRate, int idNumber)
        {

            SalaryRate=0;
            idNumber=0;
            strcpy(name, hisname) ;
        }

        float setBonus()
        {
            cout<<"What is the bonus for this employee?\n";
            cin>>bonus;

        }

        void increaseSalary (float increase)
        {
            cout<<"By what percentage would you like to increase ";
            cout<<"p";
            cout<<"'s salary? \n";
            cin>>increase;
            finalSalary = salary * (increase/100)+bonus;
        }


        void print ()
        {
            cout<<"the salary of ";
            cout<<* name;
            cout<< " is "; 
            cout<<finalSalary; 
        }
};


int main() {
    Employee * employees[100];

    for(int i = 0; i < 100; i++)
    {
        cout<<"What is the name you would like to input? ";
        cin>>employees[i]->name;
        int idNumber=i;
        cout<<"What is "; employees[i]->name; "'s hourly rate? ";
        cin>>employees[i]->SalaryRate;       
    }

    //Employee a();
    //a.increaseSalary();

    return 0;
}
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
user1166637
  • 1
  • 1
  • 3

5 Answers5

2

You are allocating 100 pointers to an Employee. But these are not constructed yet.

Employee* employees[100];

for(int i = 0; i < 100; i++)
{
    Employee* emp = new Employee(i);
    cout<<"What is the name you would like to input? ";
    cin >> emp->name;
    int idNumber=i;
    cout << "What is "; emp->name; "'s hourly rate? ";
    cin >> emp->SalaryRate;

    employees[i] = emp;
}
RvdK
  • 19,580
  • 4
  • 64
  • 107
1

The array of pointers employees[i] is not allocated any memory.
You need to allocate the pointers with memory to be able to use them in a meaningful way.
Further,
You are trying to write data to an unallocated pointer resulting in Undefined Behavior.
You need to allocate enough memory to the pointer name using new to hold the string you input.

Also, You need to follow the Rule of Three for your class.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

You are not initializing your Employee * employees[100]; nor the strings in employees.

Probably what you want is:

class Employee{
public: 
    int idNumber;
    float SalaryRate;
    std::string name; // <--- !
    int BaseSalary;
    std::string hisname; // <--- !
    float salary;
    float bonus;
    float finalSalary;  
...
};
int main() {
    Employee employees[100]; // <--- !

    for(int i = 0; i < 100; i++)
    {
        cout<<"What is the name you would like to input? ";
        cin>>employees[i].name;
        int idNumber=i;
        cout<<"What is "; employees[i].name; "'s hourly rate? ";
        cin>>employees[i].SalaryRate;
    }

    //Employee a();
    //a.increaseSalary();

    return 0;
}
ronag
  • 49,529
  • 25
  • 126
  • 221
  • How do I initialize Employee * employees[100] and the strings? – user1166637 Jan 24 '12 at 09:42
  • I just showed you, in my code it is automatic. `std::string` constructor handles it. And `employees` is now an array of employee which are automatically initialized, instead of as you had before an array of pointers to employee. – ronag Jan 24 '12 at 09:47
  • I know I am not using pointers correctly, so I want to understand how to use them- how can I initialize the pointers? – user1166637 Jan 24 '12 at 09:51
  • @user1166637: `Employee* employees = new Employee[100]; delete[] employees;`. My personal opinion is that you really should read a book on C++ before asking such questions. – ronag Jan 24 '12 at 09:54
1

I see a couple of problems:

  • not allocating your Employees (as noted in other answers)
  • expecting cout<<"What is "; employees[i]->name; "'s hourly rate? "; to print what you want. That's actually three separate statements. To print all three, use cout << "What is " << employees[i]->name << "'s hourly rate? ";
  • using c-style strings rather than std::string
  • breaking encapsulation by making the members of Employee public

There are possibly other issues, those are the ones I found first.

zennehoy
  • 6,405
  • 28
  • 55
0

It's crashing very quickly.

This is because this:

Employee * employees[100];

Declares an array of 100 employee pointers. NOT objects.

Then in the loop you attempt to access an object that does not exist:

employees[i]->name

Because you are accessing via a pointer that has not been initialized.
You need to understand objects before you start playing with pointers and dynamically allocated objects.

Employee     employees[100];   // Declare an array of 100 objects.

Then you can read the names with:

cin >> employees[i].name;

But now you have the problem that name is an unitialized pointer. The problems go on like this. You need to remove pointers from your code and use objets wherever you can.

Martin York
  • 257,169
  • 86
  • 333
  • 562