C++问题 删除后 显示全是烫
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
//读者信息及借阅情况
class Data
{
public:
Data();
Data(char newname[], char newbookname[], int phone, int btime, int rtime);
void setname(char newname[20]);
char* getname();
void setbookname(char newbookname[30]);
char* getbookname();
void setnum(int u);
int getnum();
void setphone(int n);
int getphone();
void setborrowtime(int i);
int getborrowtime();
void setreturntime(int k);
int getreturntime();
private:
char name[20];
char bookname[30];
int phone;
int borrowtime;
int returntime;
int num;
};
//操作类
class DataOperator
{
public:
DataOperator();
void createfile();
int funcchoice();
void printrec();
void updaterec();
void insertrec();
void deleterec();
void inquirerec();
void displine();
bool checkdata();
void accexit();
private:
Data acc;
fstream inout;
char filename[90];
};
int main()
{
DataOperator data;
int func;
while (1)
{
func = data.funcchoice();
switch (func)
{
case 1:data.createfile(); break;
case 2:data.printrec(); break;
case 3:data.updaterec(); break;
case 4:data.insertrec(); break;
case 5:data.deleterec(); break;
case 6:data.inquirerec(); break;
case 0:data.accexit(); break;
}
if (!func)
break;
}
return 0;
}
Data::Data()
{
}
Data::Data(char newname[], char newbookname[], int newphone, int newbtime, int newrtime)
{
name[20] = newname[20];
bookname[30] = newbookname[30];
phone = newphone;
borrowtime = newbtime;
returntime = newrtime;
}
void Data::setname(char newname[20])
{
strcpy_s(name, newname);
}
char* Data::getname()
{
return name;
}
void Data::setbookname(char newbookname[30])
{
strcpy_s(bookname, newbookname);
}
char* Data::getbookname()
{
return bookname;
}
void Data::setnum(int u)
{
num = u;
}
int Data::getnum()
{
return num;
}
void Data::setborrowtime(int i)
{
borrowtime = i;
}
int Data::getborrowtime()
{
return borrowtime;
}
void Data::setphone(int n)
{
phone = n;
}
int Data::getphone()
{
return phone;
}
void Data::setreturntime(int k)
{
returntime = k;
}
int Data::getreturntime()
{
return returntime;
}
DataOperator::DataOperator()
{
cout << "请输入要创建的存放读者资料及借阅情况的文件名及所在目录:" << endl;
cin.getline(filename, 100);
inout.clear(); //清除错误信息
inout.open(filename, ios::out | ios::app);
if (inout.fail())
{
cout << "文件创建失败" << endl;
exit(1);
}
cout << "资料文件:" << filename << "创建成功,可以进行后续操作" << endl;
inout.close();
}
void DataOperator::createfile() //创建账户文件,并连续输入多个读者资料
{
int accnum;
char name[20];
char bookname[30];
int phone;
int borrowtime;
int returntime;
inout.clear(); //清除错误信息
inout.open(filename, ios::out | ios::app);
if (inout.fail())
{
cout << "文件创建失败" << endl;
exit(1);
}
cout << "输入用户账号(1-500):";
cin >> accnum;
acc.setnum(accnum);
while (checkdata())
{
cout << "输入读者姓名:";
cin >> name;
acc.setname(name);
cout << "输入所借书籍名字:";
cin >> bookname;
acc.setbookname(bookname);
cout << "输入读者手机号:";
cin >> phone;
acc.setphone(phone);
cout << "输入借阅时间:";
cin >> borrowtime;
acc.setborrowtime(borrowtime);
cout << "输入归还时间:";
cin >> returntime;
acc.setreturntime(returntime);
inout.seekp((acc.getnum() - 1) * sizeof(Data), ios::beg);
inout.write((char*)&acc, sizeof(Data));
cout << "请输入用户账号(1-500):";
cin >> accnum;
acc.setnum(accnum);
}
inout.close();
}
int DataOperator::funcchoice() //选择功能按钮
{
cout << "=========================================================" << endl;
cout << "请输入功能选择数字(1,2,3,4,5,6,0)" << endl;
cout << "1--连续输入账户记录到二进制文件" << filename << "中" << endl;
cout << "2--显示文件中所有记录" << endl;
cout << "3--修改读者记录" << endl;
cout << "4--添加读者记录" << endl;
cout << "5--删除读者记录" << endl;
cout << "6--查询读者记录" << endl;
cout << "0--退出" << endl;
cout << "=============================================" << endl;
int choice;
cout << "请输入要选择的功能号:";
cin >> choice;
return choice;
}
void DataOperator::printrec()
{
inout.clear(); //清楚错误状态信息
inout.open(filename, ios::in);
if (inout.fail())
{
cout << "打开文件失败!" << endl;
exit(1);
}
cout << left << setw(10) << "读者姓名:";
cout << setw(16) << "读者电话";
cout << setw(16) << "所借书籍名称";
cout << setw(15) << "借阅时间";
cout << setw(15) << "归还时间";
inout.seekg(0, ios::beg);
inout.read((char*)&acc, sizeof(Data));
while (!inout.eof())
{
if (acc.getnum() != 0)
displine();
inout.read((char*)&acc, sizeof(Data));
}
inout.close();
}
void DataOperator::updaterec()
{
int accnum;
cout << "请输入要修改的记录号:";
cin >> accnum;
inout.clear(); //清楚错误状态信息
inout.open(filename, ios::in | ios::out);
if (inout.fail())
{
cout << "文件打开失败" << endl;
exit(1);
}
inout.seekg((accnum - 1) * sizeof(Data), ios::beg);
inout.read((char*)&acc, sizeof(Data));
if (acc.getnum() == accnum)
{
cout << "要修改记录的原始内容为:" << endl;
displine();
cout << "输入读者姓名为:" << endl;
char name[20];
cin >> name;
acc.setname(name);
cout << "输入所借书籍名字为:" << endl;
char bookname[30];
cin >> bookname;
acc.setbookname(bookname);
cout << "输入读者手机号为:" << endl;
int phone;
cin >> phone;
acc.setphone(phone);
cout << "输入借阅时间为:" << endl;
int btime;
cin >> btime;
acc.setborrowtime(btime);
cout << "输入归还时间为:" << endl;
int rtime;
cin >> rtime;
acc.setreturntime(rtime);
displine();
inout.seekp((acc.getnum() - 1) * sizeof(Data), ios::beg);
inout.write((char*)&acc, sizeof(Data));
}
else
cout << "对不起,未找到要修改的记录" << endl;
inout.close();
}
void DataOperator::insertrec()
{
int accnum;
char name[20];
char bookname[30];
int phone;
int btime;
int rtime;
cout << "请输入要添加的记录号:" << endl;
cin >> accnum;
inout.clear(); //清除错误状态信息
inout.open(filename, ios::in | ios::out);
if (inout.fail())
{
cout << "打开文件失败" << endl;
exit(1);
}
inout.seekg((accnum - 1) * sizeof(Data), ios::beg);
inout.read((char*)&acc, sizeof(Data));
if (acc.getnum() == accnum)
cout << "对不起,记录已经存在" << endl;
else
{
acc.setnum(accnum);
cout << "输入读者姓名:";
cin >> name;
acc.setname(name);
acc.getname();
cout << "输入所借书籍名字:";
cin >> bookname;
acc.setbookname(bookname);
acc.getbookname();
cout << "输入读者手机号:";
cin >> phone;
acc.setphone(phone);
acc.getphone();
cout << "输入借阅时间:";
cin >> btime;
acc.setborrowtime(btime);
acc.getborrowtime();
cout << "输入归还时间:";
cin >> rtime;
acc.setreturntime(rtime);
acc.getreturntime();
inout.seekp((acc.getnum() - 1) * sizeof(Data), ios::beg);
inout.write((char*)&acc, sizeof(Data));
}
inout.close();
}
void DataOperator::deleterec()
{
int accnum;
cout << "请输入要删除的记录号:" << endl;
cin >> accnum;
inout.clear(); //清楚错误状态信息
inout.open(filename, ios::binary |ios::in | ios::out);
if (inout.fail())
{
cout << "打开文件失败" << endl;
exit(1);
}
inout.seekg((accnum - 1) * sizeof(Data), ios::beg);
inout.read((char*)&acc, sizeof(Data));
if (acc.getnum() == accnum)
{
cout << "要删除记录的原始内容为:" << endl;
displine();
cout << "输入空白记录覆盖该处的记录" << endl;
Data data;
inout.seekp((accnum - 1) * sizeof(Data), ios::beg);
inout.write((char*)&data, sizeof(Data));
}
else
cout << "对不起,没有要删除的记录" << endl;
inout.close();
}
void DataOperator::inquirerec()
{
int accnum;
cout << "请输入要查询的记录号:" << endl;
cin >> accnum;
inout.clear(); //清楚错误状态信息
inout.open(filename, ios::binary | ios::in);
if (inout.fail())
{
cout << "打开文件失败" << endl;
exit(1);
}
inout.seekg((accnum - 1) * sizeof(Data), ios::beg);
inout.read((char*)&acc, sizeof(Data));
if (acc.getnum() == accnum)
{
cout << "要查询的记录的内容为:" << endl;
displine();
}
else
cout << "对不起,没有要查询的记录" << endl;
inout.close();
}
void DataOperator::accexit()
{
cout << "谢谢使用图书馆管理系统" << endl;
}
void DataOperator::displine()
{
cout << setw(8) << acc.getname();
cout << setw(8) << acc.getphone();
cout << setw(8) << acc.getbookname();
cout << setw(8) << acc.getborrowtime();
cout << setw(8) << acc.getreturntime() << endl;
}
bool DataOperator::checkdata()
{
if (acc.getnum() >= 1 && acc.getnum() <= 500)
return 1;
else
return 0;
}
deleterec()问题 以及 插入函数未插入文本