把一个班的学生姓名和成绩存放到一个结构数组中,输出分数最高的学生信息c++
时间: 2024-03-03 08:49:58 浏览: 26
好的,以下是代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
const int MAX_STUDENTS = 100; // 最多学生数
struct Student {
string name; // 姓名
int score; // 成绩
};
int main() {
Student students[MAX_STUDENTS];
int n; // 学生人数
cout << "请输入学生人数:";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "请输入第" << i+1 << "个学生的姓名和成绩:";
cin >> students[i].name >> students[i].score;
}
// 找到分数最高的学生
int maxIndex = 0;
for (int i = 1; i < n; i++) {
if (students[i].score > students[maxIndex].score) {
maxIndex = i;
}
}
// 输出分数最高的学生信息
cout << "分数最高的学生信息为:" << endl;
cout << "姓名:" << students[maxIndex].name << endl;
cout << "成绩:" << students[maxIndex].score << endl;
return 0;
}
```
以上代码中,我们使用了结构体数组 `students` 存放学生的姓名和成绩,然后通过遍历数组找到分数最高的学生,最后输出该学生的信息。