将学生信息初始化,另一个学生信息从键盘输入,交换两个学生的信息,再输出交换后两个学生的信息
时间: 2023-05-22 13:07:05 浏览: 39
这是一道编程题,可以用以下代码实现:
```
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
float score;
};
void swapStudent(Student &stu1, Student &stu2) {
Student temp = stu1;
stu1 = stu2;
stu2 = temp;
}
void printStudent(Student stu) {
cout << "姓名:" << stu.name << endl;
cout << "年龄:" << stu.age << endl;
cout << "分数:" << stu.score << endl;
}
int main() {
// 初始化学生信息
Student stu1 = {"Tom", 18, 90.0f};
Student stu2 = {"Jerry", 20, 88.5f};
// 从键盘输入另一个学生信息
Student stu3;
cout << "请输入学生姓名:";
cin >> stu3.name;
cout << "请输入学生年龄:";
cin >> stu3.age;
cout << "请输入学生成绩:";
cin >> stu3.score;
// 交换两个学生信息
swapStudent(stu1, stu3);
// 输出交换后的两个学生信息
cout << "交换后,学生1的信息为:" << endl;
printStudent(stu1);
cout << "交换后,学生2的信息为:" << endl;
printStudent(stu2);
return 0;
}
```
这样,当你输入第三个学生信息后,会将第一个学生(stu1)的信息和第三个学生(stu3)的信息进行交换,再输出stu1和stu2的信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)