输入学生成绩代码c++
时间: 2023-07-09 09:17:51 浏览: 67
下面是一个简单的 C++ 代码,用于输入 5 个学生的成绩,并计算它们的平均分数:
```c++
#include <iostream>
using namespace std;
int main()
{
float grade1, grade2, grade3, grade4, grade5;
float average;
cout << "请输入 5 个学生的成绩:";
cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5;
average = (grade1 + grade2 + grade3 + grade4 + grade5) / 5;
cout << "这 5 个学生的平均成绩为:" << average << endl;
return 0;
}
```
在这个程序中,我们首先声明了 6 个变量:5 个分别用于存储每个学生的成绩,以及一个用于存储平均分数。然后,我们使用 `cout` 语句要求用户输入 5 个成绩,然后使用 `cin` 语句将这些成绩存储在相应的变量中。接下来,我们计算这些成绩的平均值,并将结果存储在 `average` 变量中。最后,我们使用 `cout` 语句输出平均分数。
相关问题
学生成绩查询c++源代码
以下是一个简单的学生成绩查询的C++源代码示例。该程序可以让用户输入学生的姓名和成绩,然后查询指定学生的成绩。
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Student {
string name;
int score;
};
int main() {
vector<Student> students;
// 添加学生信息
students.push_back({"Tom", 80});
students.push_back({"Jerry", 90});
students.push_back({"Alice", 85});
students.push_back({"Bob", 75});
// 查询学生成绩
string name;
cout << "请输入要查询的学生姓名:" << endl;
cin >> name;
bool found = false;
int score = 0;
for (const auto& student : students) {
if (student.name == name) {
found = true;
score = student.score;
break;
}
}
if (found) {
cout << name << " 的成绩是:" << score << endl;
} else {
cout << "找不到学生:" << name << endl;
}
return 0;
}
```
在这个示例程序中,我们使用了一个结构体 `Student` 来表示学生的信息,包括姓名和成绩。我们使用了一个 `vector` 容器来存储所有的学生信息,并在程序中添加了一些示例数据。
在查询学生成绩时,我们要求用户输入要查询的学生姓名,然后遍历所有的学生信息,查找姓名匹配的学生,并输出其成绩。如果找不到匹配的学生,则输出相应的提示信息。
学生成绩管理系统c++代码
以下是一个简单的学生成绩管理系统的C++代码示例:
```cpp
#include <iostream>
#include <string>
using namespace std;
struct Student {
string id;
string name;
float scores[3];
};
void inputStudentInfo(Student& student) {
cout << "请输入学生的学号:";
cin >> student.id;
cout << "请输入学生的姓名:";
cin >> student.name;
cout << "请输入学生的三门课程成绩:" << endl;
for (int i = 0; i < 3; i++) {
cout << "请输入第" << i + 1 << "门课程成绩:";
cin >> student.scores[i];
}
}
void outputStudentInfo(const Student& student) {
cout << "学生的学号:" << student.id << endl;
cout << "学生的姓名:" << student.name << endl;
cout << "学生的三门课程成绩:" << endl;
for (int i = 0; i < 3; i++) {
cout << "第" << i + 1 << "门课程成绩:" << student.scores[i] << endl;
}
}
int main() {
Student student;
inputStudentInfo(student);
cout << endl;
outputStudentInfo(student);
return 0;
}
```