Question:
programming assingment?? c language?
Mister
2012-04-22 04:28:38 UTC
Make a data entry software which can store name, age, salary and gender.
All the stored data could also be displayed on screen.
NOTE: Use function and array techniquies.
Hint: Make global variables and use exception handling.

i've got this assingment, how to scan an array of unknown size???
Five answers:
MichaelInScarborough
2012-04-25 08:39:09 UTC
1. Because the "hint" indicates exception handling I suggest that C++ rather than C language is requested.

2. "CEmployee *c" and the variable iNumber are used globally. Both of these variables should be read and updated as necessary.

3. In case you want to keep the number of records unknown you could use lists as explained here: http://www.cplusplus.com/reference/stl/list/

However, your assignment seems to recommend usage of arrays and that is what you find coded further down.

4. In order to make sure that class data keep their integrity you could provide parameter checking in your methods and friend functions!



#include

#include

#include

using namespace std;



class CPerson{

private:

time_t date;

int iAge;

string strName;

char cGender;

public:

CPerson* get_aperson() {return this;}

CPerson() : iAge(0), strName(""), cGender('\0'), date(0) {}

CPerson(const CPerson& p)

{

date = p.date;

iAge = p.iAge;

strName = p.strName;

cGender = p.cGender;

}

CPerson(const string& name, const int& age, const char& gender, const time_t& d = time(NULL)) : strName(name), iAge(age), cGender(gender), date(d) {}

friend ostream& operator<<(ostream &os, const CPerson& p)

{

os << setw(25) << p.strName << setw(3) << p.cGender << setw(4) << p.iAge;

return os;

}

friend istream& operator>>(istream &is, CPerson *p)

{

p->date = time(NULL);

is >> p->strName >> p->cGender >> p->iAge;

}

friend istream& operator>>(istream &is, CPerson& p)

{

p.date = time(NULL);

is >> p.strName >> p.cGender >> p.iAge;

return is;

}

};



class CEmployee : public CPerson{

private:

double dblSalary;

public:

const double& get_salary() const{return dblSalary;}

CEmployee(const CPerson &p, const double &dblIncome) : CPerson(p), dblSalary(dblIncome) {}

CEmployee(const string& name, const int& age, const char& gender, const double& income) : CPerson(name, age, gender), dblSalary(income) {}

CEmployee(const string& name, const char& gender, const int& age, const double& income) : CPerson(name, age, gender), dblSalary(income) {}

CEmployee(){}

friend ostream& operator<<(ostream &os, const CEmployee& p)

{

os << static_cast(p) << setw(12) << fixed << setprecision(2) << p.dblSalary;

return os;

}



friend istream& operator>>(istream& is, CEmployee &a)

{

string strName = "";

int iAge = 0;

char cGender = '\0';

double dblIncome = 0.0;

cout << "Enter name> ";

getline(is, strName);

cout << "Enter GENDER, AGE, and INCOME> ";

is >> cGender >> iAge >> dblIncome;

while(is.get() != '\n');

CPerson p(strName, iAge, cGender);

a = CEmployee(p, dblIncome);

return is;

}

};



// Global constants/variables

const int iMaxNumber = 10;

CEmployee *c = NULL;

int iNumber = 0;



int main()

{

do {

cout << "How many Employees do you want to enter?> ";

cin >> iNumber;

if (iNumber < 0 || iNumber > iMaxNumber){

cout << "x <= 0 <= " << iMaxNumber << endl;

}

}while(iNumber < 0 || iNumber > iMaxNumber);

while(cin.get() != '\n');

try {

c = new CEmployee[iNumber];

for (int i = 0; i < iNumber; i++) {

cin >> c[i];

}

for (int i = 0; i < iNumber; i++) {

cout << c[i] << endl;

}

delete [] c;

}

catch(...) {

cout << "There was an exception!" << endl;

}

return 0;

}
Motorhead
2012-04-22 14:41:53 UTC
You don't. Instead you use an array of a fixed size that is larger than you could need.

But you make it an array of pointers, so that each entry does not take much space.

Then when you want to fill an entry, you set the pointer to the new record struct instance.



typedef struct employee

{

char name[256];

int age;

int salary;

int gender; //0 male, 1 female]

} Employee;



Employee* payroll[1000]; //just allocates room for pointers

payroll[0] = new Employee; //now you allocate the rest of the space for the record

strcpy( payroll[0]->name, "Bob Tucker"); //use -> instead of . to dereference because it is ptr
anonymous
2012-04-22 16:48:49 UTC
You don't, you use a dynamic array. Basically you declare a variable called size, then as the user how many people you need to put in, then you declare a POINTER to an array and allocate the size amount of elements to it.
Brian
2012-04-22 05:00:36 UTC
A typical solution is to use a linked list. For example, here's how you might set up your employee list:



typedef struct EmployeeStruct {

.....char name[50];

.....int age;

.....int salary;

.....char gender; // 'M' = male, 'F' = female

.....struct EmployeeStruct *next;

}Employee;



// keep pointers to the first and last node in the list

Employee* listHead = null;

Employee* listTail = null;



// This function appends an employee to the back to the list.

void Insert(Employee* e) {

.....if (listHead == null) { // if listHead is null, then the list is empty

..........listHead = e;

..........listTail = e;

.....} else {

..........listTail->next = e;

..........listTail = e;

.....}

}



// This function compares the given string to the name field of all Employee records in the list.

// It returns a pointer to the first matching Employee record.

// If no match is found, then it returns null.

Employee* FindEmployeeByName(const char* name) {

.....Employee* e = listHead;

.....while(e != null) {

..........if (strcmp(e->name, name) == 0) {

...............return e;

..........}

..........e = e->next;

.....}

.....return null; // no employee by that name was found

}
tbshmkr
2012-04-22 04:34:20 UTC
Show your code.

=

People will help you get your assignment right.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...