问题遇到的现象和发生背景
题目如下:就是说,为什么我输入10个人的信息后,没有按降序排列?
定义一个学生类,包含基本学生的基本信息属性,学号、姓名、年龄性别。构造函数实现初始化。缺省初始化学号:-1、姓名:"None"、年龄:0,性别:'M'。成员函数包括设置和限时基本信息。定义继承学生类一个派生类包括属性为:所学专业及四门门课的成绩,并实现构造函数、析构函数及计算总分平均分的成员函数。
要求定义10个人的对象数组,完成输入数据,实现总分和平均分的计算,按以总分降序排序后将学生的属性:学号、姓名、年龄、性别、专业、四门课成绩、总分、平均分存入文本文件(或二进制文件,自选,文件名是学号后四位.dat)。
遇到的现象和发生背景,请写出第一个错误信息
为什么第二个for循环没有运行?
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
class Student {
public:
int id;
string name;
int age;
char sex;
Student(int m_id = -1, string m_name = "None", int m_age = 0, char m_sex = 'M') :
id(m_id),
name(m_name),
age(m_age),
sex(m_sex)
{}
};
class Score {
public:
int math;
int English;
int Chinese;
int art;
int add() {
return math + English + Chinese + art;
}
double average() {
return (math + English + Chinese + art) / 4;
}
};
class A :public Student {
public:
string major;
Score sc;
A() {};
A(string m_major, Score m_sc) {
major = m_major;
sc = m_sc;
}
~A() {
};
};
int main() {
A temp;
A a[10];
for (int i = 0; i < 10; i++) {
cout << "第" << i + 1 << "个人的学号、姓名、年龄、性别、专业、四门课成绩为:" << endl;
cin >> a[i].id >> a[i].name >> a[i].age >> a[i].sex >> a[i].major
>> a[i].sc.art >> a[i].sc.math >> a[i].sc.English >> a[i].sc.Chinese;
cout << "第" << i + 1 << "个人总分为:" << a[i].sc.add() << " 平均分为:" << a[i].sc.average();
cout << endl;
}
for (int i = 0; i < 9; i++) {
for (int j = i + 1; j < 10; j++) {
if (a[j].sc.add() > a[i].sc.add())
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
for (int i = 0; i < 10; i++)
{
ofstream ofs;
ofs.open("0001.dat", ios::out);
ofs << "第"<<i+1<<"个人学号为:"<< a[i].id<<" 姓名:"<< a[i].name<<" 年龄:"<< a[i].age<<" 性别:"
<< a[i].sex<<" 专业:"<< a[i].major<<" 四门课成绩为:"<< a[i].sc.art << a[i].sc.math <<
a[i].sc.English << a[i].sc.Chinese;
ofs.close();
}
return 0;
}