How do I pass structures in C++?

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
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...

 

manly

Lifer
Jan 25, 2000
13,302
4,076
136
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.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
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]);
}
}
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
I would also recommend that you rid yourself of arrays and use the STL (strings, vectors etc).
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
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.
 

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
I need someone who is on AIM right now to help me with this program, it's hell.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
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 :)