gpt4 book ai didi

c++ - 搜索并行数组

转载 作者:行者123 更新时间:2023-11-28 03:20:49 24 4
gpt4 key购买 nike

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

titleassociated authornext titleassociated authoretc.

The problem I'm having is with my showBooksByTitle and showBooksByAuthor functions. Right now this code returns only exact matches and also prints an empty newline and a new line with some spaces and a ().

Of course any help is greatly appreciated. This is my first year programming. I've included the whole code just to be safe that I'm not leaving out anything that could be the problem.

#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;
}

最佳答案

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

loadData 中的循环更改为:

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

这利用了 std::getline 返回流的事实,并且流可以用作 true/false 值。

关于c++ - 搜索并行数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15476062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com