用c++ 编程有三个学生的信息(包含年龄,姓名,成绩) ,输出分数最高的学生的所有信息
时间: 2024-01-03 11:03:44 浏览: 32
好的,以下是一个用 C++ 编写的程序,用于输出三个学生的信息,并找出分数最高的学生的所有信息。
```c++
#include<iostream>
#include<string>
using namespace std;
struct Student{ // 定义一个结构体,用于存储学生的信息
string name;
int age;
int score;
} stu[3];
int main(){
// 输入三个学生的信息
for(int i=0; i<3; i++){
cout<<"请输入第"<<i+1<<"个学生的姓名、年龄和成绩:"<<endl;
cin>>stu[i].name>>stu[i].age>>stu[i].score;
}
// 找到分数最高的学生
int max_score = stu[0].score;
int max_index = 0;
for(int i=1; i<3; i++){
if(stu[i].score > max_score){
max_score = stu[i].score;
max_index = i;
}
}
// 输出分数最高的学生的所有信息
cout<<"分数最高的学生的信息为:"<<endl;
cout<<"姓名:"<<stu[max_index].name<<endl;
cout<<"年龄:"<<stu[max_index].age<<endl;
cout<<"成绩:"<<stu[max_index].score<<endl;
return 0;
}
```
上述程序通过定义一个结构体 `Student`,用于存储每个学生的姓名、年龄和成绩。然后通过一个 for 循环输入三个学生的信息。接着,通过一个循环找到分数最高的学生,最后输出分数最高的学生的所有信息。