Q: I can not quite understand how to structure variables pass. I tried to do by reference, but can not remember how. Give me the prototypes and the actual code for the functions. I want to pass a structure called employee. Employee variable called objEmp. Like this:
struct Employee
(977 503 char lname [100] 977 503 char fname [100] 977 503 int salary; 977 503 int hours, int 977 503 pay 977 503)
void getInfo ();
void calc ();
int main () (977 503 977 503 getInfo ();) 977 503
void getInfo () (977 503 977 503 int index = 0; 977 503 Employee objEmp [10] 977 503 co?t u003cu003c “lname”; 977 503 cinu003eu003e objEmp [index]. lname, 977 503 (going on) 977 503 (?) calc / * Getting through the structure and the index? This function will be called again and again for 9 other people to give their information so that index needs to increment in Calc function getInfo and returned to. * / 977 503)
Itll be performed as the first employee, 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 will have to print all data in a different role for them all. As Employee # 1 Name, Employee # 2 Name lname, lname. Etc. .
Re:Originally posted by: guy
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 ![]()
Re:I need someone who is on AIM right now to help me with this program, it's hell.
Re:Originally posted by: guy
guy 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.
Re:I would also recommend that you rid yourself of arrays and use the STL (strings, vectors etc).
Re: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]);
}
}
Re:guy 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.
0 Comments.