I am required to create a program that will allow user to enter employee name,age and update salary later on. After which able to generate out the details.

I am trying to declare a class object in the form of an array so that i can input data and then manipulate the data - add and view by id

class Employee
   {
   private:
	   static int id;
           char name[20];
           char age[20];
           double salary;
   public:
          Employee();
          void Add(char n[], char a[], int size);
          double getSalary();
          void print();
          
   };

int Employee::id = 0;

int Employee::getID()
{
     return id;
}

void Employee::Add(char n[], char a[], int size)
{  
  	name[i] = n[i];
	age[i] = a[i];
   
}

int main()
{
char name[20];
char age[20];
Employee employee_list[100];

do{
cout << "Enter name: ";
cin >> name[count];

    return 0;
}
cout << "Enter age: ";
cin >> age[count];

employee_list[ ?? ].setInfo( name[count],age[count],  count)  // nt sure??
cout++;

)while(count < 20);
}

it will display like

Employee List 1
name1 22
name2 33
so on

From here how do i carry on?

{
private:
static int id;
char name[20];
char age[20];
double salary;
public:
Employee();
char *getDepartment();
double getSalary();
void print();
};
int Employee::id = 0;
int Employee::getID()
{
return id;
}
void Employee::Add(char n[], char a[], int size)
{
name = n;
age = a;
}
int main()
{
char name[20];
char age[20];
Employee employee_list[100];
do{
cout << "Enter name: ";
cin >> name[count];
return 0;
}
cout << "Enter age: ";
cin >> age[count];
employee_list[ ?? ].setInfo( name[count],age[count],  count)  // nt sure??
cout++;
)while(count < 100);

Code tags and formatting please (as abopve)

Not using code tags loses all the indentation, which makes it too hard to read.

You obviously need a bracket to end the main function, right? You can't get anything to work till you have that. Make sure all of your class functions are defined inside your class.

first of all use code tags. otherwise u wont see any help on the way...

Perhaps you guys may have another alternative way.. 1 obj array with item array in it (beginner mode)

Firstly a couple of things, you should use the code tags as it makes your program much easier to read and reply to all you have to do is [ code = language ]....[/code ] without the spaces.

You have declared a constructor called Employee but its not being used, so your first move should be to to initialize your variables through your constructor. You have also declared member functions which are not being used in your program such as the

void Add(char n[], char a[], int size);
double getSalary();

Try and get a paper and pen and prepare a draft to see how the program should run and you will get a clearer idea.

