【问题标题】:C++ Searching a String Array for every instance of a user given search term.C++ 为用户给定搜索词的每个实例搜索字符串数组。
【发布时间】: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

标签: c++ arrays search find


【解决方案1】:

根据此页面http://www.cplusplus.com/reference/string/string/find/ 和此http://www.cplusplus.com/reference/string/string/npos/,当没有匹配时,String 上的 find() 方法返回 -1。

只要some_num 不为零,包括-1,if ( some_num ) 就为真,其中some_num 是任何表达式。

仔细阅读 find 文档以了解如何使用它来检查匹配项。

【讨论】:

    【解决方案2】:

    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
          }
          //...
       }
    

    【讨论】:

    • 谢谢 Paul 我在发布我的问题之前尝试过,但它只找到字符串的第一个实例。这主要是我努力寻找说“Malik”并且只得到---->对象C ++编程:来自问题分析...... Malik而不是这个----> C ++编程:程序设计包括...... D.S.马利克
    猜你喜欢
    • 2019-02-12
    • 2013-11-28
    • 2021-10-06
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    相关资源
    最近更新 更多