希望可以帮忙找一下问题,如果代码不够详细体现问题可以私我,明天晚上19点前问题始终有效,16日19点后问题我会取消,希望可以收到解决方案
自己做的“班级学期成绩管理系统”,程序功能已写完,但在运行时有时会出现
也有时是
正常时占一半
类中成员变量的定义和成员函数的声明如下(相关功能的实现省略了)头文件:
#include "Configure.h"
#define FILENAME "lecture_grade.txt"
#define FILENAME2 "lecture.txt"
using uint = unsigned int;
class Management
{
public:
//ctor
Management();
//de_ctor
virtual ~Management();
void menu();//菜单
void choose_menu();//选择菜单
int seek_student();
int number_seek(int number);
int name_seek(string name);
//统计文件中的学生数量
int get_stu();
//统计列数
int get_line();
//统计文件中的科目数量
int get_sub();
//初始化学生
void init_stu();
//初始化科目
void init_sub();
void test();
public:
//读取表头
string m_header;
int datarow = 0;//学生人数&数据行数
int dataline = 0;//文件列数
int subsum = 0;//学科数目
vector<string> stu_header;//存放成绩表头
uint* numberarr = new uint[datarow];//存放学号
vector<string> namearr;//存放姓名
int** dataarr = nullptr;//存放成绩
double* GPA = new double[datarow];//存放学分绩点
//Subject* subarr;//学科数组指针
vector<string> sub_header;//课程表头
vector<string> sub_name;//课程名
vector<double> sub_credit;//课程学分
vector<string> sub_type;//课程类型
bool m_FileIsEmpty;//判断文件是否为空 标志
};
类中的构造、析构函数与文件读取部分如下(相关功能的实现太长省略了)cpp文件:
```c++
Management::Management()
{
int num = this->get_stu();
int num2 = this->get_line();
int sum = this->get_sub();
//cout << "职工人数为: " << num << endl;
//cout << "学科数目为: " << sum << endl;
this->datarow = num;//成绩行数
this->dataline = num2;//成绩列数
this->subsum = sum;//课程行数
//数据指针
dataarr = new int* [datarow];
for (int i = 0; i < datarow; i++)
{
dataarr[i] = new int[dataline];
}
//将文件中的数据 ,存到数组中
this->init_stu();
this->init_sub();
}
Management::~Management()
{
delete[] numberarr;
numberarr = nullptr;
for (int i = 0; i < datarow; i++)
{
delete[] dataarr[i];
dataarr[i] = nullptr;
}
delete[] dataarr;
dataarr = nullptr;
delete[] GPA;
GPA = nullptr;
stu_header.clear();
namearr.clear();
sub_header.clear();
sub_name.clear();
sub_credit.clear();
sub_type.clear();
}
void Management::choose_menu()
{
int n;//选项
while (true)
{
//menu();//菜单界面
cin >> n;
switch (n)
{
case 0:
exit(0);
break;
case 1:
//look_grade();
break;
case 2:
//count_score();
break;
case 3:
//cout_file();
break;
case 4:
//look_record();
break;
case 5:
//modify();
break;
case 6:
//count_grades();
break;
default:
system("cls");
}
system("pause");
system("cls");
}
}
//成绩行数
int Management::get_stu()
{
ifstream ifs;
ifs.open(FILENAME, ios::in); //打开文件 读
int num = 0;
string temp;
while (getline(ifs, temp))
{
num++;
}
return num - 1;
}
//成绩列数
int Management::get_line()
{
ifstream readFile(FILENAME);
int line = 0;
string str, word;
getline(readFile, str);
stringstream temp_str(str);
while (temp_str >> word)
{
++line;
}
return line - 2;
}
//课程行数
int Management::get_sub()
{
fstream file;
file.open(FILENAME2, ios::in);
int n = 0;
while (!file.eof())
{
string temp;
getline(file, temp);
n++;
}
return n - 1;
}
//初始化学生
void Management::init_stu()
{
fstream ifs;
ifs.open(FILENAME, ios::in);
char temp[1024];
ifs.getline(temp, 1024);//读表头
m_header = temp;
stringstream head(temp);
for (size_t i = 0; i < dataline + 2; i++)
{
string header;
head >> header;
stu_header.push_back(header);
}
int index = 0;
while (!ifs.eof())//判断文件是否读取完
{
char data[1024];
ifs.getline(data, 1024);
//跳过空行
if (strlen(data) == 0)
break;
stringstream ss(data);
ss >> numberarr[index];
string name;
ss >> name;
namearr.push_back(name);
//namearr[index] = sztemp;
for (int i = 0; i < dataline; i++)
{
int num;
ss >> num;
dataarr[index][i] = num;
}
index++;
}
//关闭文件
ifs.close();
}
//初始化科目
void Management::init_sub()
{
fstream file;
file.open(FILENAME2, ios::in);
char temp[1024];
file.getline(temp, 1024);//读表头
stringstream head(temp);
string header;
head >> header;
sub_header.push_back(header);
while (!file.eof())
{
char data[1024];
file.getline(data, 1024);
stringstream sb(data);
string name;
double credit;
string type;
sb >> name >> credit >> type;
sub_name.push_back(name);
sub_credit.push_back(credit);
sub_type.push_back(type);
}
file.close();
}
补充:调试时错误常显示
1.
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/859575197376145.png "#left")
2.
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/31474619737613.png "#left")
3.
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/686896197376181.png "#left")
在创建可视化窗口时
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/723597197376110.png "#left")