搜索并行数组
Searching parallel arrays
我的程序设计用于从包含标题和作者列表的文件中获取输入。文件看起来像这样:
<>之前标题有关作者下一个冠军有关作者等。之前我的问题是我的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;
}
意外的输出是因为您错误地读取了数据文件。直到之后才设置流的EOF标志,所以循环迭代一次到多次。
将loadData
中的循环更改为:
while(getline(library, books[i].title) && getline(library, books[i].author))
i++;
这使用了std::getline
返回流的事实,并且流可以用作真/假值。
Joachim已经指出了主要问题。我想补充一些评论:
- c++有一个可变长度数组,
std::vector
。它还有一个链表和其他容器。所以不要把这些书做成固定的数组,而是使用一些标准的容器。 - 使用迭代器迭代,而不是索引。对于数组指针是迭代器(c++ 11有
end()
函数获取指针刚刚超过数组的结束;在c++ 03中编写是很简单的,但是你不应该使用普通的旧数组)。 - 您正在查找输入字符串中的图书名称/书名。如果你想要子字符串匹配,你应该用另一种方式来做。
相关文章:
- 并行用于C++17中数组索引范围内的循环
- OpenMP:并行更新数组总是需要减少数组吗
- 遍历并行数组以确定C++中的最大数字
- 排序并行数组
- 如何从具有两列的.txt文件创建并行数组?
- 是否可以在并行区域中为共享 2D 数组创建选定元素的线程本地副本?(共享,私有,障碍:OPenMP)
- 为什么并行读取数组会导致内存泄漏?
- C++赋值简介:并行数组
- 如何解决在使用动态 2D 数组进行矩阵乘法的 MPI 进行并行编程时的问题
- 循环函数中的并行数组
- 在OpenMP上的并行合并排序上的数组大小问题.如何将合并分类分类为更多任务
- 使用getline从文本文件中创建2个并行字符串数组
- 对于循环和并行数组:未声明的标识符
- 如何使用并行数组使用 c++ 制作包含数量和单词的列表
- 数组的并行求和比C++中的顺序求和慢
- 对一个具有重复元素的数组进行排序时更新并行数组
- 将2D数组行存储在并行数组中
- 将并行数组与 txt 文件分开
- 如何并行删除数组中的零值
- 需要帮助解决特征数组并行构造中的数据争用