下面是一个图书馆借阅管理系统,在菜单中选择1.录入新书或是6.录入新读者之后,txt文档中只会显示若干0,且无法正确通过4.显示所有书信息和9.显示所有读者信息来显示txt文档中的数据。请帮我看看问题在哪
代码如下
#include<iostream>
#include<fstream>
#include<string>
#define BOOKFILE "BookFile.txt"//存放图书信息的文档
#define BORROWERFILE "BorrowerFile.txt"//存放读者信息的文档
using namespace std;
//图书信息结构体
struct Book_struct
{
string m_book_name;//图书名称
int m_book_id;//图书编号
float m_book_price;//单价。考虑到单价不一定为整数,这里的数据类型使用浮点型
string m_author;//作者
bool m_loan;//存在状态。true表示状态为已经借出,标记为1;false表示未借出,可被借,标记为0
};
//读者信息结构体
struct Borrower_struct
{
string m_borrower_name;//借书人姓名
string m_sex;//性别
int m_borrower_id;//学号
};
//读者类
class Borrower
{
private:
int borrower_num;//统计已注册的读者的数量
Borrower** m_Borrower_Array;
bool m_BORROWERFILE_empty;
public:
Borrower_struct m_borrower;
//构造函数
Borrower()
{
//分三种情况
//第一种情况:文件不存在
ifstream ifs;
ifs.open(BORROWERFILE, ios::in | ios::binary); // 读文件
if (!ifs.is_open())
{
//初始化记录人数
this->borrower_num = 0;
//初始化数组指针
this->m_Borrower_Array = NULL;
//初始化文件是否为空
this->m_BORROWERFILE_empty = true;
ifs.close();
return;
}
//第二种情况:文件存在,数据为空
char ch;
ifs >> ch;//先读一个字符
if (ifs.eof())//如果读到的标志时eof,意思是读到头了,也就是数据为空的意思
{
//初始化记录人数
this->borrower_num = 0;
//初始化数组指针
this->m_Borrower_Array = NULL;
//初始化文件是否为空
this->m_BORROWERFILE_empty = true;
ifs.close();
return;
}
//3、文件存在,并且记录数据
int num = this->get_borrower_number();
//cout << "职工人数为: " << num << endl;
this->borrower_num = num;
//开辟空间
this->m_Borrower_Array = new Borrower * [this->borrower_num];
//将文件中的数据 ,存到数组中
this->Borrower_initialize();
}
//初始化读者
void Borrower_initialize()
{
ifstream ifs;
ifs.open(BORROWERFILE, ios::in | ios::binary);
Borrower_struct borrower_struct;
int index = 0;
while (ifs >> borrower_struct.m_borrower_name && ifs >> borrower_struct.m_sex && ifs >> borrower_struct.m_borrower_id)
{
Borrower* borrower = NULL;
borrower = new Borrower;
this->m_Borrower_Array[index] = borrower;
index++;
}
//关闭文件
ifs.close();
}
//新读者注册函数。另外注意,不能加const,不用管此处编译器的提示。一旦增加了const,会导致不能修改该类的成员变量borrower_num,然而这个函数需要做到能修改borrower_num
void set_borrower()
{
cout << "请输入要新增读者的数量" << endl;
int add_number = 0;
cin >> add_number;
if (add_number > 0)
{
//计算新空间的大小,其中,等号右边第一项是已经存储的书的数目,第二项是新增加的书的数目
int new_size = this->borrower_num + add_number;
//开辟新空间
Borrower** new_space = new Borrower * [new_size];
//将原空间下内容存放到新空间下
if (this->m_Borrower_Array != NULL)
{
for (int i = 0; i < this->borrower_num; i++)
{
new_space[i] = this->m_Borrower_Array[i];
}
}
//输入新数据
for (int i = 0; i < add_number; i++)
{
Borrower_struct new_borrower;
cout << "请输入第 " << i + 1 << " 位新读者的名字:" << endl;
cin >> new_borrower.m_borrower_name;
cout << "请输入第 " << i + 1 << " 位新读者的性别:" << endl;
cin >> new_borrower.m_sex;
cout << "请输入第 " << i + 1 << " 位新读者的学号:" << endl;
cin >> new_borrower.m_borrower_id;
Borrower* borrower = NULL;
borrower = new Borrower;
new_space[this->borrower_num + i] = borrower;
}
//释放原有空间
delete[] this->m_Borrower_Array;
//更改新空间的指向
this->m_Borrower_Array = new_space;
//更新读者数量
this->borrower_num = new_size;
//更新判断读者数量是否为空的标志为“不为空”
this->m_BORROWERFILE_empty = false;
//提示添加成功
cout << "成功添加" << add_number << "位新读者!" << endl;
//保存数据到文件中
this->save();
}
else
{
cout << "输入有误" << endl;
}
//按任意键后 清屏回到上级目录
system("pause");
system("cls");
}
//将信息写入文件
void save()
{
ofstream ofs;
ofs.open(BORROWERFILE, ios::out /*| ios::binary*/);//以二进制方式写文件
//将每个人数据写入到文件中
for (int i = 0; i < this->borrower_num; i++)
{
ofs << this->m_Borrower_Array[i]->m_borrower.m_borrower_name << " "
<< this->m_Borrower_Array[i]->m_borrower.m_sex << " "
<< this->m_Borrower_Array[i]->m_borrower.m_borrower_id << " " << endl;
}
//关闭文件
ofs.close();
}
//统计文件中的读者数量
int get_borrower_number()
{
ifstream ifs;
ifs.open(BORROWERFILE, ios::in | ios::binary);
Borrower_struct borrower;
int num = 0;
while (ifs >> borrower.m_borrower_name && ifs >> borrower.m_sex && ifs >> borrower.m_borrower_id)
{
//统计人数变量
num++;
}
return num;
}
//显示所有读者的信息函数中的show_one_borrower函数,该函数用于输出单个读者的信息,再通过for循环来调用该函数以输出全部读者信息
void show_one_borrower()
{
cout << "读者姓名: " << this->m_borrower.m_borrower_name
<< "\t性别: " << this->m_borrower.m_sex
<< "\t学号: " << this->m_borrower.m_borrower_id << endl;
}
//显示所有读者的信息
void show_all_borrower()
{
//判断文件是否为空
if (this->m_BORROWERFILE_empty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
for (int i = 0; i < borrower_num; i++)
{
//利用多态调用程序接口
this->m_Borrower_Array[i]->show_one_borrower();
}
}
//按任意键后清屏
system("pause");
system("cls");
}
};
//图书馆类(公有继承Borrower类)
class Library:public Borrower
{
private:
Book_struct m_book;
int book_num;//统计已录入的图书数量
Library** m_Library_Array;
bool m_BOOKFILE_empty;
public:
//构造函数
Library()
{
//分三种情况
//第一种情况:文件不存在
ifstream ifs;
ifs.open(BOOKFILE, ios::in | ios::binary); // 读文件
if (!ifs.is_open())
{
//初始化记录人数
this->book_num = 0;
//初始化数组指针
this->m_Library_Array = NULL;
//初始化文件是否为空
this->m_BOOKFILE_empty = true;
ifs.close();
return;
}
//第二种情况:文件存在,数据为空
char ch;
ifs >> ch;//先读一个字符
if (ifs.eof())//如果读到的标志时eof,意思是读到头了,也就是数据为空的意思
{
//初始化记录人数
this->book_num = 0;
//初始化数组指针
this->m_Library_Array = NULL;
//初始化文件是否为空
this->m_BOOKFILE_empty = true;
ifs.close();
return;
}
//3、文件存在,并且记录数据
int num = this->get_borrower_number();
//cout << "职工人数为: " << num << endl;
this->book_num = num;
//开辟空间
this->m_Library_Array = new Library * [this->book_num];
//将文件中的数据 ,存到数组中
this->Library_initialize();
}
//初始化图书
void Library_initialize()
{
ifstream ifs;
ifs.open(BOOKFILE, ios::in | ios::binary);
Book_struct book_struct;
int index = 0;
while (ifs >> book_struct.m_book_name && ifs >> book_struct.m_book_id && ifs >> book_struct.m_book_price && ifs>> book_struct.m_author && ifs >> book_struct.m_loan)
{
Library* book = NULL;
book = new Library;
this->m_Library_Array[index] = book;
index++;
}
//关闭文件
ifs.close();
}
//显示菜单的函数
void show_menu()
{
cout << "********************************************" << endl;
cout << "**********欢迎使用图书借阅管理系统**********" << endl;
cout << "************** 0.退出系统 **************" << 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 << "********* 9.显示所有读者的信息 *********" << endl;
cout << "************ 10.删除某一读者 ************" << endl;
cout << "**************** 11.借书 ****************" << endl;
cout << "**************** 12.还书 ****************" << endl;
cout << "********************************************" << endl;
cout << endl;
}
//录入新书函数。另外注意,不能加const,不用管此处编译器的提示。一旦增加了const,会导致不能修改该类的成员变量book_num,然而这个函数需要做到能修改book_num
void set_book()
{
cout << "请输入要新增图书的数量" << endl;
int add_number = 0;
cin >> add_number;
if (add_number > 0)
{
//计算新空间的大小,其中,等号右边第一项是已经存储的书的数目,第二项是新增加的书的数目
int new_size = this->book_num + add_number;
//开辟新空间
Library** new_space = new Library * [new_size];
//将原空间下内容存放到新空间下
if (this->m_Library_Array != NULL)
{
for (int i = 0; i < this->book_num; i++)
{
new_space[i] = this->m_Library_Array[i];
}
}
//输入新数据
for (int i = 0; i < add_number; i++)
{
Book_struct new_book;
cout << "请输入第 " << i + 1 << " 本新书的名称:" << endl;
cin >> new_book.m_book_name;
cout << "请输入第 " << i + 1 << " 本新书的编号:" << endl;
cin >> new_book.m_book_id;
cout << "请输入第 " << i + 1 << " 本新书的价格:" << endl;
cin >> new_book.m_book_price;
cout << "请输入第 " << i + 1 << " 本新书的作者:" << endl;
cin >> new_book.m_author;
new_book.m_loan = false;//由于是新录入的图书,所以状态一定是未借出
Library* book = NULL;
book = new Library;
new_space[this->book_num + i] = book;
}
//释放原有空间
delete[] this->m_Library_Array;
//更改新空间的指向
this->m_Library_Array = new_space;
//更新图书数量
this->book_num = new_size;
//更新判断图书数量是否为空的标志为“不为空”
this->m_BOOKFILE_empty = false;
//提示添加成功
cout << "成功添加" << add_number << "本新书!" << endl;
//保存数据到文件中
this->save();
}
else
{
cout << "输入有误" << endl;
}
//按任意键后 清屏回到上级目录
system("pause");
system("cls");
}
//将信息写入文件
void save()
{
ofstream ofs;
ofs.open(BOOKFILE, ios::out | ios::binary);//以二进制方式写文件
//将每个人数据写入到文件中
for (int i = 0; i < this->book_num; i++)
{
ofs << this->m_Library_Array[i]->m_book.m_book_name << " "
<< this->m_Library_Array[i]->m_book.m_book_id << " "
<< this->m_Library_Array[i]->m_book.m_book_price << " "
<< this->m_Library_Array[i]->m_book.m_author << " "
<< this->m_Library_Array[i]->m_book.m_loan << endl;
}
//关闭文件
ofs.close();
}
//按书名查询图书
void get_book_information_by_book_name()
{
}
//按作者名查询图书
void get_book_information_by_author()
{
}
//显示所有图书的信息函数中的show_one_book函数,该函数用于输出单个图书的信息,再通过for循环来调用该函数以输出全部读者信息
void show_one_book()
{
cout << "图书名称: " << this->m_book.m_book_name
<< "\t编号: " << this->m_book.m_book_id
<< "\t单价: " << this->m_book.m_book_price
<< "\t作者:" << this->m_book.m_author
<< "\t存在状态" << this->m_book.m_loan << endl;
}
//显示所有图书的信息
void show_all_book()
{
//判断文件是否为空
if (this->m_BOOKFILE_empty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
for (int i = 0; i < book_num; i++)
{
//利用多态调用程序接口
this->m_Library_Array[i]->show_one_book();
}
}
//按任意键后清屏
system("pause");
system("cls");
}
void exit_system()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
//析构函数
~Library()
{
//手动释放堆区数据
if (this->m_Library_Array != NULL)
{
delete[] this->m_Library_Array;
}
}
};
int main() {
Library lib;
Borrower bor;
int choice = 0;
while (true)//为了能够结合清屏功能,使得每次屏幕都打印菜单,且可以连续进行选择
{
lib.show_menu();
cout << "请输入您的选择:" << endl;
cin >> choice;
switch (choice)
{
case 0://退出系统
lib.exit_system();
break;
case 1://录入新书
lib.set_book();
break;
case 2://按照书名查询图书
break;
case 3://按作者名查询图书
break;
case 4://显示所有图书的信息
lib.show_all_book();
break;
case 5://删除某一本书
break;
case 6://为借书人办理注册
bor.set_borrower();
break;
case 7://按照姓名查询读者
break;
case 8://按照学号查询读者
break;
case 9://显示所有读者的信息
bor.show_all_borrower();
break;
case 10://删除某一读者
break;
case 11://借书
break;
case 12://还书
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}