May 29, 2012 at 6:20pm UTC
It's been many years since I played with C and I am trying to get back into it again. I am working through a book "Programming abstractions in C++" by Eric S. Roberts and Julie Zelenski.
I have come across an exercise with a question posed as follows:
"Design a new type called payrollT that is capable of holding the data for a list of employees, each of which is represented using the employeeT type introduced in the section on “Dynamic records” at the end of the chapter. The type payrollT should be a pointer type whose underlying value is a record containing the number of employees and a dynamic array of the actual employeeT values, as illustrated by the following data diagram:"
"After the input values have been entered, the GetPayroll function should return a value of type payrollT that matches the structure shown in the diagram."
Unfortunately I cannot post the diagrams, but I am hoping this is enough info for someone to get the general idea.
Here is my code:
#include <iostream>
#include <cstddef>
#include <string>
using namespace std;
struct employeeT
{
string fName;
string sName;
string title;
string ssNum;
double salary;
int withHoldingExempts;
};
struct payrollT
{
int numberOfEmployees;
employeeT *eT;
};
payrollT *GetPayRoll();
int main()
{
int counter;
payrollT *pRT;
pRT = GetPayRoll();
for (counter = 0; counter < pRT->numberOfEmployees;counter++)
{
cout << "Employee number:\n" << counter;
cout << "\tFirst Name = " << pRT->eT[counter].fName << endl;
cout << "\tSecond Name =" << pRT->eT[counter].sName << endl;
cout << "\tTitle =" << pRT->eT[counter].title << endl;
// cout << "\tSSNum ="; pRT->eT[counter].ssNum << endl;
// cout << "\tSalary ="; pRT->eT[counter].salary << endl;
// cout << "\tWithholding exemptions ="pRT->eT[counter].withHoldingExempts << endl;
cout << endl << endl;
}
delete pRT;
return 0;
}
payrollT *GetPayRoll()
{
int numberOfEmployees;
payrollT *l_PR = new payrollT;
int counter;
cout << "Enter number of employees:";
cin >> numberOfEmployees;
employeeT *l_empT = new employeeT[numberOfEmployees];
l_PR->numberOfEmployees = numberOfEmployees;
for (counter = 0; counter < numberOfEmployees; counter++)
{
cout << "Details for employee" << counter+1 << endl;
cout << "\tFirst Name: "; cin >> l_empT->fName;
cout << "\tSecond Name: "; cin >> l_empT->sName;
cout << "\tTitle: "; cin >> l_empT->title;
cout << "\tSSNum: "; cin >> l_empT->ssNum;
cout << "\tSalary: "; cin >> l_empT->salary;
cout << "\tWithholding exemptions: "; cin >> l_empT->withHoldingExempts;
l_PR.eT[counter] = l_empT;
}
// l_PR->eT = l_empT;
return l_PR;
}
CodeBlocks is reporting the following error and I am finding it hard to make sense of it:
...\main.cpp|67|error: request for member 'eT' in 'l_PR', which is of non-class type 'payrollT*'|
Does anyone have any ideas how to fix this? Please help.
May 29, 2012 at 6:36pm UTC
Insetad of
l_PR.eT[counter] = l_empT;
you shall write
l_PR->eT[counter] = l_empT;
May 29, 2012 at 6:38pm UTC
Thanks for the reply, but I tried that, and got the same error message during rebuild.
May 29, 2012 at 6:42pm UTC
Sorry, my mistake, I get a new error now:
"...\main.cpp|67|error: no match for 'operator=' in '*(l_PR->payrollT::eT + ((unsigned int)(((unsigned int)counter) * 32u))) = l_empT'|
May 29, 2012 at 7:02pm UTC
O.K., Fiddled a bit and found the solution:
#include <iostream>
#include <cstddef>
#include <string>
using namespace std;
struct employeeT
{
string fName;
string sName;
string title;
string ssNum;
double salary;
int withHoldingExempts;
};
struct payrollT
{
int numberOfEmployees;
employeeT *eT;
};
payrollT *GetPayRoll();
int main()
{
int counter;
payrollT *pRT;
pRT = GetPayRoll();
for (counter = 0; counter < pRT->numberOfEmployees;counter++)
{
cout << "Employee number:\n" << counter;
cout << "\tFirst Name = " << pRT->eT[counter].fName << endl;
cout << "\tSecond Name =" << pRT->eT[counter].sName << endl;
cout << "\tTitle =" << pRT->eT[counter].title << endl;
cout << "\tSSNum =" << pRT->eT[counter].ssNum << endl;
cout << "\tSalary =" << pRT->eT[counter].salary << endl;
cout << "\tWithholding exemptions =" << pRT->eT[counter].withHoldingExempts << endl;
cout << endl << endl;
}
delete pRT;
return 0;
}
payrollT *GetPayRoll()
{
int numberOfEmployees;
payrollT *l_PR = new payrollT;
int counter;
cout << "Enter number of employees:";
cin >> numberOfEmployees;
employeeT *l_empT = new employeeT[numberOfEmployees];
l_PR->numberOfEmployees = numberOfEmployees;
for (counter = 0; counter < numberOfEmployees; counter++)
{
cout << "Details for employee" << counter+1 << endl;
cout << "\tFirst Name: "; cin >> l_empT[counter].fName;
cout << "\tSecond Name: "; cin >> l_empT[counter].sName;
cout << "\tTitle: "; cin >> l_empT[counter].title;
cout << "\tSSNum: "; cin >> l_empT[counter].ssNum;
cout << "\tSalary: "; cin >> l_empT[counter].salary;
cout << "\tWithholding exemptions: "; cin >> l_empT[counter].withHoldingExempts;
}
l_PR->eT = l_empT;
return l_PR;
}