【发布时间】:2014-11-30 04:24:35
【问题描述】:
好的。所以我有一个任务,加载一个书籍文件和他们的作者顺序行。我已将书名加载到书名数组中,将书名加载到书名数组中。 我需要发生以下情况。
显示所有书籍和作者。
显示来自用户输入作者搜索字符串的所有书籍和作者。
从用户输入的 BOOK 搜索字符串中显示所有书籍和作者。
最后两个有问题
作者功能。
用户输入:马利克
输出:找到 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;
}
提前谢谢你!
【问题讨论】:
-
你了解
struct吗?通常这样做的方法是拥有一个具有作者和标题的结构,并创建一个该结构的数组。 -
您的要求不明确,尤其是
book搜索。您搜索一本书,然后显示该书的作者。是这样吗?还是你搜索这本书,找到作者,然后列出该作者的所有其他书籍?或者您是否搜索任何具有该标题的书?... -
还没学过结构体。不幸的是,那是下周,我的老师说,你需要做的就是布置数组,这周是“数组”周。
-
至于要求它应该像这样工作:您搜索作者,输出将是作者贡献的所有书籍的标题和作者。文件内容看起来像 Objects First 与Java Barnes and Kolling` 游戏开发要点 Novak The Game Maker's Apprentice Overmars C++ 编程:从问题分析... Malik C++ 编程实验手册 Scholl 开始 LINUX 编程 Stones 和 Matthew C++ 编程:程序设计包括... D. S. Malik