사용자가 책의 ISBN 번호를 입력 할 때 이미 저장되어있는 경우 오류 메시지가 출력되는 데이터 유효성 검사 양식을 입력하려고합니다. 그러나, 나는 이것을하는 데 문제가 있습니다. == 연산자를 올바르게 오버로드하고 있는지 잘 모르겠습니다. store_ISBN() 함수의 벡터 값을 비교하는 방법을 모르겠습니다. 내가하는 방법을 알아 내기 전에 모든 일을 입력하지 않기 때문에 store_ISBN 기능에 난 단지 하나 개의 변수에 대한 테스트를 포함했다고벡터의 값을 입력 값과 비교
가#include "std_lib_facilities.h"
// Classes ---------------------------------------------------------------------
class Book{
public:
vector<Book> books; // stores book information
Book() {}; // constructor
friend ostream& operator<<(ostream& out, const Book& b);
bool operator==(const Book& d);
string what_title();
string what_author();
int what_copyright();
void store_ISBN();
void is_checkout();
private:
char check;
int ISBNfirst, ISBNsecond, ISBNthird;
char ISBNlast;
string title;
string author;
int copyright;
};
// Class Functions -------------------------------------------------------------
string Book::what_title()
{
cout << "Title: ";
getline(cin,title);
cout << endl;
return title;
}
string Book::what_author()
{
cout << "Author: ";
getline(cin,author);
cout << endl;
return author;
}
int Book::what_copyright()
{
cout << "Copyright Year: ";
cin >> copyright;
cout << endl;
return copyright;
}
void Book::store_ISBN()
{
bool test = false;
cout << "Enter ISBN number separated by spaces: ";
while(!test){
cin >> ISBNfirst >> ISBNsecond >> ISBNthird >> ISBNlast;
for(int i = 0; i < books.size(); ++i)
if(ISBNfirst == books[i]) cout << "test"; // no idea how to implement this line
if((ISBNfirst<0 || ISBNfirst>9) || (ISBNsecond<0 || ISBNsecond>9) || (ISBNthird<0 || ISBNthird>9))
error("Invalid entry.");
else if(!isdigit(ISBNlast) && !isalpha(ISBNlast))
error("Invalid entry.");
else test = true;}
cout << endl;
}
void Book::is_checkout()
{
bool test = false;
cout << "Checked out?(Y or N): ";
while(!test){
cin >> check;
if(check == 'Y') test = true;
else if(check == 'N') test = true;
else error("Invalid value.");}
cout << endl;
}
// Operator Overloading --------------------------------------------------------
bool Book::operator==(const Book& d){ // is this right???
if((ISBNfirst == d.ISBNfirst) && (ISBNsecond == d.ISBNsecond)
&& (ISBNthird == d.ISBNthird) && (ISBNlast == d.ISBNlast)) return true;
else return false;
}
ostream& operator<<(ostream& out, const Book& b){
out << "Title: " << b.title << endl;
out << "Author: " << b.author << endl;
out << "ISBN: " << b.ISBNfirst << "-" << b.ISBNsecond << "-" << b.ISBNthird << "-" << b.ISBNlast << endl;
out << endl;
return out;
}
// Main ------------------------------------------------------------------------
int main()
{
Book store;
string question;
while(true){
store.what_title();
store.what_author();
store.what_copyright();
store.store_ISBN();
store.is_checkout();
store.books.push_back(store);
cout << "Are you finished?(Y or N): ";
cin >> question;
if(question == "Y") break;
else if(question == "N"){
cout << endl;
cin.ignore();}
else error("Invalid value.");
}
cout << endl;
cout << "Books stored -\n" << endl;
for(int i = 0; i < store.books.size(); ++i)
cout << store.books[i];
keep_window_open();
}
참고 : 여기에
코드입니다 그것.책이 메인의 루프를 통과 할 때마다 알 수 있듯이 해당 책의 데이터가 저장됩니다. 그런 다음 < < 연산자에 오버로드하여 제목, 작성자 및 ISBN을 인쇄하여 루프 이후의 모든 데이터 입력을 출력 할 수 있습니다. 그래서 나는 벡터의 개별 데이터에 액세스하여 사용자 입력 ISBN과 비교할 수 있어야한다고 생각하지만 어떻게해야할지 모르겠다. 내가 혼란스러워하는 부분은 그러한 것으로 주석 처리되었습니다.
while 루프는 잘못된 형식입니다. while (true!) 대신 while (question! = "Y")를 사용하십시오. – jkeys
또한 "무엇"접두사를 사용하지 않고 그냥 변수의 이름을 사용하십시오. 일반적인 규칙에 따라 모든 데이터 멤버를 "_"(예 : string title_;)로 추적 한 다음 밑줄없이 이름을 사용하여 해당 변수에 액세스해야합니다. – jkeys