在这里,我们给出了一个包含员工详细信息的结构。我们的任务是创建一个C程序,以编程方式使用结构打印员工详细信息。
结构中的员工详细信息包括姓名,年龄,号码,薪水,我们的程序将打印这些详细信息。
员工的详细信息已在程序中预先声明,我们将一一打印所有值。为此,我们将创建一个函数,该函数将访问结构的对象,然后使用该对象打印结构的所有成员。
// C程序使用结构打印员工详细信息-
#include <iostream> using namespace std; struct employee { int empId; string name; int age; string phone_number; int salary; }; void displayDeitals(struct employee emp[], int n) { cout<<"\n\n --- Employee "<<n+1<<" ---- \n"; cout<<"Employee ID: "<<emp[n].empId<<endl; cout<<"Employee name: "<<emp[n].name<<endl; cout<<"员工年龄:"<<emp[n].age<<endl; cout<<"Employee phone number: "<<emp[n].phone_number<<endl; cout<<"Employee salary : "<<emp[n].salary<<endl; } int main() { int n = 3; struct employee emp[4]; emp[0].empId = 0121; emp[0].name = "Nupur"; emp[0].age = 22; emp[0].phone_number = "942135439"; emp[0].salary = 100000; emp[1].empId = 0322; emp[1].name = "Ramesh"; emp[1].age = 41; emp[1].phone_number = "908564363"; emp[1].salary = 50000; emp[2].empId = 023; emp[2].name = "Yash"; emp[2].age = 45; emp[2].phone_number = "943299231"; emp[2].salary = 250000; emp[3].empId = 0112; emp[3].name = "Zarin"; emp[3].age = 35; emp[3].phone_number = "796892522"; emp[3].salary = 300000; for(int i= 0; i<=n; i++) displayDeitals(emp, i); return 0; }
输出结果
--- Employee 1 ---- Employee ID: 81 Employee name: Nupur 员工年龄: 22 Employee phone number: 942135439 Employee salary: 100000 --- Employee 2 ---- Employee ID: 210 Employee name: Ramesh 员工年龄: 41 Employee phone number: 908564363 Employee salary: 50000 --- Employee 3 ---- Employee ID: 19 Employee name: Yash 员工年龄: 45 Employee phone number: 943299231 Employee salary: 250000 --- Employee 4 ---- Employee ID: 74 Employee name: Zarin 员工年龄: 35 Employee phone number: 796892522 Employee salary: 300000