#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class student //基类,考生信息
{
public:
virtual ~student() {};
friend int menu();
int seat[100]{}; //座位号
int num[100]{}; //学号
string name[100]{}; //姓名
};
class stuscore :public student/*派生类1,学生成绩和考试科目信息*/
{
public:
~stuscore() {};
string lname[100]{}; //学科名
int score[100]{}; //成绩
int lesson_num{}; //学科数目
float sum{}; //总分
float ave{}; //平均分
}stu[100];
class stuwenjian :public stuscore/*派生类2,学生信息*/
{
public:
friend void Write(int, int);
};
class stucaozuo :public stuwenjian/*派生类3*/
{
public:
friend void Input(int, int);
};
/*写入操作只能够覆盖式写入*/
void Write(int n, int lesson_num)/*写入操作*/
{
fstream myFile;
myFile.open("成绩.txt", ios::out | ios::binary);//以二进制,只写入
if (!myFile)
{
cout << "成绩.txt can't open!" << endl; abort();
}//不存在score.txt文件时
myFile << n << " " << lesson_num << endl << endl;
myFile << "学生成绩管理系统" << '\n' << "座位号" <<'\t' << '\t' << "学号" << '\t' << "姓名" << '\t';
for (int k = 0; k < lesson_num; k++)
{
myFile << stu[n].lname[k] << '\t';
}//循环写入科目名称
myFile << endl;
for (int i = 0; i < n; i++)//循环以写入学生信息及成绩
{
myFile
<< stu[n + i].seat << "\t"
<< stu[n + i].num << "\t"<<"\t"
<< stu[n + i].name << "\t";
for (int j = 0; j < lesson_num; j++)
myFile << stu[n + i].score[j] << "\t";
myFile << endl;
}
myFile.close();
}
void Input(int n, int lesson_num)
{
system("cls");
cout << "=======>>请输入考试科目数量:";
cin >> lesson_num;
cout << "=======>>请输入考试科目名称(用空格隔开):";
for (int k = 0; k < lesson_num; k++)
{
cin >> stu[n].lname[k];
}
system("cls");
int i = 0;
char sign = '\0';
cout << endl
<< "======>> 请输入学生信息及成绩 <<======" << endl;
while (sign != 'n' && sign != 'N')
{
cout << "===>>请输入学生的信息" << endl;
cout << "座位号:";
cin >> stu[n + i].seat[i];
loop:
cout << "学号:";
cin >> stu[n + i].num[i];
int c = 0;
while (c < i)
{
c++;
if (stu[n + i].num == stu[n + i - c].num) //链表查询判断
{
cout << "您输入的学号已存在!请重新输入。" << endl;
goto loop;
}
}
cout << "姓名:";
cin >> stu[n + i].name[i];
for (int j = 0; j < lesson_num; j++)//循环输入学生成绩
{
cout << "请输入该学生的科目【" << stu[n].lname[j] << "】的成绩:" << endl;
cin >> stu[n + i].score[j];
}
cout << "======>>提示:继续写入成绩?(y/n)";
cin >> sign;//判断
i++;
}Write(n, lesson_num);//写入文件中
}
int menu() //菜单界面
{
char c;
do {
system("cls");
cout << "******************************************************" << endl;
cout << "----------------欢迎使用学生成绩管理系统---------------" << endl;
cout << " * 【1】输入学生成绩 * " << endl;
cout << " * 【2】显示统计数据 * " << endl;
cout << " * 【3】查找学生成绩 * " << endl;
cout << " * 【4】修改学生成绩 * " << endl;
cout << " * 【5】删除学生成绩 * " << endl;
cout << " * 【6】插入学生成绩 * " << endl;
cout << " * 【7】按平均分排列 * " << endl;
cout << " * 【8】显示学生成绩 * " << endl;
cout << " * 【0】退出管理系统 * " << endl;
cout << "******************************************************" << endl;
cout << "请选择您的操作 (0-8):" << endl;
c = getchar();
} while (c < '0' || c > '8');
return (c - '0');
}
int main(int n,int lesson_num)
{
for (;;)
{
switch (menu()) {
case 1:
Input(n,lesson_num);
system("cls");
break;
case 0:
cout << endl << "退出成功";
exit(0);
}
}
return 0;
}
上面的代码是没有报错的,就是不能写入多个学生的信息、成绩之类的,麻烦各位大神帮帮忙看看。
如果可以的话说说要怎么改才行。
跪谢!!
myFile.open("成绩.txt", ios::out | ios::binary);
改为:
myFile.open("成绩.txt", ios::out | ios::binary | ios::app);
试试