问题遇到的现象和发生背景
我想要创建一个图书馆的类,我分了头文件、头文件的实现文件和主函数(main)文件。
问题相关代码,请勿粘贴截图
然后我是在main函数这里调用了一个函数,然后卡住了:
int* seek = new int[30];
for (unsigned int q = 0; q < d; q++)
{
for (int jiu = 0;; jiu++)
{
if (catalogueA.getCatalogue_all()[jiu].getBookID() == borr.getBookID_borrower()[q])
{
seek[q] = jiu;
break;
}
else;
}
}
for (unsigned int r = 0; r < d; r++)
{
catalogueA.setBookRecord_number_catalogue(seek[r]);
}
运行结果及报错内容
我输入时只能输入一点
这是报错信息:
我的解答思路和尝试过的方法
源代码贴一下,不知道会不会太长了TAT
这是头文件:
```c++
// Library.h
#include <string>
#ifndef LIBRARY_H
#define LIBRARY_H
class BookRecord
{
public:
BookRecord(std::string = "a", std::string = "a", std::string = "a", std::string = "a", int = 0, int = 0);
~BookRecord();
std::string getBookID();
void printBookRecord();
unsigned int getBookNumber();
unsigned int getBookNumber_now();
void changeBook_number();
private:
std::string bookID;
std::string bookName;
std::string firstName;
std::string lastName;
unsigned int year_published;
unsigned int bookNumber;
unsigned int bookNumber_now;
};
//................................................................
class Borrower
{
public:
//Borrower();
Borrower(unsigned int = 0, std::string = "a", std::string = "a", unsigned int = 0); //这个最后一个形参需要注意,可能会错
~Borrower();
void printBorrower();
void setBookID();
int getBorrower_id();
static int getBorrower_number();
void test(unsigned int);
std::string* getBookID_borrower();
private:
unsigned int borrower_ID;
std::string firstName;
std::string lastName;
unsigned int borrow_number;
std::string* bookID;
static int borrower_number;
};
//........................................................................
class Catalogue
{
friend class Libraryy;
public:
Catalogue();
Catalogue(unsigned int);
~Catalogue();
void setBookNumber_all_now(int);
unsigned int getBookNumber_all_now();
unsigned int getBookNumber_all();
void printBookRecord(std::string);
static BookRecord getBookRecord(std::string);
void setCatalogue(BookRecord);
static int findID(std::string);
static unsigned int getBookRecord_number();
void printCatalogue();
BookRecord* getCatalogue_all();
void setBookRecord_number_catalogue(int);
private:
static int bookNumber_all;
static BookRecord* catalogue_all;
static int bookNumber_all_now;
static int bookRecord_number;
static int iii;
};
//............................................................................................
class Libraryy
{
public:
Libraryy(Catalogue&, Borrower[]);//constructor
~Libraryy(); //destructor
unsigned int getBookNumber_onLoan();
Catalogue getLibraryy_Catalogue();
void printBookRecord_number();
void printBorrower_number();
void printBorrower(int = 0);
static void setBorrowerNumber(int);
void setBorrower_list(Borrower[]);
Catalogue Libraryy_catalogue;
int getBorrower_number();
void printBorrower_all();
private:
static int bookNumber_onLoan;
static int borrowerNumber;
static Borrower* borrower_list;
};
#endif
```c++
#include<iostream>
#include"Library.h"
#include<string.h>
using namespace std;
BookRecord::BookRecord(string a, string b, string c, string d, int date, int num)
:bookID(a),
bookName(b),
firstName(c),
lastName(d),
year_published(date),
bookNumber(num),
bookNumber_now(num)
{
int test = date / 1000;
char test1 = a[0];
//try {
// if ((test > 2 && test < 1) || (int)test1 < 65 || (int)test1 >= 97)
// throw std::invalid_argument("date must be 1*** or 2***, and the first letter of the ID must be an uppercase letter ");
//}
//catch (invalid_argument& e)
//{
// cerr << "\nException while initializing:" << e.what() << endl;
//}
}
BookRecord::~BookRecord()
{}
string BookRecord::getBookID()
{
return bookID;
}
unsigned int BookRecord::getBookNumber()
{
return bookNumber;
}
unsigned int BookRecord::getBookNumber_now()
{
return bookNumber_now;
}
void BookRecord::printBookRecord()
{
cout << "Book ID:" << bookID << endl;
cout << "Title:" << bookName << endl;
cout << "Author:" << firstName << " " << lastName << endl;
cout << "Year published:" << year_published << endl;
cout << "Total number of copies:" << bookNumber << endl;
cout << "Number available for loan:" << bookNumber_now << endl;
}
void BookRecord::changeBook_number()
{
bookNumber_now = bookNumber_now - 1;
}
//分割线,上面是BookRecord类................................................
int Borrower::borrower_number = 0;
Borrower::Borrower(unsigned int a, string b, string c, unsigned int d)
:borrower_ID(a),
firstName(b),
lastName(c),
borrow_number(d)
{
bookID = new string[50];
setBookID();
borrower_number++;
}
//////////
string* Borrower::getBookID_borrower()
{
return bookID;
}
/////////////
void Borrower::setBookID()
{
for (unsigned int i = 0; i < borrow_number; i++)
{
if (i < borrow_number - 1)
getline(cin, bookID[i], ';');
else
cin >> bookID[i];
}
}
Borrower::~Borrower()
{
}
void Borrower::printBorrower()
{
cout << "Borrower ID:" << borrower_ID << endl;
cout << "Name:" << firstName << " " << lastName << endl;
cout << "Number of loaned books: " << borrow_number << endl;
cout << "IDs of books on loan: ";
unsigned int i = 0;
for (; i < borrow_number; i++)
{
cout << bookID[i] << " ";
}
cout << endl;
}
int Borrower::getBorrower_number()
{
return borrower_number;
}
int Borrower::getBorrower_id()
{
return borrower_ID;
}
void Borrower::test(unsigned int a)
{
if (a / 10000 == 0 || a / 10000 > 9)
throw std::invalid_argument("Id must be five digits");
}
//分割线,上面是类Borrower....................................................................
int Catalogue::bookNumber_all = 0;
int Catalogue::bookNumber_all_now = 0;
int Catalogue::bookRecord_number = 0;
BookRecord* Catalogue::catalogue_all = new BookRecord[50];
int Catalogue::iii = 0;
Catalogue::Catalogue(unsigned int a)
{
bookRecord_number = a;
}
Catalogue::Catalogue()
{
}
Catalogue::~Catalogue()
{}
void Catalogue::setBookNumber_all_now(int a)
{
bookNumber_all_now -= a;
}
unsigned int Catalogue::getBookNumber_all_now()
{
return bookNumber_all_now;
}
unsigned int Catalogue::getBookNumber_all()
{
return bookNumber_all;
}
void Catalogue::setCatalogue(BookRecord cord)
{
catalogue_all[iii] = cord;
iii++;
bookNumber_all += cord.getBookNumber();
bookNumber_all_now += cord.getBookNumber();
}
void Catalogue::printBookRecord(string id)
{
catalogue_all[findID(id)].printBookRecord();
}
BookRecord Catalogue::getBookRecord(string id)
{
return catalogue_all[findID(id)];
}
int Catalogue::findID(string a)
{
int i = 0;
for (int e = 0; e == 0; )
{
if (strcmp(catalogue_all[i].getBookID().c_str(), a.c_str()) == 0)
e++;
else
i++;
}
return i;
}
unsigned int Catalogue::getBookRecord_number()
{
return bookRecord_number;
}
void Catalogue::printCatalogue()
{
for (int a = 0; a < bookRecord_number; a++)
{
cout << "Book Record " << a << endl;
cout << "=============" << endl;
catalogue_all[a].printBookRecord();
}
}
BookRecord* Catalogue::getCatalogue_all()
{
return catalogue_all;
}
void Catalogue::setBookRecord_number_catalogue(int v)
{
catalogue_all[v].changeBook_number();
}
//分割线,上面是类Catalogue...................................................................................
int Libraryy::bookNumber_onLoan = 0;
int Libraryy::borrowerNumber = 0;
Borrower* Libraryy::borrower_list = new Borrower[50];
Libraryy::Libraryy(Catalogue& a, Borrower b[])
:
Libraryy_catalogue(a)
{
setBorrower_list(b);
}
Libraryy::~Libraryy()
{}
unsigned int Libraryy::getBookNumber_onLoan()
{
return Catalogue::bookNumber_all - Catalogue::bookNumber_all_now;
}
Catalogue Libraryy::getLibraryy_Catalogue()
{
return Libraryy_catalogue;
}
void Libraryy::printBookRecord_number()
{
cout << Catalogue::getBookRecord_number();
}
void Libraryy::printBorrower_number()
{
cout << Borrower::getBorrower_number();
}
void Libraryy::setBorrowerNumber(int pp)
{
borrowerNumber = pp;
}
void Libraryy::setBorrower_list(Borrower pp[])
{
for (int i = 0; i < borrowerNumber; i++)
{
borrower_list[i] = pp[i];
}
}
void Libraryy::printBorrower(int a)
{
for (int i = 0; i < borrowerNumber; i++)
{
if (borrower_list[i].getBorrower_id() == a)
{
borrower_list[i].printBorrower();
break;
}
else;
}
}
int Libraryy::getBorrower_number()
{
return borrowerNumber;
}
void Libraryy::printBorrower_all()
{
for (int i = 0; i < borrowerNumber; i++)
{
cout << "Borrower " << i << endl << "===========" << endl;
borrower_list[i].printBorrower();
}
}
这是main:
#include<iostream>
#include"Library.h"
#include<sstream>
using namespace std;
int main()
{
unsigned int u = 0;
cin >> u;
string ID, bn, fn, ln;
string datep;
int date = 0, num = 0;
Catalogue catalogueA(u); //这里我创建好了整个图书馆总共有多少种书。
for (unsigned int i = 0; i < u; i++)
{
stringstream temp;
getline(cin, ID, ';');
getline(cin, bn, ';');
cin >> fn;
getline(cin, ln, ';');
getline(cin, datep, ';');
temp << datep;
temp >> date;
cin >> num;
BookRecord a(ID, bn, fn, ln, date, num);
catalogueA.setCatalogue(a);
}
//这里我创建好了图书馆所拥有的书的信息
int borrnum = 0;
cin >> borrnum;
Libraryy::setBorrowerNumber(borrnum);
string aa, dd;
unsigned int a = 0, d = 0;
string b, c;
Borrower* bow = new Borrower[50];
for (int i = 0; i < borrnum; i++)
{
stringstream tempp, tempp1;
getline(cin, aa, ';');
cin >> b;
getline(cin, c, ';');
getline(cin, dd, ';');
tempp << aa;
tempp >> a;
tempp1 << dd;
tempp1 >> d;
Borrower borr(a, b, c, d);
bow[i] = borr;
catalogueA.setBookNumber_all_now(d);
/// <summary>
/// //
/// </summary>
/// <returns></returns>
int* seek = new int[30];
for (unsigned int q = 0; q < d; q++)
{
for (int jiu = 0;; jiu++)
{
if (catalogueA.getCatalogue_all()[jiu].getBookID() == borr.getBookID_borrower()[q])
{
seek[q] = jiu;
break;
}
else;
}
}
for (unsigned int r = 0; r < d; r++)
{
catalogueA.setBookRecord_number_catalogue(seek[r]);
}
}
//在这里我把借款人的信息都传到了Libraryy里
Libraryy LibraryyA(catalogueA, bow);
//这里是用构造函数把图书信息和借书人信息传到了Libraru里面。
cout << "Total number of books on loan: "
<< catalogueA.getBookNumber_all() - catalogueA.getBookNumber_all_now() << endl;
cout << "Total number of books in catalogue: "
<< catalogueA.getBookNumber_all_now() << endl;
LibraryyA.Libraryy_catalogue.printCatalogue();
cout << "Total number of borrowers:" << LibraryyA.getBorrower_number() << endl;
LibraryyA.printBorrower_all();
}