Home > Software design >  How to pass struct array arguments to a function the right way?
How to pass struct array arguments to a function the right way?

Time:02-06

Source Code:

#include <iostream>

using namespace std;

struct Employee {

   string name;
   int age;
   float salary;

};

void displayData(Employee);

int main() {

   Employee employee[3];
   int sizeOfEmployee = sizeof(employee) / sizeof(employee[0]);

   for(int i = 0; i < sizeOfEmployee; i  ) {
       cout << "Enter name for employee " << i   1 << ": ";
       cin >> employee->name;
       cout << "Enter age for employee " << i   1 << ": ";
       cin >> employee->age;
       cout << "Enter salary for employee " << i   1 << ": ";
       cin >> employee->salary;       
}

   for(int i = 0; i < sizeOfEmployee; i  ) {
       displayData(employee[i]);
}

}

void displayData(Employee employee) {

   cout << "DISPLAYING INFORMATION" << endl;
   cout << "Name: " << employee.name << endl;
   cout << "Age: " << employee.age << endl;
   cout << "Salary: " << employee.salary << endl;

}

My code doesn't display all the data i've stored into the array, it only reads and display the last data i've inputted.

CODE OUTPUT:

CODE OUTPUT:

In what way could i possibly get all the data i've stored into the array and display it?

CodePudding user response:

You are reading data into a pointer to the first element of the array. To read into n-th element your code should be using indexing, like cin >> employee[i].name;.

However...

"Naked" arrays are a source of many errors, subtle bugs, and generally unsafe code. Consider using std::array instead - this will eliminate the need for code like int sizeOfEmployee = sizeof(... and will nicely encapsulate that array as a parameter to a function:

#include <array>

int main() {

   std::array<Employee, 3> employee;

   for(int i = 0; i < employee.size(); i  ) {
       cout << "Enter name for employee " << i   1 << ": ";
       cin >> employee[i].name;
   // the rest of that for loop...
   }
// and the second loop becomes:
   for(const auto& e: employee) {
       displayData(e);
}

CodePudding user response:

You are reading data 3 times into the first element in your array:

Employee employee[3];

for(int i = 0; i < sizeOfEmployee; i  ) {
    cout << "Enter name for employee " << i   1 << ": ";
    cin >> employee->name; // employee points to the first element in the array
}

One more comment: your are passing the Employee class by value just to display it. It would be mych better, efficient and robust to pass it by const reference:

void displayData(const Employee&);

CodePudding user response:

int sizeOfEmployee = sizeof(employee) / sizeof(employee[0]); 

Don't do this. Use std::size(employee) if you want to get the length of the array.

for(int i = 0; i < sizeOfEmployee; i  ) {

Don't do this. Instead, use a range-for to loop over all elements of a range:

for (auto&& e : employee)

Which incidentally will fix the bug that you had. The bug is that you always write to the first element of the array. Each iteration over-writes the previous ones and the other elements remain uninitialised.

The range-for will correctly iterate all elements with less chance of messing things up.

  •  Tags:  
  • Related