显示数组中的学生 ID 和最高分
Display Student ID and highest score from an array
我在弄清楚如何正确显示学生的 ID 以及 10 名学生中的最高分时遇到了问题。对于考试,如果学生 4 的分数最高,则会显示该学生的 ID 和他们的分数。如果可能的话,我还想添加学生的名字和姓氏。我的代码如下:
// HW2.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include <iostream>
using namespace std;
double TestScore(double score);
struct studentType {
string studentFName;
string studentLName;
double testScore;
int studentID;
double highScore;
};
int main()
{
// # of students
studentType student[10];
// For loop to get user input for the 10 students
for (int i = 0; i < 10; i++) {
cout << "Student ID: ";
cin >> student[i].studentID;
cout << "Student First Name: ";
cin >> student[i].studentFName;
cout << "Student Last Name: ";
cin >> student[i].studentLName;
cout << "Student's Test Score: ";
cin >> student[i].testScore;
cout << endl;
//Calls TestScore function
student[i].testScore = TestScore(student[i].testScore);
}
//Displays student ID and score v code that I need help on
//cout <<student[i].studentID << " has the highest score, which is "<< TestScore;
}
double TestScore(double score)
{
double newScore = 0;
//Determines student with highest score
for (int n = 0; n < 10; n++) {
if (score > newScore)
{
newScore = score;
}
}
return newScore;
}
它需要: 1(将学生的数据读入数组。 2(找到最高的考试分数。 3(打印考试成绩最高的学生姓名。
你的TestScore
函数实际上什么都不做。我已经修改了您的代码,使其具有一个函数,该函数返回得分最高的学生的索引。然后,使用此索引可以访问数组的该元素,并打印出其详细信息。
#include <iostream>
using namespace std;
struct studentType {
string studentFName;
string studentLName;
double testScore;
int studentID;
double highScore;
};
int getBestStudent( studentType student[10] );
int main() {
// # of students
studentType student[10];
// For loop to get user input for the 10 students
for ( int i = 0; i < 10; i++ ) {
cout << "Student ID: ";
cin >> student[i].studentID;
cout << "Student First Name: ";
cin >> student[i].studentFName;
cout << "Student Last Name: ";
cin >> student[i].studentLName;
cout << "Student's Test Score: ";
cin >> student[i].testScore;
cout << endl;
}
// Displays student ID and score v code that I need help on
int best = getBestStudent( student );
cout <<student[best].studentFName << " " << student[best].studentLName << " has the highest score, which is "<< student[best].testScore;
}
int getBestStudent( studentType student[10] ) {
int best = 0;
//Determines student with highest score
for ( int n = 1; n < 10; n++ ) {
if ( student[n].testScore > student[best].testScore ) {
best = n;
}
}
return best;
}```
相关文章:
- 从函数角度看ID到文件路径的内部与外部映射
- 通过组合不同的类型来创建唯一的id
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- FFMPEG配置文件级别id大小无效
- 方法内部但循环仍得到预期的不合格id错误C++
- 如何获取 GLFW 窗口 ID?
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 显示数组中的学生 ID 和最高分
- C++:"("令牌"之前有预期的非限定 id 指向类中成员函数的指针
- 在PostgreSQL中根据它们的ID选择大量行的最快方法是什么?
- Xcode 9.4.1 中的 Apple Mach-O 链接器 (id) 错误
- 错误:令牌 { '{' 之前应存在非限定 ID
- 断言"id < 0"在Qt ActiveX中失败
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- 如何根据两个因素组织向量:id 和数量?(C++)
- 在返回 0 之前应为非限定 ID
- Direct3D 11 - HLSL - 获取顶点索引 ID
- 编译时检查特征专用化是否具有唯一 ID
- DCMTK 了解"DIMSE 没有有效的演示上下文 ID"错误
- 一种有效的数据结构,用于按 ID 访问和查找加权随机项