0

我的程序旨在从包含标题和作者列表的文件中获取输入。该文件如下所示:

标题
相关作者
下一个标题
相关作者
等等

我遇到的问题是我的 showBooksByTitle 和 showBooksByAuthor 函数。现在这段代码只返回完全匹配并且还打印一个空的换行符和一个带有一些空格和 () 的新行。

当然,非常感谢任何帮助。这是我编程的第一年。为了安全起见,我已经包含了整个代码,我不会遗漏任何可能出现问题的东西。

#include <iostream>
#include<string>
#include<fstream>
#include<cstring>

using namespace std;

struct Book {
    string title;
    string author;
};

const int ARRAY_SIZE = 1000;
Book books [ARRAY_SIZE];

int loadData (string);
void showAll (int);
int showBooksByAuthor (int, string);
int showBooksByTitle (int, string);

int main() {
    //Declare variables
    string pathname;
    string title;
    string name;
    string word;
    int count;
    char response;

    //ask user for pathname
    cout << "What is the path of the library file? ";
    cin >> pathname;
    cout << endl;
    count = loadData(pathname);

    //input data into arrays
    loadData(pathname);
    cout << endl << count << " records loaded successfully." << endl << endl;

    //Show user menu
    cout << "Please enter Q to Quit, A to search for the Author, T to search for the Title, "
    << endl << "or S to Show all: ";
    cin >> response;

    switch(response) {
        case 'q':
            break;
        case 'Q':
            break;
        case 'a':
            cout << endl << "Please enter author's name: ";
            cin >> name;
            showBooksByAuthor(count, name);
            break;
        case 'A':
            cout << endl << "Please enter author's name: ";
            cin >> name;
            showBooksByAuthor(count, name);
            break;
        case 't':
            cout << endl << "Please enter all or part of the title: ";
            cin >> title;
            showBooksByTitle(count, title);
            break;
        case 'T':
            cout << endl << "Please enter all or part of the title: ";
            cin >> title;
            showBooksByTitle(count, title);
            break;
        case 's':
            cout << endl;
            showAll(count);
            break;
        case 'S':
            cout << endl;
            showAll(count);
            break;
        default:
            cout << endl << "Invaled input, please try again: ";
            break;
    }

    //pause and exit
    cout << endl;
    system("PAUSE");
    return 0;
}


int loadData(string pathname) {
    int i = 0;
    int j = 0;
    ifstream library;

    //open file, if not successful, output error message
    library.open(pathname.c_str());
    if (!library.is_open()) {
        cout << "Unable to open input file." << endl;
        return -1;
    }
    //reads title and author from file into designated string
    //this is assuming title comes first and author comes after
    while(!library.eof()) {
        getline(library, books[i].title);
        getline(library, books[i].author);
        i++;
    }
    return i;
}

void showAll (int count) {
    for (int i = 0; i < count; i++) {
        cout << books[i].title << " (" << books[i].author << ")" << endl;
    }
}

int showBooksByAuthor(int count, string name) {
    int found;
    for(int n = 0; n < 28; n++) {
        found = name.find(books[n].author);
        if(found != string::npos) {
            cout << endl << books[n].title << " (" << books[n].author << ")" << endl;
        }
    }
    return 0;
}


int showBooksByTitle (int count, string title) {
    int found;
    for(int n = 0; n < 28; n++) {
        found = title.find(books[n].title);
        if(found !=string::npos) {
            cout << endl << books[n].title << " (" << books[n].author << ")" << endl;
        }
    }
    return 0;
}
4

2 回答 2

1

意外的输出是因为你读错了数据文件。直到您尝试对流执行操作后才会设置流 EOF 标志,因此您的循环会迭代一到多次。

将循环更改为loadData

while(getline(library, books[i].title) && getline(library, books[i].author))
    i++;

这使用了返回流的事实std::getline,并且流可以用作真/假值。

于 2013-03-18T11:53:47.170 回答
1

Joachim 已经指出了主要问题。我想添加一些评论评论:

  1. C++ 有一个可变长度数组,即std::vector. 它还有一个链表和其他容器。所以不要让书籍成为一个固定的数组,而是使用一些标准的容器。
  2. 使用迭代器迭代事物,而不是索引。对于数组,指针是迭代器(C++11 具有end()获取刚刚超过数组末尾的指针的功能;在 C++03 中编写自己是微不足道的,但无论如何你不应该使用普通的旧数组)。
  3. 您正在输入字符串中查找书名/书名。如果你想要子字符串匹配,你应该反过来做。
于 2013-03-18T12:05:32.390 回答