Thread: I need help on this problem using classes and arrays

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    18

    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:

    1. Add an employee
    2. Modify employee data
    3. Delete an employee
    4. Exit this menu

    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

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Vectors perhaps? Hows 'bouts sum coed?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Yeah, coeds!

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    I'm still a novice I dont know how to use coeds or vectors, I'm thinking of using if loops?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    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;
    }
    Last edited by Salem; 11-21-2008 at 11:55 AM. Reason: [code][/code] go AROUND the code!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    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

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    We haven't studied vectors, so we can't use that, we can use loops

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Loops are not enough. If you do not know dynamic memory management, you will have to go route 1.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Just write a container class.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Write a container class?
    That goes back to square 1 again...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    I don't know how to do that what does the syntax look like?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in classes and arrays
    By hiya in forum C++ Programming
    Replies: 15
    Last Post: 04-09-2005, 02:45 PM
  2. Problem with classes and Arrays
    By GonzO86 in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2005, 04:35 PM
  3. Arrays with base/derived classes
    By shaeng in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2004, 11:54 AM
  4. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 03:57 AM
  5. Help with Arrays of Classes
    By GrNxxDaY in forum C++ Programming
    Replies: 15
    Last Post: 07-25-2002, 09:40 PM