我想让用户搜索作者姓名或图书年份。一旦他们这样做,我希望它可以打印出所有发现的结果。
我提供了一个开关盒,打开了文件,甚至还提供了搜索功能。但是我的搜索功能似乎无法正常工作,我无法知道原因。
如果您可以看一下我的搜索功能。也许您会发现我似乎找不到的错误?
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
ifstream input("C:/Users/mziad/Desktop/School/C++/HW2/bestsellers.txt");
string line;
vector <string> v;
string temp;
int index, prev;
int i;
if (input.is_open())
{
while (!input.eof())
{
int data;
getline(input, line);
v.push_back(line);
}
}
char choice;
int x, y;
string m, n;
string author, title;
while (1)
{
cout << "What would you like to do?" << endl;
cout << "1: Look up year range" << endl;
cout << "2: Look up month / year" << endl;
cout << "3 : Search for author" << endl;
cout << "4 : Search for title" << endl;
cout << "Q: Quit" << endl;
cin >> choice;
switch (choice)
{
case '1':
cout << "Enter beginning year: ";
cin >> x;
cout << "Enter ending year: ";
cin >> y;
break;
case '2':
cout << "Enter month (as a number, 1-12): ";
cin >> m;
cout << "Enter year: ";
cin >> n;
break;
case '3':
cout << "Enter an author's name (or part of a name): ";
cin >> author;
break;
case '4':
cout << "Enter a title (or part of a title): ";
cin >> title;
break;
case 'Q':
exit(0);
default:
cout << "Invalid choice\n";
}
bool isFound = 0;
while (!input.eof())
{
string temp = "";
getline(input, temp);
for (int i = 0; i < line.size(); i++)
{
if (temp[i] == line[i])
isFound = 1;
else
{
isFound = 0;
break;
}
}
if (isFound)
{
cout << "Your Search Query: ";
for (int i = line.size() + 1; i < temp.size(); i++)
cout << temp[i];
break;
}
}
if (input.eof() && (!isFound))
{
cout << "Name not found!\n";
}
input.close();
}
}
我希望它搜索用户输入并返回结果。它只是说“找不到名称”,或者循环返回选项而不打印任何结果。