【发布时间】:2014-04-29 09:35:58
【问题描述】:
首先,这是我到目前为止的代码:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int arraysize = 35;
int i = 0;
string line;
string searchTerm;
int main()
{
string words[arraysize];
ifstream wordFile;
wordFile.open ("wordFile.txt");
if (wordFile.is_open())
{
while (! wordFile.eof())
{
getline (wordFile, line);
words[i] = line;
i++;
}
wordFile.close();
}
else
{
cout << "Unable to open file" << endl;
}
for (int x = 0; x < arraysize; x++)
{
cout << words[x] << " ";
}
cout << "\n\nEnter in a word you would like to search in the story above:" << endl;
cin >> searchTerm;
for (int y = 0; y < arraysize; y++)
{
if (words[y].compare(searchTerm) !=0)
{
cout << "No match found" << endl;
}
}
}
到目前为止,我所拥有的是从文本文件读取然后打印这些单词的程序。我接下来要做的是让用户输入他们想在文本文件中搜索的单词,如果有像他们输入的单词一样的单词,如果没有打印出“没有单词”,则打印该单词就像在文本文件中一样” 我只是无法弄清楚搜索,关于如何做到这一点的任何建议?
【问题讨论】:
标签: c++ arrays string file search