• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How do I pass structures in C++?

idNut

Diamond Member
I can't quite understand how to pass structure variables. I've been trying to do it by reference but can't remember how. Please give me the prototypes and actual code for the functions. I want to pass a structure called Employee. The Employee variable is called objEmp. Like this:

struct Employee
{
char lname[100];
char fname[100];
int Wage;
int Hours;
int Pay;
}

void getInfo();
void calc();

int main()
{
getInfo();
}

void getInfo()
{
int Index = 0;
Employee objEmp[10];
cout<<"LName: ";
cin>>objEmp[Index].lname;
(It goes on)
calc(?????????????) /*How do I pass the structure and Index? This function will be called again and again for 9 other people to enter their info so Index needs to increment in the calc function and returned to getInfo. */
}

It'll be run like, first employee has the index number of 0 so everything inside objEmp.lname has my last name and then it goes to 1 for the next employee and he has his name in objEmp.lname and eventually I'll have to print off all the data in another function for all of them together. Like Employee #1 Name, LName Employee #2 Name, LName. Etc...

 
singh already gave you the answer to your question on pass by reference in your other thread.

According to Stroustrup (creator of C++), pass by reference should be used when you don't modify the argument. He prefers pointers in the situation you describe. So go study up on pointers. 🙂

The method signature could look like:

void getInfo(struct employee * emp);

with input calls such as:

cin >> emp->lname;

getInfo() is the wrong place to implement the array. The procedure calling getInfo should be implementing that logic, and delegates to getInfo() to fill in one struct at a time.
 
struct Employee
{
char lname[100];
char fname[100];
int Wage;
int Hours;
int Pay;
};


// Prototype
void getInfo(Employee &emp);


// Definition
void getInfo(Employee &emp)
{
cin >> emp.lname;
// ...
}


void main()
{
Employee MyEmployees[10];


for(int x=0; x < 10; ++x)
{
getInfo(MyEmployees[x]);
}
}
 
Originally posted by: manly
singh already gave you the answer to your question on pass by reference in your other thread.

According to Stroustrup (creator of C++), pass by reference should be used when you don't modify the argument. He prefers pointers in the situation you describe. So go study up on pointers. 🙂

Why would you prefer pointers to references when when modifying the argument? I always pass by reference ... the notation is much cleaner IMHO. If I don't intend to modify the object, I pass it as a const reference.
 
Originally posted by: idNut
I need someone who is on AIM right now to help me with this program, it's hell.

Post the code that you have. Better yet, host it somewhere and give post a link 🙂
 
Back
Top