📄 修改书的信息.cpp
字号:
#include "stdafx.h"
#include"harRange.h"
#include"BookData.h"
void BookInfo(BookData &obj);
void changeISBN(fstream &file,BookData &obj)
{
char isbn[14];
cout<<"\t\t\t请输入新的isbn号:";
cin.getline(isbn,14);
obj.setISBN (isbn);
}
void changeTitle(fstream &file,BookData &obj)
{
char title[51];
cout<<"\t\t\t请输入新的书名:";
cin.getline(title,51);
obj.setTitle (title);
}
void changeAuthor(fstream &file,BookData &obj)
{
char author[31];
cout<<"\t\t\t请输入新的作者:";
cin.getline(author,31);
obj.setAuthor (author);
}
void changePub(fstream &file,BookData &obj)
{
char publisher[31];
cout<<"\t\t\t请输入新的出版社:";
cin.getline(publisher,31);
obj.setPub (publisher);
}
void changeDateAdded(fstream &file,BookData &obj)
{
char dateAdded[11];
cout<<"\t\t\t请输入新的进书日期:";
cin.getline(dateAdded,11);
obj.setDateAdded (dateAdded);
}
void changeWholesale(fstream &file,BookData &obj)
{
float wholesale;
cout<<"\t\t\t请输入新的批发价:";
cin>>wholesale;
obj.setWholesale (wholesale);
}
void changeQty(fstream &file,BookData &obj)
{
int qty;
cout<<"\t\t\t请输入新的库存量:";
cin>>qty;
obj.setQty (qty);
}
void changeRetail(fstream &file,BookData &obj)
{
float retail;
cout<<"\t\t\t请输入新的零售价:";
cin>>retail;
obj.setRetail (retail);
}
void editBook()
{
BookData obj;
int a; //用来记录指针位置
char ch;
string bookname,name; //name用来将文件里的bookTitle转换成string类型
fstream file;
file.open("shendan.dat",ios::out|ios::in|ios::binary);
if(file.fail())
{
cout<<"\t\t\t打开文件失败!";
exit(0);
}
cout<<"\t\t\t请输入您要修改的书的书名:";
cin>>bookname;
file.seekg(0,ios::beg);
a = file.tellg();
file.read((char *)&obj,sizeof(obj));
name.assign (obj.getTitle() );
while(bookname !=name && !file.eof())
{
a = file.tellg();
file.read((char *)&obj,sizeof(obj));
name.assign (obj.getTitle());
}
if(bookname!=name)
{
cout<<"\t\t\t本书库没有该书!\n";
}
else
{
BookInfo(obj);
do
{
cout<<"\t\t\t-------------------------------\n";
cout<<"\t\t\t1.ISBN号"<<endl;
cout<<"\t\t\t2.书名"<<endl;
cout<<"\t\t\t3.作者"<<endl;
cout<<"\t\t\t4.出版社"<<endl;
cout<<"\t\t\t5.进书日期"<<endl;
cout<<"\t\t\t6.库存量"<<endl;
cout<<"\t\t\t7.批发价"<<endl;
cout<<"\t\t\t8.零售价"<<endl;
cout<<"\t\t\t请输入您要修改的项目:";
harRange obj1(1,8,"请输入1-8之间的数字!\n");//出错检验
switch(obj1.getChar())
{
case 1:changeISBN(file,obj);break;
case 2:changeTitle(file,obj);break;
case 3:changeAuthor(file,obj);break;
case 4:changePub(file,obj);break;
case 5:changeDateAdded(file,obj);break;
case 6:changeQty(file,obj);break;
case 7:changeWholesale(file,obj);break;
case 8:changeRetail(file,obj);break;
}
cout<<"\t\t\t还要修改其它数据项吗(是:y,否:n)?";
cin>>ch;
}while(ch=='y');
}
file.seekg(a,ios::beg);
file.seekp(a,ios::beg);
file.write((char *)&obj,sizeof(obj));
file.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -