使用结构体打印员工详情的 C 语言程序
本文中,我们给定一个包含员工详细信息的结构体。我们的任务是创建一个 C 语言程序,使用结构体来打印员工详细信息。
位于结构体中的员工详细信息为姓名、年龄、电话号码、工资,我们的程序将打印这些详细信息。
员工的详细信息在程序中预先声明,我们将逐个打印所有值。为此,我们将创建一个函数来访问结构体的对象,然后使用该对象来打印结构体中的所有成员。
使用结构体打印员工详细信息的 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<<"
--- Employee "<<n+1<<" ----
"; cout<<"Employee ID: "<<emp[n].empId<<endl; cout<<"Employee name: "<<emp[n].name<<endl; cout<<"Employee age:"<<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 Employee age: 22 Employee phone number: 942135439 Employee salary: 100000 --- Employee 2 ---- Employee ID: 210 Employee name: Ramesh Employee age: 41 Employee phone number: 908564363 Employee salary: 50000 --- Employee 3 ---- Employee ID: 19 Employee name: Yash Employee age: 45 Employee phone number: 943299231 Employee salary: 250000 --- Employee 4 ---- Employee ID: 74 Employee name: Zarin Employee age: 35 Employee phone number: 796892522 Employee salary: 300000
广告