有3个学生的信息,放在结构体数组中,要求输出全部学生的信息 {{10101,"Li Lin",'M',18},{10102,"Zhang Fun",'M',19},{10104,"Wang Min",'F',20}}。
时间: 2024-05-06 16:18:52 浏览: 82
好的,可以参考以下代码实现:
```cpp
#include <iostream>
using namespace std;
struct Student {
int id;
string name;
char gender;
int age;
};
int main() {
Student students[3] = {{10101,"Li Lin",'M',18},{10102,"Zhang Fun",'M',19},{10104,"Wang Min",'F',20}};
for (int i = 0; i < 3; i++) {
cout << "ID: " << students[i].id << endl;
cout << "Name: " << students[i].name << endl;
cout << "Gender: " << students[i].gender << endl;
cout << "Age: " << students[i].age << endl << endl;
}
return 0;
}
```
输出结果如下:
```
ID: 10101
Name: Li Lin
Gender: M
Age: 18
ID: 10102
Name: Zhang Fun
Gender: M
Age: 19
ID: 10104
Name: Wang Min
Gender: F
Age: 20
```
注意:结构体数组的初始化要使用花括号括起来,每个元素之间用逗号隔开。
阅读全文