如何以正确的方式将结构数组参数传递给函数?

How to pass struct array arguments to a function the right way?

源代码:

#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;

}

我的代码没有显示我存储到数组中的所有数据,它只读取并显示我最后输入的数据。

代码输出:

有什么方法可以把我存储到数组中的所有数据都显示出来?

您正在将数据 3 次读取到数组的第一个元素中:

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
}

还有一条评论:您按值传递 Employee class 只是为了显示它。通过 const 引用传递它会更好、更高效、更健壮:

void displayData(const Employee&);

您正在将数据读入指向数组第一个元素的指针中。要读入 n-th 元素,您的代码应该使用索引,例如 cin >> employee[i].name;.

然而...

“裸”数组是许多错误、细微错误和通常不安全代码的来源。考虑改用 std::array - 这将消除对像 int sizeOfEmployee = sizeof(... 这样的代码的需要,并将很好地将该数组封装为函数的参数:

#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);
}
int sizeOfEmployee = sizeof(employee) / sizeof(employee[0]); 

不要这样做。如果要获取数组的长度,请使用 std::size(employee)

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

不要这样做。相反,使用 range-for 遍历范围内的所有元素:

for (auto&& e : employee)

顺便说一下,这将修复您遇到的错误。错误是您总是写入数组的第一个元素。每次迭代 over-writes 之前的元素和其他元素保持未初始化状态。

range-for 将正确地迭代所有元素,并减少出现混乱的可能性。

如果你想输入可变数量的员工,那么你应该使用STL 容器 std::vector。请看看它 documentation.

下面您可以找到 std::vector 对给定代码的用法以及一些 以标签 //CKE:.

开头的提示
#include <iostream>
#include <vector> //CKE: Very powerful STL container for arrays

using namespace std;

struct Employee {
   string name;
   int age = INT_MIN; //CKE: Always initialize variables in C++
   float salary = 0.; //CKE: Always initialize variables in C++
};

void displayData(const Employee&); //CKE: Use const reference if possible

int main() {

   vector<Employee> employees; //CKE: Declaration of dynamic array in C++
   size_t counter = 0;         //CKE: Counts number of given data sets
   string addOneMore;          //CKE: Holds check for more input data
   do {
       ++counter;
       Employee employee;
      cout << "Enter name for employee " << counter << ": ";
      cin >> employee.name;
      cout << "Enter age for employee " << counter << ": ";
      cin >> employee.age;
      cout << "Enter salary for employee " << counter << ": ";
      cin >> employee.salary;
      employees.push_back(employee); //CKE: Save data in dynamic array
      cout << "Want to add another employee? (y/n): ";
      cin >> addOneMore;
   } while (addOneMore == "y");

   for (const auto& employee : employees) { //CKE: Use range base loop for output
      displayData(employee);
   }
   return 0; //CKE: Always return an integer in int main function of C++
}

void displayData(const Employee& employee) { //CKE: No copying of struct Employee
   cout << "DISPLAYING INFORMATION" << endl;
   cout << "Name: " << employee.name << endl;
   cout << "Age: " << employee.age << endl;
   cout << "Salary: " << employee.salary << endl;
}

我只是通过使用循环计数器解决了我的问题:

#include <iostream>

using namespace std;

struct Employee {

   string name;
   int age;
   float salary;

};

void displayData(Employee[], int);

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 << ": ";
       getline(cin, employee[i].name);
       cout << "Enter age for employee " << i + 1 << ": ";
       cin >> employee[i].age;
       cout << "Enter salary for employee " << i + 1 << ": ";
       cin >> employee[i].salary;     
       cin.ignore();  
   }

   displayData(employee, sizeOfEmployee);


}

void displayData(Employee employee[], int sizeOfEmployee) {

   cout << "DISPLAYING INFORMATION" << endl;

   for(int i = 0; i < sizeOfEmployee; i++) {
       cout << "Name: " << employee[i].name << endl;
       cout << "Age: " << employee[i].age << endl;
       cout << "Salary: " << employee[i].salary << endl;
   }

}

感谢所有帮助过我并为我提供答案的人。非常感谢,因为我还在学习:))