# I need help on this problem using classes and arrays

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-21-2008
john5754
I need help on this problem using classes and arrays
a. Construct a class definition that can be used to represent an employee of a company. Each employee is defined by an integer ID number, a floating-point pay rate, and the maximum number of hours the employee should work each week. The services provided by the class should be the ability to enter data for a new employee, the ability to change data for a new employee, and the ability to display the existing data for a new employee.

b. Include the class definition created for Exercise a in a working C++ program that asks the user to enter data for 3 employees and displays the entered data.

c.Modify the program written for exercise b to include a menu that offers the user the following choices:

2. Modify employee data
3. Delete an employee

My teacher told us that using an 2 dimensional array would be a good idea. I have no idea what to do. Please help I will take any advice, or hints. Thanks
• 11-21-2008
master5001
Vectors perhaps? Hows 'bouts sum coed?
• 11-21-2008
rags_to_riches
Yeah, coeds! ;)
• 11-21-2008
john5754
I'm still a novice I dont know how to use coeds or vectors, I'm thinking of using if loops?
• 11-21-2008
Salem
I've no idea why you would want a 2D array either.

But let's begin with step a)
How much of that can you do?
• 11-21-2008
laserlight
Quote:

Originally Posted by john5754
I'm still a novice I dont know how to use coeds or vectors, I'm thinking of using if loops?

By "coed" master5001 presumably meant "code", i.e., you should show what you have tried to code so far. rags_to_riches was just playing on the typo error ("coed" is slang for a female student in a school with both male and female students, i.e., a coeducational school).
• 11-21-2008
john5754
Code:

```#include <iostream> #include <iomanip> using namespace std; class Employee { private:         double id;         double pay;         double hours;         public:         Employee (double = 0.0, double = 0.0, double = 0.0);         void showEmp();         void setNewEmp(double,double,double); }; Employee::Employee(double i,double p, double h) {         id = i;         pay = p;         hours = h; } void Employee::showEmp() {         cout << " Employee ID = " << id <<                 "\n Pay = " << pay << "\n Max. Hours = "<< hours << endl; } void Employee::setNewEmp(double i, double p, double h) {         id = val[0][1];         pay = val[0][2];         hours = val[0][3]; } int main() {         const int r=3;         const int c=3;         Employee info(0,0,0);         float val[r][c];         double a, b,d,e,f,g;                 for(a=0; a<r;)         {                         cout<<"1. Add an employee"<<endl;         cout<<"2. Modify employee data"<<endl;         cout<<"3. Delete an employee"<<endl;         cout<<"4. Exit this menu"<<endl;         cin>>d;                 if(d==1)         {                                 cout<< "Type in employees id number, pay, and maximum hours:"<<endl;                 cin>> b>>e>>f;                 info.setNewEmp(b,e,f);             info.showEmp();                                 }         else if(d==2)         {                 cout<<"Which employee information do you wat to modify."<<endl;         }         else if(d==3)         {                 cout<<"Which employee do yo want to delete"<<endl;         }         else         {                         }                 }         return 0; }```
• 11-21-2008
Elysia
Are you having problems with the array part? On how to be able to add or delete employees from a list of them?

And as a suggestion, firstly, in your class definition, don't delete the parameter names - anyone whose reads that definition can't make out what the parameters are. Secondly, a, b, c, etc, are not very informative variable names. We cannot say for what they are used - use better names!
• 11-21-2008
john5754
Yeah I'm having a problem with the array part on how to delete and add employess. I don't know how to do it. I don't know how I would exit the menu for 4. I'm totally lost
• 11-21-2008
Elysia
Well, the easiest way would, of course, be a vector. But what have you actually studied? What are you allowed to use? Is it about dynamic memory management?
You have 3 choices:
1) Make an array that can hold n employees and enforce this limit.
2) You can use new/delete to make a dynamic array, and manage it yourself.
3) You can use std::vector to make a dynamic array.

Any C++ programmer would use the 3rd option, but if you haven't studied vectors or if you are supposed to use something else...
• 11-21-2008
john5754
We haven't studied vectors, so we can't use that, we can use loops
• 11-21-2008
Elysia
Loops are not enough. If you do not know dynamic memory management, you will have to go route 1.
• 11-21-2008
master5001
Just write a container class.
• 11-22-2008
Elysia
Write a container class? :rolleyes:
That goes back to square 1 again...
• 11-23-2008
john5754
I don't know how to do that what does the syntax look like?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last