结构体输出两个学生信息C++
时间: 2024-04-27 11:22:33 浏览: 7
好的,下面是一个简单的示例代码,展示如何定义一个包含学生信息的结构体,并输出两个学生的信息:
```c++
#include <iostream>
#include <string>
using namespace std;
// 定义学生结构体
struct Student {
string name;
int age;
string gender;
double score;
};
int main() {
// 初始化两个学生结构体
Student stu1 = {"Tom", 18, "Male", 89.5};
Student stu2 = {"Lucy", 20, "Female", 95.0};
// 输出学生信息
cout << "Student 1: " << endl;
cout << "Name: " << stu1.name << endl;
cout << "Age: " << stu1.age << endl;
cout << "Gender: " << stu1.gender << endl;
cout << "Score: " << stu1.score << endl;
cout << "Student 2: " << endl;
cout << "Name: " << stu2.name << endl;
cout << "Age: " << stu2.age << endl;
cout << "Gender: " << stu2.gender << endl;
cout << "Score: " << stu2.score << endl;
return 0;
}
```
输出结果如下:
```
Student 1:
Name: Tom
Age: 18
Gender: Male
Score: 89.5
Student 2:
Name: Lucy
Age: 20
Gender: Female
Score: 95
```