我把代码都展出来吧
#ifndef LIBRARY_H
#define LIBRART_H
#include
#include
#include
#include
#include
#include
using namespace std ;
class Library
{
public:
Library();
~Library();
void ShowData(const char[],const char[] );
private:
string BookID[10] ; //书的型号
char *BookTitle ; //书名
char *FirstName ; //书名
char *LastName ; //书名
int PublicationYear[10]; //出版年份
int CopyNumber[10] ; //每种书的数量
int AvailableNumber[5] ; // 还可借书的数量
string ID[10] ; //借书者的ID
char *firstname ; //人名
char *lastname ; //人名
int loanednumber[5] ; //借书的数量
int loanedtotal ; // 借书的总数
int CopyTotal ; // 书的总数
string bookid[5] ; //借的书的型号的数量
};
#endif
#include "Library.h"
Library::Library()
{
char *BookTitle = new char[100] ; //书名
char *FirstName = new char[100] ; //书名
char *LastName = new char[100] ; //书名
int AvailableNumber[5] = {0} ; // 还可借书的数量
char *firstname = new char[100] ; //人名
char *lastname = new char[100] ; //人名
loanedtotal = 0 ; // 借书的总数
CopyTotal = 0 ; // 书的总数
}
Library::~Library()
{
delete [] BookTitle ;
delete [] FirstName ;
delete [] LastName ;
delete [] firstname ;
delete [] lastname ;
}
void Library::ShowData(const char BookRecord[],const char Borrower[])
{
ifstream InBookRecord ;
InBookRecord.open(BookRecord) ;
int i = 0 ;
int n ;
if (!InBookRecord.is_open())
{
cout << "File could not be opened." ;
}
else
{
while (!InBookRecord.eof())
{
InBookRecord >> BookID[i] >> BookTitle[i] >> FirstName[i]
>> LastName[i] >> PublicationYear[i] >> CopyNumber[i] ;
i++ ;
}
}
n = i ;
InBookRecord.close();
for (int x=0;x<n;x++)
{
CopyTotal += CopyNumber[x] ;
}
ifstream InBorrower ;
InBorrower.open(Borrower) ;
int j = 0 ;
int m ;
if (!InBorrower.is_open())
{
cout << "File could not be opened.";
}
else
{
while (!InBorrower.eof())
{
InBorrower >> ID[j] >> firstname[j] >> lastname[j]
>> loanednumber[j] ;
for (int z=0;z<loanednumber[j];z++)
{
InBorrower >> bookid[z] ;
}
j++ ;
}
}
m = j ;
InBorrower.close() ;
for (int y=0;y<m;y++)
{
loanedtotal += loanednumber[y] ;
}
for (int x=0;x<n;x++)
{
for (int y=0;y<m;y++)
{
for (int z=0;z<loanednumber[j];z++)
{
if (BookID[x] == bookid[z])
{
AvailableNumber[x]++ ;
}
}
}
}
cout << "Total number of books on loan:" << loanedtotal << endl ;
cout << "Total number of books in catalogue:" << (CopyTotal-loanedtotal) << endl ;
for (int x=0;x<n;x++)
{
if (BookID[x].at(0)>='A'&&BookID[x].at(0)<='Z'&&PublicationYear[x]>=1000&&PublicationYear[x]<=2999)
{
cout << "BookRecord" << x << endl << endl ;
cout << "=============" << endl << endl ;
cout << "Book ID:" << BookID[x] << endl << "Title:"<< BookTitle[x] << endl
<< "Author:" << FirstName[x] << " " << LastName[x] << endl
<< "Year published:" << PublicationYear[x] << endl
<< "Total number of copies:" << CopyNumber[x] << endl
<< "Number available for loan:" << (CopyNumber[x] - AvailableNumber[x]) << endl << endl ;
}
}
for (int y=0;y<m;y++)
{
if (ID[y].length()==5&&loanednumber[y]>=0&&loanednumber[y]<=5)
{
cout << "Borrower" << y << endl << endl ;
cout << "=============" << endl << endl ;
cout << "Borrower ID:" << ID[y] << endl
<< "Name:" << firstname[y] << " " << lastname[y] << endl
<< "Number of loaned books: "<< loanednumber[y] << endl
<< "IDs of books on loan: " ;
for(int z=0;z<loanednumber[y];z++)
{
cout << bookid[z] << " " ;
}
}
}
}
凡越界访问问题,
先检查你所有分配内存的大小是否多1;
再检查你所有的数组遍历时,是否下标越界。
你的代码没有格式,看起来很吃力。简单看了下。
没有看到你如何调用showData.
但showData中,基本没有越界检查。比如下面这段:
while (!InBookRecord.eof())
{
InBookRecord >> BookID[i] >> BookTitle[i] >> FirstName[i]
LastName[i] >> PublicationYear[i] >> CopyNumber[i] ;
i++ ;
}
根据你的代码,注意到几个点:
char *firstname = new char[100] ; //人名
char *lastname = new char[100] ; //人名
int CopyNumber[10] ; //每种书的数量
10和100的大小,那你想想你这样的代码会不会出现问题:
InBookRecord >> BookID[i] >> BookTitle[i] >> FirstName[i]
LastName[i] >> PublicationYear[i] >> CopyNumber[i] ;
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。