C++ 未通过引用传递另一个结构内部的结构数组的成员变量
这真是让我大吃一惊 我有两个结构,员工和部门。department结构中有一个employee结构数组 调用将员工添加到部门的函数(add_emply_to_dept())时,值会正确保存到部门的员工数组(emply_in_dept[]),但仅保存在函数的范围内。一旦程序返回main,值就会丢失 为什么会发生这种情况?如果能够解决,该如何解决 抱歉,这里的代码太长了。复制/粘贴整个东西比把它拆开要容易得多。如果这是个问题,请告诉我 谢谢你的帮助C++ 未通过引用传递另一个结构内部的结构数组的成员变量,c++,arrays,struct,C++,Arrays,Struct,这真是让我大吃一惊 我有两个结构,员工和部门。department结构中有一个employee结构数组 调用将员工添加到部门的函数(add_emply_to_dept())时,值会正确保存到部门的员工数组(emply_in_dept[]),但仅保存在函数的范围内。一旦程序返回main,值就会丢失 为什么会发生这种情况?如果能够解决,该如何解决 抱歉,这里的代码太长了。复制/粘贴整个东西比把它拆开要容易得多。如果这是个问题,请告诉我 谢谢你的帮助 #include <iostream>
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
struct employee
{
int eID;// stores the employee ID number
char fName[40]; //stores the employee's first
char lName[40];//stores the employee's last name
float salary;//stores the employee's salary
};
// structure for storing department information
struct department
{
int dID;//stores the department's ID number
char dName[40];//stores the name of the department
int empl_in_dept_count;// stores how many employees have been added to the department
employee empl_in_dept[10];//stores upto 10 employees
};
bool dCheck(department dList[], int dCount, int dID);
bool eCheck(employee eList[], int eCount, int eID);
bool empl_in_dept_check(department dList[], int dCount, employee eList[], int eID, int dID);
void addDepartment(department dList[], int &dCount);
void addEmployee(employee eList[], int &eCount);
void add_empl_to_dept(department dList[], int dCount, employee eList[], int eCount);
void ePrint(employee eList[], int eCount);
void dPrint(department dList[], int dCount, employee eList[]);
void e_in_dPrint(department dList[], int dCount, employee eList[]);
void average(department dList[], int dCount, employee eList[]);
void save(department dList[], int dCount, employee eList[], int eCount);
void load(department dList [], int &dCount, employee eList[], int &eCount);
void printMenu();
int main()
{
employee eList[30];//stores individual employee information in each index
int eCount = 0; //stores total number of employees added. Used as an index number for the eList array when adding employees
department dList[10];//stores the individual department information in each index.
int dCount = 0; //stores total number of departments added. Used as an index number for the dList array when adding departments
//initialize all dList's empl_in_dept_count variables to zero
for (int i = 0; i < 10; i++)
{
dList[i].empl_in_dept_count = 0;
}
//do-while loop based on the bool variable, run. When run = false, the program ends.
do
{
printMenu();//prints out the menu and prompts the user for a choice.
do// check for a valid menu choice
{
cin >> menu;//reads in the user's menu choice
cout << "----------------------------------------------------------" << endl;
if (menu <1 || menu > 10)
{
cout << "That is not a valid selection." << endl;
printMenu();//prints out the menu and prompts the user for a choice.
}
}while(menu <1 || menu > 10);
//switch statement based on the variable, menu.
switch(menu)
{
case 1://calls the addDepartment function
addDepartment(dList, dCount);
break;
case 2://calls the addEmployee function
addEmployee(eList, eCount);
break;
case 3://calls the add_empl_to_dept function
add_empl_to_dept(dList, dCount, eList, eCount);
break;
case 4://calls the ePrint function
ePrint(eList, eCount);
break;
case 5://calls the dPrint function
dPrint(dList, dCount, eList);
break;
case 6://calls the e_in_dPrint function
e_in_dPrint(dList, dCount,eList);
break;
case 7://calls the average function
average(dList, dCount, eList);
break;
case 8://calls the save function
save(dList, dCount, eList, eCount);
break;
case 9://calls the load function
load(dList, dCount, eList, eCount);
break;
case 10://sets run to false and ends the program
run = false;
break;
}
}while(run);
return 0;
#包括
#包括
#包括
使用名称空间std;
结构雇员
{
int eID;//存储员工ID号
char fName[40];//存储员工的第一个
char lName[40];//存储员工的姓氏
浮动工资;//存储员工的工资
};
//存储部门信息的结构
结构部门
{
int dID;//存储部门的ID号
char dName[40];//存储部门名称
int emply\u in\u dept\u count;//存储已添加到该部门的员工数量
部门中的员工人数[10];//最多可存储10名员工
};
bool dCheck(部门数据列表[],内部数据账户,内部数据账户);
bool eCheck(员工列表[],内部账户,内部eID);
员工入部门检查(部门数据列表[],内部数据账户,员工列表[],内部eID,内部dID);
无效添加部门(部门数据列表[],内部和数据账户);
无效添加员工(员工列表[],内部和电子账户);
作废将员工添加到部门(部门数据列表[],内部数据账户,员工列表[],内部数据账户);
无效ePrint(员工列表[],内部账户);
无效数据列表(部门数据列表[],内部数据帐户,员工列表[]);
作废e_in_dPrint(部门数据列表[],内部数据账户,员工列表[]);
无效平均值(部门数据列表[]、内部数据帐户、员工列表[]);
作废保存(部门数据列表[],内部数据帐户,员工列表[],内部电子帐户);
无效负载(部门数据列表[],内部和数据帐户,员工列表[],内部和数据帐户);
作废打印菜单();
int main()
{
员工列表[30];//在每个索引中存储单个员工信息
int ecoount=0;//存储添加的员工总数。添加员工时用作eList数组的索引号
department dList[10];//在每个索引中存储各个部门的信息。
int dCount=0;//存储添加的部门总数。添加部门时用作dList数组的索引号
//将所有数据列表的emp\u in\u dept\u count变量初始化为零
对于(int i=0;i<10;i++)
{
数据列表[i]。部门中的员工计数=0;
}
//基于bool变量run执行while循环。当run=false时,程序结束。
做
{
printMenu();//打印出菜单并提示用户进行选择。
执行//检查是否有有效的菜单选项
{
cin>>菜单;//读取用户的菜单选项
cout您正在将数据列表作为值传递,需要通过引用或使用指针来传递它。请尝试:
使用指针:
void addDepartment(department *dList, int &dCount)
通过引用:
void addDepartment(department &dList[], int &dCount)
过度渲染和非功能性代码。将您的代码砍成一个main
,调用add_emp\u to_dept
丢弃其余部分,然后让我们看看您的情况。欢迎使用Stack Overflow。将代码缩减为a不仅仅是为了我们的方便,它将帮助您自己发现错误。这是一项至关重要的技能,比任何一个错误都有价值ix.我听说你:)。我知道这太难发布了。现在就把它砍掉:)@nonreature0714它是通过指针传递的,这意味着函数可以修改它。你的第二个建议无法编译。你的第一个建议与OP的代码完全相同(在函数参数列表中,tx[]
表示tx
)。
void addDepartment(department &dList[], int &dCount)