确定。所以我有一个任务,加载一个书籍文件和他们的作者顺序行。我已将标题加载到书名列表中,并将书籍作者加入书籍作者数组中。 我需要做以下事情。
显示所有书籍和作者。 <<<完成。
显示来自用户输入AUTHOR搜索字符串的所有书籍和作者。 <<半作品
从用户输入BOOK搜索字符串显示所有书籍和作者。 <<半作品
最后两个有问题
作者功能。
用户输入:Malik
输出:0作者发现! (14次)
标题查询相同。
我已经在网上搜索了一百万次阅读我的书。好吧也许没那么多。尝试了一百个不同版本的代码并且仍在受到伤害。任何帮助都会很棒!
这是我的代码。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//these declarations should be at file scope
const int ARRAY_SIZE = 1000;
string bookTitle [ARRAY_SIZE];
string bookAuthor [ARRAY_SIZE];
int nob = 0;
ifstream dataBase;
// Function prototypes
int loadData(string pathName);//complete
void showAll(int nob);
int showBooksByAuthor (int nob, string name);
int showBooksByTitle (int nob, string title);
void showChoices();
int main()
{
string pathName;
int x = 1;
char menu;
cout << "Welcome to Phil's Library Database. " << endl;
cout << "Enter the location of the Database: (ex: c:/filename.txt ";
cin >> pathName;
//read pathName and input data to arrays
dataBase.open(pathName);
loadData(pathName);
//output number of files loaded
cout << nob <<" records loaded successfully." <<endl;
string name;
string title;
while(x == 1)
{
//prompt User for what they want to do
cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
cin >> menu;
switch(menu)
{
case 'Q':
case 'q':
//exit out of program
x = 3;
break;
case 'A':
case 'a':
cout << "Author's Name: ";
getline (cin, name, '\n');
//list books by author
showBooksByAuthor (nob,name);
break;
case 'T':
case 't':
cout << "Book Title: ";
getline (cin, title, '\n');
showBooksByTitle (nob, title);
break;
case 'S':
case 's':
//cout the entire documents array-ed contents
showAll(nob);
break;
}
}
system("pause");
getchar();
return 0;
}
int loadData(string pathName)
{
if(dataBase.is_open())
{
while(!dataBase.eof())
{
getline(dataBase, bookTitle[nob], '\n');
getline(dataBase, bookAuthor[nob], '\n');
nob += 1;
}
dataBase.clear();
dataBase.seekg(0);
return nob;
}
else
cout << "Bad file path!"<< endl;
return -1;
}
//showall Function
void showAll(int nob)
{
for (int i = 0; i < nob; i++)
{
//show all content of each array
cout << bookTitle[i]<<endl;
cout << bookAuthor[i]<<endl;
}
}
//showall books based on Author
int showBooksByAuthor (int nob, string name)
{
int x = 0;
for(int i = 0; i < nob; i++)
{
if (bookAuthor[i].find(name))
{
//output array location data
cout << bookTitle[i];
cout << "(" << bookAuthor[i] << ")" << endl;
//add onto counter for output of successfully searched items
x++;
}
cout << x << " Records found. " << endl;
}
return x;
}
//showall books based on Title
int showBooksByTitle (int nob, string title)
{
int x = 0;
for(int i = 0; i < nob; i++)
{
if (bookAuthor[i].find(title))
{
//output array location data
cout << bookTitle[i];
cout << "(" << bookAuthor[i] << ")" << endl;
//add onto counter for output of successfully searched items
x++;
}
cout << x << " Records found. " << endl;
}
return x;
}
提前谢谢!
答案 0 :(得分:0)
根据此页面http://www.cplusplus.com/reference/string/string/find/和此http://www.cplusplus.com/reference/string/string/npos/,当没有匹配项时,String上的find()方法返回-1。
只要if ( some_num )
非零, some_num
为真,包括-1,其中some_num
是任何表达式。
仔细阅读查找文档,了解如何使用它来检查匹配项。
答案 1 :(得分:0)
1)不需要find
功能。您需要做的就是使用==
。
2)对于标题,您应该测试bookTitle
数组,而不是bookAuthor
。
int showBooksByAuthor (int nob, string name)
{
// ...
// Search for author
if ( bookAuthor[i] == name)
{
// name found
}
// ...
}
// Search for title
int showBooksByTitle (int nob, string title)
{
//...
if (bookTitle[i] == title)
{
// title found
}
//...
}