char option;
cout <<"What langauge would you like to translate from - English 'E' or French 'F': ";
cin >> option;
cin.ignore();
ifstream in;
in.open("Q3.dic");
size_t pos;
string phrase;
if (option == 'E')
{
cout <<"please enter a sentence in English: ";
getline(cin, phrase);
for(string english, french;
getline(in, english, '\t') && getline(in, french);
)
{
pos = english.find(phrase);
if(pos != string::npos)
{
cout << french <<endl;
break;
}
//cout << english << '\t' << french <<endl;
}
}
else if (option == 'F')
{
cout <<"please enter a sentence in French: ";
getline(cin, phrase);
for(string english, french;
getline(in, english, '\t') && getline(in, french);
)
{
pos = french.find(phrase);
if(pos != string::npos)
{
cout << english <<endl;
break;
}
//cout << english << '\t' << french <<endl;
}
}
in.close();
cout <<endl;
system("pause");
return 0;
}
现在我的代码只有在我只键入一个单词时才有效。但如果我输入一个句子,它就不会输出任何东西。
所以我的问题是如果用户输入:你好早上好,我如何在我的文本文件中搜索你好和早上好并输出它。
这是文本文件的示例:
今天aujourd'hui
好朋友早上好bonjour
下午après-midi
晚上好的bonsoir很多beaucoup
答案 0 :(得分:1)
看起来你需要一个三步过程:
1)阅读一行文字
2)从文本字符串中提取英语单词
3)从文本字符串中提取法语单词。
你应该研究一个很好的数据结构:std :: istringstream。这允许您将字符串视为输入流。
另一种方法是使用find
的{{1}}方法。