/*
Declare a array of classes and loop to go through the array
Delcare a member function to add the data
Declare a member function to view the data
Use operator overloading to compare to classes and sort the array

Now this is just one method and may not be the method that is right for you to use.

Does this compile? You don't have this function in your Employee class declaration.

int Employee::getID()
{
     return id;
}

It doesnt compile. Just hope that my class have the idea of it?
Basically what i want first, is the understanding of passing array to an object array. Of course, how the array is linked to tat object array after many insertion etc

It doesnt compile. Just hope that my class have the idea of it?
Basically what i want first, is the understanding of passing array to an object array. Of course, how the array is linked to tat object array after many insertion etc

I told you at least one reason why it won't compile. Have you changed it?

I don't know how to respond to this:

Basically what i want first, is the understanding of passing array to an object array. Of course, how the array is linked to tat object array after many insertion etc

"passing array to an object array"? What does that mean? The other sentence doesn't make sense either to me. I don't know what you are asking.

Have a main function that is empty first. Get the program to compile with the empty main function. Then start at the top of the main function and start adding the lines back in till it doesn't compile anymore, at which point you know you have a problem.

Hi
this a an error- and warning- free solution to your task:

#include <iostream>

const int LEN = 20;

class Employee
{
public:
	Employee() : _Age(0), _Salary(0.0) {}
	Employee(int n) : _Age(0), _Salary(0.0) 
	{
		for (int i=0;i<n;++i)
			_theEmployees = new Employee[n];
	}
	void setData()
	{
		std::cout<<"Enter employess's last name: "<<std::endl;
		std::cin>>_Name;
		std::cout<<"Enter employess's age: "<<std::endl;
		std::cin>>_Age;
		std::cout<<"Enter employess's salary: "<<std::endl;
		std::cin>>_Salary;
		std::cout<<"---"<<std::endl;
	}
	void getData(int n)
	{
		std::cout<<"Entry "<<n+1<<": Employees's last name is: "<<_Name;
		std::cout<<"\nEntry "<<n+1<<": Employees's age is: "<<_Age;
		std::cout<<"\nEntry "<<n+1<<": Employees's salary is: "<<_Salary;
		std::cout<<"\n---"<<std::endl;
	}
	void add(int n);
	void display(int n);
private:
	char _Name[LEN];
	int _Age;
	double _Salary;
	//Array of employee objects
	Employee* _theEmployees;
};

void Employee::add(int n)
{  
	for (int i=0;i<n;++i)
		_theEmployees[i].setData();
}

void Employee::display(int n)
{  
	for (int i=0;i<n;++i)
		_theEmployees[i].getData(i);
}

int main()
{
	int n; //number of employees to be added
	std::cout<<"Please enter the number of";
	std::cout<<"\nemployees u would like to add: ";
	std::cout<<std::endl;
	std::cin>>n;
	Employee employees(n);
	employees.add(n);		//Add employee	
	employees.display(n); //Display all employees
}

However I left a bug for u to find it yourself :D

And an advice for designing and writing code:
Start always from main trying to write some stubbed implementation.
Try to keep things in main as simple as possible. When u r done there try to implement
these " interfaces" inside the class.
Its like "playing the role" of a " 3rd user" who has no idea how a class is implemented and uses only her interface when working in main and the "developer role" when u implement the methods within the classes.

#include <iostream>
using namespace std;
class Employee
   {
   private:
	   static int id;
           char name[20];
           char age[20];
           double salary;
   public:
          Employee(){};
          int getID();
          void Add(char n[], char a[], int size);
          double getSalary();
          void print();
          
   };

int Employee::id = 0;

int Employee::getID()
{
     return id;
}

void Employee::Add(char n[], char a[], int size)
{  
     for(int i =0; i<size; i++)
     {
  	name[i] = n[i];
	age[i] = a[i];
  }
}

int main()
{
char name[20];
char age[20];
Employee emp;
int empCount = emp.getID();
Employee employee_list[100];
int count =0;

while(count < 20){
cout << "Enter name: ";
cin >> name[count];

  

cout << "Enter age: ";
cin >> age[count];

employee_list[empCount].Add(name,age, count);  // nt sure??
count++;

 
}return 0;
}

Each Employee object array will have item arrays of name[20], age[20]

employee_list[0] will consist item array of name[20], age[20]
employee_list[1] will consist item array of name[20], age[20]
employee_list[2] will consist item array of name[20], age[20]

What i meant - how do the compiler know which item arrays belong to the specfic employee_list when viewing


Each Employee object array will have item arrays of name[20], age[20]

employee_list[0] will consist item array of name[20], age[20]
employee_list[1] will consist item array of name[20], age[20]
employee_list[2] will consist item array of name[20], age[20]

What i meant - how do the compiler know which item arrays belong to the specfic employee_list when viewing

If you have 100 employee_lists, you'll have 100 name arrays and 100 age arrays, one for each employee_list. If you specify a particular employee_list and do something to change that employee_list's data, the program will access the two arrays that are "owned" by that employee_list.

while(count < 20){
cout << "Enter name: ";
cin >> name[count];

  

cout << "Enter age: ";
cin >> age[count];

employee_list[empCount].Add(name,age, count);  // nt sure??
count++;

 
}

Line 3 - make sure you aren't confusing a char array with a char* array. You are incrementing count by 1 each time, so if I enter "Dave", then the next time through the loop I enter "Sam", the name[] array ends up holding "DSam" the second time through when count = 1.

You may want to just replace line 3 with:

cin >> name;

Also check whether empCount ever changes. If it doesn't what's the point in having an array of type employee_list. You're just writing over the same array element each time in line 53.

Also check whether empCount ever changes. If it doesn't what's the point in having an array of type employee_list. You're just writing over the same array element each time in line 53.

i have a static id which i am not sure where do i include it to such that whenever the user finished adding a new emp, the id will +1;

Also check whether empCount ever changes. If it doesn't what's the point in having an array of type employee_list. You're just writing over the same array element each time in line 53.

i have a static id which i am not sure where do i include it to such that whenever the user finished adding a new emp, the id will +1;


If you have 100 employee_lists, you'll have 100 name arrays and 100 age arrays, one for each employee_list. If you specify a particular employee_list and do something to change that employee_list's data, the program will access the two arrays that are "owned" by that employee_list.

how do i go on this???

hi again,

please dont PM me questions, because if I answer them,then here nobody else will know what the question was, and if answer u with PM then nobody else will be able to see to the solution...
:D :D

However I left a bug for u to find it yourself

I did this before on purpose in order to force to further examine and thus understand the code. U r right I left the return 0; statement out.

i have a static id which i am not sure where do i include it to such that whenever the user finished adding a new emp, the id will +1;

One way to do it is to do following steps:
1. Declaration within the class body:

static int id;

2. Initialization outside the class and outside of main:

int Employee::id=0;

3. Implementation within a class of a suitable accessor method:

int getId() const
	{
		return id;
	}

4. U want to count the objects when they are added->inside the add method u increase id:

void Employee::add(int n)
{  
	for (int i=0;i<n;++i)
	{
		_theEmployees[i].setData();
		++_theEmployees[i].id;            //HERE
	}
}

5. Now u would might lets see this id count in our main using:

std::cout<<"Till now were added: ";
	std::cout<<employees.id<<" employees"<<std::endl;

WRONG WRONG
because this way declaring private members would not be meaningfull
Thus a solution which preserves data encapsulation is:

std::cout<<"Till now were added: ";
	std::cout<<employees.getId()<<" employees"<<std::endl;

And this is the reason of making step 3

From your post
Let's say no pointers allowed
1)how do i declare Employee* _theEmployees; ..Can declare in main Employee employee_list[100];

void Employee::add(int n)
{  
	for (int i=0;i<n;++i)
	{
		_theEmployees[i].setData();
		++_theEmployees[i].id;            //HERE
	}
}

2)For each insertion, how do i need to set the id++ to each list? using set method or the above will do??

3) After that i can enter list id to retrieve out the details. Can i be able to reteive the selected empolyee_list based on this id after add other list. (pass in the object)??

From your post
Let's say no pointers allowed
1)how do i declare Employee* _theEmployees; ..Can declare in main Employee employee_list[100];

Here is the code without pointers:

#include <iostream>

const int LEN = 20;
const int MAX_EMPL = 100;

class Employee
{
public:
	Employee(){}
	void setData()
	{
		std::cout<<"Enter employess's last name: "<<std::endl;
		std::cin>>_Name;
		std::cout<<"Enter employess's age: "<<std::endl;
		std::cin>>_Age;
		std::cout<<"Enter employess's salary: "<<std::endl;
		std::cin>>_Salary;
		std::cout<<"---"<<std::endl;
	}
	void getData()
	{
		std::cout<<"Employees's last name is: "<<_Name;
		std::cout<<"\nEmployees's age is: "<<_Age;
		std::cout<<"\nEmployees's salary is: "<<_Salary;
		std::cout<<"\n---"<<std::endl;
	}
	int getId()const
	{
		return id;
	}
	void add();
	void display();
private:
	char _Name[LEN];
	int _Age;
	double _Salary;
	static int id;
};
int Employee::id=0;

void Employee::add()
{  
	setData();
	++id;
}

void Employee::display()
{  
	getData();
}

int main()
{
	Employee employees[MAX_EMPL];
	int n; //number of employees to be added
	std::cout<<"Please enter the number of";
	std::cout<<"\nemployees u would like to add: ";
	std::cout<<std::endl;
	std::cin>>n;
	for (int i=0;i<n;++i)
	{
		employees[i].add();		//Add employee	
	}
	for (int i=0;i<n;++i)
	{
		employees[i].display(); //Display all employees
	}	
	std::cout<<"Till now were added: ";
	std::cout<<employees[n].getId()<<" employees"<<std::endl;
	
	return 0;
}

Of course u can name n to: empCount if u like :D

In case u ask yourself why I use both the display and the getData functions AND NOT SIMPLY THE getData:

void Employee::display()
{  
	getData();
}

There is a reason:
In case I inherit later from Employee and use polymorphism, I will be this way able to add later case-specific functionality to the display independent to the getData function :D

How can i make it such that it will add employee list obj at a time and insert max of 7 employee.

employee list 1 <=== static id of 1 when create 1st employee list .. with 5 employee in it
name x5
age x5
salary x5


employee list 2 <=== id of 2 when create 2nd employee list .. with 6 employee in it
name x6
age x6
salary x6

and so on...


employees[0] will have a array of 5 item in it..
employees[1] will have a array of 6 item in it..

no clue...
....
...
private:
	char _Name[LEN];
	int _Age[LEN];
	double _Salary[LEN];


void Employee::add(int size)
{
     for(int i =0; i<size;i ++)
    {
       setData();
     }

//++id; 
}


int main
{
Employee employees[MAX_EMPL];

bool done = false;
int choice;
int id;
    while (!done) 
    {      
       cout <<" Company Employee List creator ";
        cout <<" 1) Add employees - Max 7 ";
        cout <<" 2) View employee list by id";
        cout <<" 3) Update list by id";
         out <<" 0) Exit";
       cin >> choice;
       
       switch(choice)
       {
           case '1':
               
                employees[0].add(7); <== 0 which is the ID.. where do i increment it?
                break;
           case '2':
               cout<<"enter id :" <<endl;
                cin >> id;
                 employees[id].display();
                break;  
           case '3':
                 cout<<"enter id :" <<endl;
                cin >> id;
                 employees[id].display();
                 //update details 
                break;        
           case '0': 
                done = true;               
                break;
           default:                
                cout<<"Invalid choice!\n"<<endl;    
        }
    }
    return 0;
}

How can i make it such that it will display
employee list 1 <=== static id when create 1st employee list .. with 5 employee in it
name x5
age x5
salary x5
employee list 2 <=== static id when create 2nd employee list .. with 6 employee in it
name x6
age x6
salary x6
and so on...

void Employee::add()
{
for(int i =0; i<7;i ++)
{
setData();
}
++id;
}

The above would normally work but u should make changes in main as well, due to the fact that the add function is called within a loop there. That means that if u make the aforementioned changes in the add function, but leave the main as it is then id will increase while being for example in the first list, because of the for loop in main...

In addition u should pass then as parameter the number of employees of each list like in the initial posted code i posted, instead of using constants to the loop within the add method.

Edited my post ... still not clear on this issue..

Which one of the following do u want that the user decides:

  1. Both the number of lists and employees per list
  2. Only the number of lists and employees per list will be always 7
  3. Only the number of lists and employees per list will be always 7 or less

When u want to view things, how to do u want to do this:

  1. Give the list id and then view all the employees of that list
  2. Give the list id and the number of the employee in order to view only this
    specific employee

For the 1st..
user can decide on the number of lists
Employee employees[user input] <= 30 etc

1st Add -> employees[0] with array details
employee list 1
name x2
age x2
salary x2

2nd Add -> employees[1] with array details
employee list 2
name x5
age x5
salary x5

It will prompt the user to enter employee details till he is satisfied but limit to max 7
he can be allow to enter min of 1 and exit to menu. Thus when add option selected, the employee list id will increment by 1 and proceed on which the adding of employee


2nd will be
1. Give the list id and then view all the employees of that list . ... entered 2
From there, user can be able to update each employee details upon his option

employee list 2
name x5
age x5
salary x5

Hope you understand what i mean..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.