gpt4 book ai didi

c++ - 将输入值与 vector 中的值进行比较

转载 作者:行者123 更新时间:2023-11-27 22:32:02 25 4
gpt4 key购买 nike

我正在尝试输入一种数据验证形式,当用户输入一本书的 ISBN 编号时,如果它已经被存储,那么它将输出一条错误消息。但是,我在这样做时遇到了麻烦。我不确定是否正确重载了 == 运算符,也不确定如何比较 store_ISBN() 函数中的 vector 值。

代码如下:

#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();
}

请注意,在 store_ISBN 函数中,我只包含了对一个变量的测试,因为我不想在弄清楚如何去做之前输入所有内容。

如您所见,每次一本书通过 main 中的循环时,都会存储该书的数据。然后,通过重载 << 运算符打印标题、作者和 ISBN,我能够在循环后输出所有数据输入。所以我认为我应该能够访问 vector 中的单个数据以与用户输入的 ISBN 进行比较,但我不知道如何做。我感到困惑的部分已被这样评论。

最佳答案

我不太确定用户应该为 ISBN 输入什么。

从一个流中读入一个 int 将读取数字直到一个空格,并将结果转换为 int(如果一切顺利,无论如何)。读入 char 将存储 char 值。因此,目前您正在验证的 ISBN 看起来像三个个位数 (0-9),然后是下一个字符。这不是我认为的 ISBN 的样子。

你的 operator==看起来不错,但请注意,对于 bool 返回值,

if (X) return true;
else return false;

可以替换为

return X;

因为条件已经是 bool 类型了。

设置您的 ISBN 值后(以及您计划在 operator== 中使用的任何其他字段,如果尚未完成),在商店中查找匹配图书的方法是:

for(int i = 0; i < books.size(); ++i)
if(*this == books[i]) cout << "test";

换句话说,找一本和这本书一样的书。或者你可以使用 std::find来自 <algorithms> ,尽管在这种情况下它真的不会更简洁。

顺便说一下,使用同一个类(Book)来表示一本书和整个商店是不常见的。不过,取消它是一组相当复杂的更改和决定,所以我只想说一个类应该代表一种事物,而该类的一个对象代表这种类型的一个例子。所以通常 Book 和 Bookstore 是不同种类的东西。 Book 中的 vector 是一个实例变量,这意味着每本书都有自己的 Books vector 。这真的没有意义。

关于c++ - 将输入值与 vector 中的值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1146337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com