C++ C++;尝试查看输入字符串是否等于类的数组字符串
我正在尝试按书名或ISBN在我的数组中搜索一本书。但是,我无法使用我的搜索功能找到数组中已经存在的书籍。我不知道它为什么不起作用。我认为代码是正确的。搜索第一对条目的ISBN有效,其余条目则失败。但是,搜索所有标题都失败。由于每个人的代码几乎相同,我不知道为什么它对其中一个部分有效,而对另一个完全无效。有没有让搜索正常工作的提示C++ C++;尝试查看输入字符串是否等于类的数组字符串,c++,arrays,string,class,C++,Arrays,String,Class,我正在尝试按书名或ISBN在我的数组中搜索一本书。但是,我无法使用我的搜索功能找到数组中已经存在的书籍。我不知道它为什么不起作用。我认为代码是正确的。搜索第一对条目的ISBN有效,其余条目则失败。但是,搜索所有标题都失败。由于每个人的代码几乎相同,我不知道为什么它对其中一个部分有效,而对另一个完全无效。有没有让搜索正常工作的提示 // BooksProgram.cpp : This file contains the 'main' function. Program execution begi
// BooksProgram.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <string>
#include "Book_Class.h"
#include <fstream>
#include <cctype>
using namespace std;
void readData(Book_Class books[]);
void MainMenu(int& menu);
bool searchLibrary(Book_Class books[]);
int main()
{
int menu = 0;
Book_Class books[100];
readData(books);
while (menu != 5)
{
MainMenu(menu);
if (menu == 1)
{
searchLibrary(books);
}
else if (menu == 2)
{
}
else if (menu == 3)
{
}
else if (menu == 4)
{
}
else if (menu == 5)
{
}
}
}
void readData(Book_Class books[])
{
ifstream in;
string line;
int numAuthors = 0;
int i = 0;
int year = 0;
double price = 0;
int copies = 0;
string author1;
string author2;
string author3;
string author4;
in.open("Books.txt");
if (in.fail())
{
cout << "File did not open." << endl;
}
while (!in.eof())
{
getline(in, line);
numAuthors = stoi(line);
books[i].setNumAuthors(numAuthors);
if (numAuthors >= 5)
{
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 4)
{
getline(in, line);
author1 = line;
getline(in, line);
author2 = line;
getline(in, line);
author3 = line;
getline(in, line);
author4 = line;
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 3)
{
getline(in, line);
author1 = line;
getline(in, line);
author2 = line;
getline(in, line);
author3 = line;
author4 = "";
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 2)
{
getline(in, line);
author1 = line;
getline(in, line);
author2 = line;
author3 = "";
author4 = "";
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 1)
{
getline(in, line);
author1 = line;
author2 = "";
author3 = "";
author4 = "";
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
i++;
}
in.close();
}
void MainMenu(int& menu)
{
cout << setw(20) << "Main Menu" << endl;
cout << setw(36) << setfill('*') << "\n" << endl;
cout << "1. Search for book by Title or ISBN." << endl;
cout << "2. Add a new book to inventory." << endl;
cout << "3. Update the number of copies on hand." << endl;
cout << "4. Update the price of a book." << endl;
cout << "5. Exit" << endl;
cout << setw(36) << "\n" << endl;
cout << "Please enter a menu number to proceed: " << endl;
cin >> menu;
while (menu != 1 && menu != 2 && menu != 3 && menu != 4 && menu != 5)
{
cout << "Menu options are 1 - 6" << endl;
cout << "Please enter a menu number to proceed: " << endl;
cin.clear();
cin.ignore(100, '\n');
cin >> menu;
}
cout << setfill(' ') << "\n" << endl;
}
bool searchLibrary(Book_Class books[])
{
int search;
int i = 0;
int numAuthors = 0;
bool flag = false;
string title;
string ISBN;
cout << "Please enter 1 to search by Title or 2 to search by ISBN: " << endl;
cin >> search;
while (search != 1 && search != 2)
{
cout << "Invalid Selection." << endl;
cout << "Please enter 1 to search by Title or 2 to search by ISBN:" << endl;
cin.clear();
cin.ignore(100, '\n');
cin >> search;
}
if (search == 1)
{
cout << "Please enter the title of the book: " << endl;
cin >> title;
while (i < sizeof(books) -1)
{
if (title == books[i].getTitle())
{
flag = true;
books[i].showTitle();
numAuthors = books[i].getNumAuthors();
books[i].showAuthor(numAuthors);
books[i].showPublisher();
books[i].showYear();
books[i].showPrice();
books[i].showInStock();
break;
}
else
{
flag = false;
i++;
}
}
}
else if (search == 2)
{
cout << "Please enter the ISBN: " << endl;
cin >> ISBN;
while (i < sizeof(books) - 1)
{
if (ISBN == books[i].getISBN())
{
flag = true;
books[i].showTitle();
numAuthors = books[i].getNumAuthors();
books[i].showAuthor(numAuthors);
books[i].showPublisher();
books[i].showYear();
books[i].showPrice();
books[i].showInStock();
break;
}
else
{
flag = false;
i++;
}
}
}
if (!flag)
{
cout << "The book is not in stock." << endl;
}
return flag;
}
我认为(书)的大小表示得到书的大小并不像你期望的那样有效。试试这个计算函数readData中大小的程序
#include <iostream>
#include <string>
#include "Book_Class.h"
#include <fstream>
#include <cctype>
using namespace std;
int readData(Book_Class books[]); //Changed
void MainMenu(int& menu);
bool searchLibrary(Book_Class books[], int sizeOfBooks); //Changed
int main()
{
int menu = 0;
Book_Class books[100];
int sizeOfBooks = readData(books); //Changed
while (menu != 5)
{
MainMenu(menu);
if (menu == 1)
{
searchLibrary(books, sizeOfBooks); //Changed
}
else if (menu == 2)
{
}
else if (menu == 3)
{
}
else if (menu == 4)
{
}
else if (menu == 5)
{
}
}
}
int readData(Book_Class books[]) //Changed
{
ifstream in;
string line;
int numAuthors = 0;
int i = 0;
int year = 0;
double price = 0;
int copies = 0;
string author1;
string author2;
string author3;
string author4;
in.open("Books.txt");
if (in.fail())
{
cout << "File did not open." << endl;
}
while (!in.eof())
{
getline(in, line);
numAuthors = stoi(line);
books[i].setNumAuthors(numAuthors);
if (numAuthors >= 5)
{
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 4)
{
getline(in, line);
author1 = line;
getline(in, line);
author2 = line;
getline(in, line);
author3 = line;
getline(in, line);
author4 = line;
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 3)
{
getline(in, line);
author1 = line;
getline(in, line);
author2 = line;
getline(in, line);
author3 = line;
author4 = "";
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 2)
{
getline(in, line);
author1 = line;
getline(in, line);
author2 = line;
author3 = "";
author4 = "";
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 1)
{
getline(in, line);
author1 = line;
author2 = "";
author3 = "";
author4 = "";
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
i++;
}
in.close();
return i; //Changed
}
void MainMenu(int& menu)
{
cout << setw(20) << "Main Menu" << endl;
cout << setw(36) << setfill('*') << "\n" << endl;
cout << "1. Search for book by Title or ISBN." << endl;
cout << "2. Add a new book to inventory." << endl;
cout << "3. Update the number of copies on hand." << endl;
cout << "4. Update the price of a book." << endl;
cout << "5. Exit" << endl;
cout << setw(36) << "\n" << endl;
cout << "Please enter a menu number to proceed: " << endl;
cin >> menu;
while (menu != 1 && menu != 2 && menu != 3 && menu != 4 && menu != 5)
{
cout << "Menu options are 1 - 6" << endl;
cout << "Please enter a menu number to proceed: " << endl;
cin.clear();
cin.ignore(100, '\n');
cin >> menu;
}
cout << setfill(' ') << "\n" << endl;
}
bool searchLibrary(Book_Class books[], int sizeOfBooks) //Changed
{
int search;
int i = 0;
int numAuthors = 0;
bool flag = false;
string title;
string ISBN;
cout << "Please enter 1 to search by Title or 2 to search by ISBN: " << endl;
cin >> search;
while (search != 1 && search != 2)
{
cout << "Invalid Selection." << endl;
cout << "Please enter 1 to search by Title or 2 to search by ISBN:" << endl;
cin.clear();
cin.ignore(100, '\n');
cin >> search;
}
if (search == 1)
{
cout << "Please enter the title of the book: " << endl;
cin >> title;
while (i < sizeOfBooks -1) //Changed
{
if (title == books[i].getTitle())
{
flag = true;
books[i].showTitle();
numAuthors = books[i].getNumAuthors();
books[i].showAuthor(numAuthors);
books[i].showPublisher();
books[i].showYear();
books[i].showPrice();
books[i].showInStock();
break;
}
else
{
flag = false;
i++;
}
}
}
else if (search == 2)
{
cout << "Please enter the ISBN: " << endl;
cin >> ISBN;
while (i < sizeOfBooks - 1) //Changed
{
if (ISBN == books[i].getISBN())
{
flag = true;
books[i].showTitle();
numAuthors = books[i].getNumAuthors();
books[i].showAuthor(numAuthors);
books[i].showPublisher();
books[i].showYear();
books[i].showPrice();
books[i].showInStock();
break;
}
else
{
flag = false;
i++;
}
}
}
if (!flag)
{
cout << "The book is not in stock." << endl;
}
return flag;
}
#包括
#包括
#包括“h类图书”
#包括
#包括
使用名称空间std;
int readData(图书类图书[])//改变
无效主菜单(内部和菜单);
布尔搜索图书馆(图书类图书[],国际图书)//改变
int main()
{
int菜单=0;
图书类图书[100];
int-sizeOfBooks=readData(books);//已更改
while(菜单!=5)
{
主菜单(菜单);
如果(菜单==1)
{
searchLibrary(书籍、大小书籍);//已更改
}
否则如果(菜单==2)
{
}
否则如果(菜单==3)
{
}
else if(菜单==4)
{
}
否则如果(菜单==5)
{
}
}
}
int readData(Book_Class books[])//已更改
{
如果输入;
弦线;
int numAuthors=0;
int i=0;
整年=0;
双倍价格=0;
整数份数=0;
字符串author1;
字符串author2;
字符串author3;
字符串author4;
in.open(“Books.txt”);
if(in.fail())
{
你可以使用for循环来读取适当数量的作者,而不是每个可能数量的作者的if/else案例。谢谢你的提示。我能做到。我明白你的意思。我遇到的问题是我的搜索函数无法正确搜索标题或ISBN。它适用于ISBN,就像前3个数组元素一样仅此而已。根本不适用于标题。不确定您的数据是什么,但一般来说,通常有四位以上的作者;如果您希望将来验证您的代码,我会考虑将此规范化为另一个表。有5000多名作者。这是一个类分配,他们希望我们使用的作者不超过四位。我无法做到这一点工作。它不会让我将void函数readData初始化为int。另外,我继续,只是将99硬编码到搜索函数中的while循环中,因为我知道数组的大小,搜索函数仍然找不到标题,只找到ISBN的前三个。不确定为什么它只捕获前三个ISBN,而不捕获其余的。好的。我我听了你说的一切,但仍然不起作用。我们应该在启动程序时只填充数组中的10个条目,但数组应该有100个元素。
5
C++Programing: From Problem Analysis to Program Design
5-17-525281-3
ABC
2000
52.50
20
1
Malik, D.S.
Fuzzy Discrete Structures
3-7908-1335-4
Physica-Verlag
2000
89.00
10
2
Malik, Davender
Mordeson, John
Fuzzy Mathematic in Medicine
3-7908-1325-7
Physica-Verlag
2000
89.00
10
3
Mordeson, John
Malik, Davender
Cheng, Shih-Chung
Harry John and The Magician
0-239-23635-0
McArthur A. Devine Books
1999
19.95
10
3
Goof, Goofy
Pluto, Peter
Head, Mark
Dynamic InterWeb Programming
22-99521-453-1
GNet
1998
39.99
25
1
Hemingway, Ernest
The Sun Also Rises
978-8087888155
Scribner's
1954
19.99
10
1
Hemingway, Ernest
The Old Man and the Sea
978-1781396803
Benediction Books
1952
17.99
25
1
Carroll, Lewis
Alice in Wonderland
9783959401807
MacMillan Publishers
1865
10.00
15
1
Tolkein, J.R.R.
The Fellowship of the Ring
978-0544003415
Mariner Books
1954
29.99
102
1
Tolkein, J.R.R.
The Two Towers
978-0345339713
Mariner Books
1954
19.99
54
#include <iostream>
#include <string>
#include "Book_Class.h"
#include <fstream>
#include <cctype>
using namespace std;
int readData(Book_Class books[]); //Changed
void MainMenu(int& menu);
bool searchLibrary(Book_Class books[], int sizeOfBooks); //Changed
int main()
{
int menu = 0;
Book_Class books[100];
int sizeOfBooks = readData(books); //Changed
while (menu != 5)
{
MainMenu(menu);
if (menu == 1)
{
searchLibrary(books, sizeOfBooks); //Changed
}
else if (menu == 2)
{
}
else if (menu == 3)
{
}
else if (menu == 4)
{
}
else if (menu == 5)
{
}
}
}
int readData(Book_Class books[]) //Changed
{
ifstream in;
string line;
int numAuthors = 0;
int i = 0;
int year = 0;
double price = 0;
int copies = 0;
string author1;
string author2;
string author3;
string author4;
in.open("Books.txt");
if (in.fail())
{
cout << "File did not open." << endl;
}
while (!in.eof())
{
getline(in, line);
numAuthors = stoi(line);
books[i].setNumAuthors(numAuthors);
if (numAuthors >= 5)
{
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 4)
{
getline(in, line);
author1 = line;
getline(in, line);
author2 = line;
getline(in, line);
author3 = line;
getline(in, line);
author4 = line;
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 3)
{
getline(in, line);
author1 = line;
getline(in, line);
author2 = line;
getline(in, line);
author3 = line;
author4 = "";
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 2)
{
getline(in, line);
author1 = line;
getline(in, line);
author2 = line;
author3 = "";
author4 = "";
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 1)
{
getline(in, line);
author1 = line;
author2 = "";
author3 = "";
author4 = "";
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
i++;
}
in.close();
return i; //Changed
}
void MainMenu(int& menu)
{
cout << setw(20) << "Main Menu" << endl;
cout << setw(36) << setfill('*') << "\n" << endl;
cout << "1. Search for book by Title or ISBN." << endl;
cout << "2. Add a new book to inventory." << endl;
cout << "3. Update the number of copies on hand." << endl;
cout << "4. Update the price of a book." << endl;
cout << "5. Exit" << endl;
cout << setw(36) << "\n" << endl;
cout << "Please enter a menu number to proceed: " << endl;
cin >> menu;
while (menu != 1 && menu != 2 && menu != 3 && menu != 4 && menu != 5)
{
cout << "Menu options are 1 - 6" << endl;
cout << "Please enter a menu number to proceed: " << endl;
cin.clear();
cin.ignore(100, '\n');
cin >> menu;
}
cout << setfill(' ') << "\n" << endl;
}
bool searchLibrary(Book_Class books[], int sizeOfBooks) //Changed
{
int search;
int i = 0;
int numAuthors = 0;
bool flag = false;
string title;
string ISBN;
cout << "Please enter 1 to search by Title or 2 to search by ISBN: " << endl;
cin >> search;
while (search != 1 && search != 2)
{
cout << "Invalid Selection." << endl;
cout << "Please enter 1 to search by Title or 2 to search by ISBN:" << endl;
cin.clear();
cin.ignore(100, '\n');
cin >> search;
}
if (search == 1)
{
cout << "Please enter the title of the book: " << endl;
cin >> title;
while (i < sizeOfBooks -1) //Changed
{
if (title == books[i].getTitle())
{
flag = true;
books[i].showTitle();
numAuthors = books[i].getNumAuthors();
books[i].showAuthor(numAuthors);
books[i].showPublisher();
books[i].showYear();
books[i].showPrice();
books[i].showInStock();
break;
}
else
{
flag = false;
i++;
}
}
}
else if (search == 2)
{
cout << "Please enter the ISBN: " << endl;
cin >> ISBN;
while (i < sizeOfBooks - 1) //Changed
{
if (ISBN == books[i].getISBN())
{
flag = true;
books[i].showTitle();
numAuthors = books[i].getNumAuthors();
books[i].showAuthor(numAuthors);
books[i].showPublisher();
books[i].showYear();
books[i].showPrice();
books[i].showInStock();
break;
}
else
{
flag = false;
i++;
}
}
}
if (!flag)
{
cout << "The book is not in stock." << endl;
}
return flag;
}