2014-04-01 73 views
-1
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

良好的盂兰盆

早上好,卓悦

下午滑雪后-MIDI

晚上好bonsoir

多beaucoup

+0

看起来你可以做一些研究:“stackoverflow读取文件字句”。 –

+0

看起来你已经使用[我的代码在字典中阅读](http://stackoverflow.com/a/22772641/78845)解析用户输入,它不会这样做。如果您要让人们为您编写代码,您需要了解它的功能! – Johnsyweb

+0

[我想从英语翻译成法语和反义词]可能的重复(http://stackoverflow.com/questions/22768038/i-want-to-translate-from-english-to-french-and-viceversa) – Johnsyweb

回答

1

看起来你需要三个步骤:
1)阅读一行文本。
2)从文本字符串中提取英文单词。
3)从文本字符串中提取法语单词。

有一个很好的数据结构,你应该看看:std :: istringstream。这使您可以将字符串视为输入流。

另一种方法是使用std::stringfind方法。

相关问题