使用 getline 和字符串流检查句子中的单词是否在文件中
Using getline and stringstream to check if words in a sentence are in a file
我已经在做一项作业好几天了,我似乎无法弄清楚这一点。
我得到了 4 个包含单词的文件(形容词、名词、代词和动词),我必须检查用户输入的句子并确定句子的结构以及不同类型的单词是否按特定顺序出现。
我的方法是使用 getline 来获取句子,然后使用 istringstream 查看句子中的每个单词,并将每个单词与每个文件进行比较。
例如:我会得到第一个单词,遍历形容词文件并确定它是否是形容词。如果不是,那么我会查看同一个单词并遍历名词文件并确定它是否是名词,依此类推。
我不能为此分配使用数组、向量或字符串函数。这真的让我很失望。我想使用 istringstream 来查看句子中的每个单词,但我似乎无法做到这一点。
例如:在查看第一个单词并确定它是形容词、名词、代词还是动词之后,我想让他们使用 istringstream 查看句子中的第二个单词,依此类推。这是行不通的,我也不知道为什么。
在我完成这项工作后,我计划将每个单词放入布尔函数中,即检查第一个单词是否是形容词、名词、代词或动词,并根据它的位置返回 true。
我是C++的初学者,我快要疯了,试图做到这一点。有没有更好的方法?我想做的事情有什么问题?这是我到目前为止所拥有的:
string sentence;
string fileWord;
cout << "Input your sentence : " << endl;
getline(cin, sentence);
stringstream iss(sentence);
string firstWord;
string secondWord;
ifstream adjectiveFile;
adjectiveFile.open("/words/adjectives");
// Outputs error if there's an error opening the file
if(adjectiveFile.fail()){
cout << "Failed to open file" << endl;
exit(1);
}
// Opens noun file
ifstream nounFile;
nounFile.open("/words/nouns");
// Outputs error if there's an error opening the file
if(nounFile.fail()){
cout << "Failed to open noun file" << endl;
exit(1);
}
// Opens pronouns file
ifstream pronounFile;
pronounFile.open("/words/pronouns");
// Outputs error if there's an error opening the file
if(pronounFile.fail()){
cout << " Failed to open pronoun file" << endl;
exit(1);
}
// Opens verbs file
ifstream verbFile;
verbFile.open("/words/verbs");
// Outputs error if there's an error opening the file
if(verbFile.fail()){
cout << "Failed to open verb file" << endl;
exit(1);
}
// Gets first word from sentence and them determines what type of word it is by looping through each file
iss >> firstWord;
while (adjectiveFile >> fileWord) {
if (firstWord == fileWord) {
cout << "The first word of the sentence is the adjective " << firstWord << endl;
}
}
while(nounFile >> fileWord){
if(firstWord == fileWord){
cout << "The first word of the sentence is the noun " << firstWord << endl;
}
}
while(pronounFile >> fileWord){
if(firstWord == fileWord){
cout << "The first word of the sentence is the pronoun " << firstWord << endl;
}
}
while(verbFile >> fileWord){
if(firstWord == fileWord){
cout << "The first word of the sentence is the verb " << firstWord << endl;
}
}
// Doesn't work. Can't access the second word of the sentence using istringstream.
iss >> secondWord;
while(adjectiveFile >> fileWord){
if(secondWord == fileWord){
cout << "The second word of the sentence is the adjective " << secondWord << endl;
}
}
while(nounFile >> fileWord){
if(secondWord == fileWord){
cout << "The second word of the sentence is the noun " << secondWord << endl;
}
}
while(pronounFile >> fileWord){
if(secondWord == fileWord){
cout << "The second word of the sentence is the pronoun " << secondWord << endl;
}
}
while(verbFile >> fileWord){
if(secondWord == fileWord){
cout << "The second word of the sentence is the verb " << secondWord << endl;
}
}
return 0;
}
您正在尝试多次读取同一文件。在您第一次运行while(adjectiveFile >> fileWord){}
后,指针将位于文件的末尾,因此下次您尝试调用同一循环时,adjectiveFile >> fileWord
将失败,并且您永远不会真正检查是否secondWord == fileWord
。
你可以看看ifstream::seekg()
和ifstream::clear()
,看看如何读两遍。话虽如此,正如上面的评论中提到的,复制和粘贴相同的代码 4x 肯定不是最好的方法。
- 文本文件中的单词链表
- 在指针的帮助下,文本文件中单词的频率
- 我们如何在文本文件中找到C++中的几个单词?
- 是否可以在文本文件中找到最长单词的长度,并在同一个文本文件中读取,只需 1 个 while 循环?
- 按单词搜索文件,然后在C++中打印特定行数
- 如何检查流中文件的下一个单词是否为 alpha?
- 从文件中查找单词并替换到其他文件中
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 如何逐行从文件中获取单词,并在C++中用分号分隔?
- 如何从文件中读取单词,将它们分配给数组并分析其内容?
- 如何在 c++ 中从文本文件中获取特定单词?
- 我们如何重新制作程序以打开xml文件并在价格单词之后读取百分比值并更改此值
- 从之前添加的批处理文件中删除单词
- C++读取大文件并将其保存到字符串中,然后删除特定的随机单词
- C++ 如何检查哪些单词不在 2 个类似文件中
- 显示文件中具有最小给定元音数的单词
- 简单的二分搜索对我不起作用。我正在尝试从文本文件中搜索单词
- 使用结构数组计算文本文件中单词的出现次数C++
- 包含换行符分隔的单词的文件和C++中这些单词的字符串向量的大小是否相同?
- 将玩家输入与单词文件进行比较