在增添借閱者時輸入學號後退出,求助,謝謝。
#include <iostream>
using namespace std;
#include <string>
#include <fstream>
#include <iomanip>
const int Maxr=300;
const int Maxb=300;
const int Maxbor=10;
class Reader
{
private:
int tag;
int No;
char name[20];
int borbook[Maxbor];
public:
Reader() {}
char *getname() {return name;}
int gettag() {return tag;}
int getno() {return No;}
void setname(char na[])
{
strcpy(name,na);
}
void delbook(){ tag=1; }
void addreader(int n,char *na)
{
tag=0;
No=n;
strcpy(name,na);
for(int i=0;i<Maxbor;i++)
borbook[i]=0;
}
void borrowbook(int bookid)
{
for(int i=0;i<Maxbor;i++)
{
if (borbook[i]==0)
{
borbook[i]=bookid;
return;
}
}
}
int retbook(int bookid)
{
for(int i=0;i<Maxbor;i++)
{
if(borbook[i]==bookid)
{
borbook[i]=0;
return 1;
}
}
return 0;
}
void disp()
{
cout << setw(10) << No <<setw(15) << name<<"借書檢索號:[";
for(int i=0;i<Maxbor;i++)
if(borbook[i]!=0)
cout << borbook[i] << "|";
cout << "]"<<endl;
}
};
class RDatabase
{
private:
int top;
Reader read[Maxr];
public:
RDatabase()
{
Reader s;
top=-1;
fstream file("reader.txt",ios::in);
while (1)
{
file.read((char *)&s,sizeof(s));
if (!file)break;
top++;
read[top]=s;
}
file.close();
}
void clear()
{
top=-1;
}
int addreader(int n,char *na)
{
Reader *p=query(n);
if (p==NULL)
{
top++;
read[top].addreader(n,na);
return 1;
}
return 0;
}
Reader *query(int readerid)
{
for (int i=0;i<=top;i++)
if (read[i].getno()==readerid &&
read[i].gettag()==0)
{
return &read[i];
}
return NULL;
}
void disp()
{
for (int i=0;i<=top;i++)
read[i].disp();
}
void readerdata();
~RDatabase()
{
fstream file("reader.txt",ios::out);
for (int i=0;i<=top;i++)
if (read[i].gettag()==0)
file.write((char *)&read[i],sizeof(read[i]));
file.close();
}
};
void RDatabase::readerdata()
{
char choice;
char rname[20];
int readerid;
Reader *r;
cout <<"\n\n\t\t\t借閱者信息\n\n\n\t\t 1 增加借閱者信息\n\n\t\t 2 修改借閱者信息\n\n\t\t 3 刪除借閱者信息\n\n\t\t 4 查詢借閱者信息\n\n\t\t 0 退出"<<endl;
cin >> choice;
switch (choice)
{
case '1':
cout << "輸入借閱者學號:";
cin >> readerid;
cin >> rname;
addreader (readerid,rname);
break;
case '2':
cout << "輸入借閱者學號:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
cout << " 借閱者信息錯誤"<<endl;
break;
cout << "重新輸入姓名:"; cin >> rname;
r->setname(rname);
break;
case '3':
cout << " 輸入借閱者學號:";
cin >> readerid; r=query(readerid);
if (r==NULL)
cout <<" 借閱者信息錯誤" << endl;
break;
r->delbook();
break;
case '4':
cout << "輸入借閱者學號:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
{
cout <<"學號信息錯誤"<< endl;
} r->disp();
break;
break;
case '5':
disp();
break;
default:cout<<"信息錯誤,請重新輸入:";
break;
}
}
class Book
{
private:
float price;
char author[10];
int tag;
int No;
char place[40];
char name[20];
int onshelf;
public:
Book(){}
char *getname() { return name; }
int getNo(){ return No; }
char *getplace(){ return place;}
int gettag(){ return tag; }
char*getauthor(){return author;}
char getprice(){return price;}
void setname(char na[])
{
strcpy(name,na);
}
void setauthor(char au[])
{
strcpy(author,au);}
void delbook(){ tag=1;}
void addbook(int N,char *na,char*au,int pr,char *bp)
{
tag=0;
No=N;
price=pr;
strcpy(author,au);
strcpy(name,na);
onshelf=1;
}
int borrowbook()
{
if (onshelf==1)
{
onshelf=0;
return 1;
}
return 0;
}
void retbook()
{
onshelf=1;
}
void disp()
{
cout<< setw(6)<< "檢索號: " << No << setw(18) <<"書名: "<< name << setw(10) <<"作者: "<<author<<setw(18)<<"單價: "<<price<<setw(10) <<(onshelf==1? "有現存":"已借完") <<endl;
}
};
class BDatabase
{
private:
int top;
Book book[Maxb];
public:
BDatabase()
{
Book b;
top=-1;
fstream file("book.txt",ios::in);
while (1)
{
file.read((char *)&b,sizeof(b));
if (!file) break;
top++;
book[top]=b;
}
file.close();
}
void clear()
{
top=-1;
}
int addbook(int n,char *na,char*au,int pr,char *bp)
{
Book *p=query(n);
if (NULL==p)
{
top++;
book[top].addbook(n,na,au,pr,bp);
return 1;
}
return 0;
}
Book *query(int bookid)
{
for (int i=0;i<=top;i++)
if (book[i].getNo()==bookid &&book[i].gettag()==0)
{
return &book[i];
}
return NULL;
}
void bookdata();
void disp()
{
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
book[i].disp();
}
~BDatabase()
{
fstream file("book.txt",ios::out);
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
file.write((char *)&book[i],sizeof(book[i]));
file.close();
}
};
void BDatabase::bookdata()
{
char choice;
char bauthor[10];
int bprice;
char bname[40];
int bookid;
char bplace[40];
Book *b;
cout <<"\n\n\n\t\t\t圖書信息"<<endl<<endl; cout<<"\t\t1 增加圖書信息\n \t\t2 修改圖書信息\n\t\t3 刪除圖書信息\n\t\t4 查詢圖書信息\n\t\t0 退出"<<endl;
cin >> choice;
switch (choice)
{
case '1':
cout << "輸入圖書檢索號:"<<endl;
cin >> bookid;
cout << "輸入圖書書名:"<<endl;
cin >> bname;
cout<<" 輸入作者名:";
cin>>bauthor;
cout << "輸入圖書價格:"<<endl;
cin >> bprice;
cout << "輸入圖書出版社:"<<endl;
cin >> bplace;
addbook(bookid,bname,bauthor,bprice,bplace);
break;
case '2':
cout << "輸入圖書檢索號:"<<endl;
cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout << " 信息錯誤"<<endl;
break;
}
cout << "重新輸入書名:"<<endl;
cin >> bname;
b->setname(bname);
break;
case '3':
cout <<" 讀入圖書檢索號:"<<endl;
cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout <<" 信息錯誤" << endl;
break;
}
b->delbook();
break;
case '4':
cout << " 讀入圖書檢索號:"<<endl;
cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout <<" 信息錯誤"<< endl;
break;
}
b->disp();
break;
default:cout<<"信息錯誤,請重新輸入:";
}
}
void main()
{
char choice;
int bookid,readerid;
RDatabase ReaderDB;
Reader *r;
BDatabase BookDB;
Book *b;
cout <<endl<<endl<<"\t\t\t 圖書管理系統\n\n\n";
cout <<"\t\t\t1 借還情況\n\n\t\t\t2 圖書信息表\n\n\t\t\t3 借閱者信息表\n\n\t\t\t0 退出"<<endl;
cin >> choice;
switch (choice)
{
case '1':
cout <<"\t\t\t1 借書\n\n\t\t\t2 還書\n\n\t\t\t0 退出"<<endl;
cin>>choice;
switch(choice)
{
case '1':
cout <<" 借閱者學號:";
cin >>readerid;
cout <<" 圖書檢索號:";
cin >>bookid;
r=ReaderDB.query(readerid);
if (NULL==r)
{
cout <<" 借閱者信息錯誤,不能借書"<< endl;
break;
}
b=BookDB.query(bookid);
if (b==NULL)
{
cout<<"圖書信息錯誤,不能借書"<<endl;
break;
}
if(b->borrowbook()==0)
{
cout<<"該圖書沒有現存,不能借書"<<endl;
break;
}
r->borrowbook(b->getNo());
break;
case '2':
cout<<"借閱者學號:";
cin>>readerid;
cout<<"圖書檢索號:";
cin>>bookid;
r=ReaderDB.query(readerid);
if(r==NULL)
{
cout<<"借閱者信息錯誤,不能還書"<<endl;
break;
}
b=BookDB.query(bookid);
if(b==NULL)
{
cout<<"圖書信息錯誤,不能還書"<<endl;
break;
}
b->retbook();
r->retbook(b->getNo());
break;
}
case '2':
BookDB.bookdata();
break;
case '3':
ReaderDB.readerdata();
break;
default:cout<<"信息錯誤,請從新輸入:";
}
}
你要讓它不退出,需要外側包一個死循環,參考
http://codepad.org/XAeNGCSO