【发布时间】:2014-04-01 15:33:57
【问题描述】:
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;
}
现在我的代码只有在我只输入一个单词时才有效。但是如果我输入一个句子,它不会输出任何东西。
所以我的问题是如果用户输入:你好早上好,我将如何在我的文本文件中搜索你好和早上好并输出它。
这是一个文本文件的例子:
今天天亮
祝你好运
早安你好
午后时间
晚安晚安
非常漂亮
【问题讨论】:
-
看起来你可以做一些研究:“stackoverflow read file word sentence”。
-
看起来您已经使用my code for reading in the dictionary 来解析用户输入,但它不会这样做。如果您要让别人为您编写代码,您需要了解它的作用!
标签: c++