我正在从事学校项目 - 图书馆管理系统 - 并且遇到了问题。相关代码如下:
class student
{
int stadmno;
char stname[15];
int bkcount;
char Class[6];
int vs[6];
int i;
public:
student()
{
bkcount = 0;
i = 0;
}
void getstu(); //To input data.
void dispstu(); //To output data.
int ret_stadmno()
{
return stadmno;
}
int ret_bkcount()
{
return bkcount;
}
char* ret_stname()
{
return stname;
}
void modify_stadmno()
{
cin>>stadmno;
cin.get();
}
void modify_stname()
{
gets(stname);
}
void modify_bkcount()
{
cin>>bkcount;
cin.get();
}
char* ret_class()
{
return Class;
}
void modify_class()
{
gets(Class);
}
void bk_borrow(int n)
{
cout<<"\n Check 1.";
vs[i] = n;
cout<<"\n Check 2.";
i++;
}
int* ret_borrowed()
{
return vs;
}
void bk_return(int n)
{
int size = sizeof(vs)/sizeof(int);
for(int j = 0 ; j<size ; j++)
{
if(vs[j] == n)
vs[j] = 0;
}
}
}s;
class book
{
int bkno;
char bkname[30];
char auname[15];
char genre[10];
int state;
int borrowed;
public:
book()
{
state = 0;
borrowed = 0;
}
void getbk(); //To input data.
void dispbk(); //To output data.
char* ret_bkname()
{
return bkname;
}
char* ret_auname()
{
return auname;
}
int ret_bkno()
{
return bkno;
}
char* ret_genre()
{
return genre;
}
void modify_bkno()
{
cin>>bkno;
cin.get();
}
void modify_bkname()
{
gets(bkname);
}
void modify_auname()
{
gets(auname);
}
void modify_genre()
{
gets(genre);
}
int check()
{
return state;
}
void issue(int n)
{
state = 1;
borrowed = n;
}
void deposit()
{
state = 0;
borrowed = 0;
}
int ret_borrowed()
{
return borrowed;
}
}b;
void issue_book()
{
clear();
cout<<"\n\t\t Book Issue Screen. \n";
int n;
do
{
cout<<"\n Search according to: ";
cout<<"\n\t 1. Title. ";
cout<<"\n\t 2. Author. ";
cout<<"\n\t 3. Book number. ";
cout<<"\n\t 4. Genre. ";
cout<<"\n\n Press 5 to exit. \n";
cin>>n;
cin.get();
if(n==1 || n==2 || n==3 || n==4)
{
vector<int> vb;
switch(n)
{
case 1: vb = search_bkname();
break;
case 2: vb = search_auname();
break;
case 3: vb = search_bkno(0);
break;
case 4: vb = search_genre();
break;
}
fstream if1, if2;
if1.open("Books.dat", ios::in | ios::binary);
if2.open("Students.dat", ios::in | ios::binary);
if(vb.back() == -1)
{
cout<<"\n Record not found. Please try again. \n";
}
else
{
vector<int> vs;
int op, admno, z, c, no;
z = 0;
c = 0;
cout<<"\n Search results: \n";
book_header();
for(int i = 0; i<vb.size(); i++)
{
if1.seekg(vb[i], ios::beg);
if1.read((char*)&b, sizeof(b));
b.dispbk();
}
cout<<"\n Choose a book (Numerically): ";
cin>>op;
cin.get();
if1.seekg(vb[op-1], ios::beg);
if1.read((char*)&b, sizeof(b));
if(b.check() == 1)
{
cout<<"\n Book is currently borrowed.";
vs = search_stadmno(b.ret_borrowed());
if2.seekg(vs.back(), ios::beg);
if2.read((char*)&s, sizeof(s));
cout<<"\n Holder: "<<s.ret_stname()<<", "<<s.ret_stadmno()<<endl;
c = 1;
}
if(c == 0)
{
do
{
cout<<"\n Enter the admission number of student borrowing the book: ";
cin>>admno;
cin.get();
vs = search_stadmno(admno);
if(vs.back() == -1)
{
cout<<"\n Record not found. Please try again. \n";
z = 1;
}
else
{
z = 0;
if2.seekg(vs.back(), ios::beg);
if2.read((char*)&s, sizeof(s));
student_header();
s.dispstu();
if1.seekg(vb[op-1], ios::beg);
if1.read((char*)&b, sizeof(b));
book_header();
b.dispbk();
no = b.ret_bkno();
s.bk_borrow(no);
cout<<"\n Check 3.";
b.issue(s.ret_stadmno());
cout<<"\n Book issued successfully. \n";
}
}while(z != 0);
}
}
if1.close();
if2.close();
}
else if(n != 5)
cout<<"\n Invalid input. \n";
}while(n != 5);
}
执行程序时,没有错误,程序输出到Check 1.之后,它崩溃,说'project.exe已停止工作&#39;。使用矢量时会发生同样的事情。有人可以指出问题吗?
PS:我使用的是Dev-C ++ 5.11 PPS:这是我在这里的第一个问题,如果格式不正确,我很抱歉。
编辑:为这两个类和函数issue_book添加了代码。
谢谢!
答案 0 :(得分:5)
如果没有看到完整的代码,这很难说清楚。但最有可能的是,作业
vs[i] = n;
由于超出范围而失败。您将vs
保留为大小为6的固定大小数组,因此i
必须介于0和5之间(包括)。
附加调试器并在失败时中断,或在分配之前输出i
以查看它可以采用的值。