到目前为止,这是我的代码:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
int count = 0;
string fileName;
string keyWord;
string word;
cout << "Please make sure the document is in the same file as the program, thank you!"
<< endl << "Please input document name: " ;
getline(cin, fileName);
cout << endl;
cout << "Please input the word you'd like to search for: " << endl;
cin >> keyWord;
cout << endl;
ifstream infile(fileName.c_str());
while(infile.is_open())
{
getline(cin,word);
if(word == keyWord)
{
cout << word << endl;
count++;
}
if(infile.eof())
{
infile.close();
}
}
cout << count;
}
我不确定如何进入下一个单词,目前这个无限循环......有什么建议吗?
另外......我如何告诉它打印出那个单词所在的行?
提前致谢!