我从事一项任务已有几天,但似乎无法弄清楚。

我给了4个文件,里面有单词(形容词,名词,代词和动词),我必须检查用户输入的句子并确定句子的结构,以及不同类型的单词是否以一定顺序出现。

我的方法是使用getline获取句子,然后使用istringstream查看句子中的每个单词并将每个单词与每个文件进行比较。

例如:我会得到第一个单词,遍历形容词文件并确定它是否是一个形容词。如果不是,那么我将查看相同的单词并遍历名词文件,确定其是否为名词,依此类推。

我不能将数组,向量或字符串函数用于此分配。这真让我失望。我想使用istringstream来查看句子中的每个单词,但似乎无法正常工作。

例如:在查看了第一个单词并确定它是一个形容词,名词,代词或动词之后,我希望他们使用istringstream来查看句子中的第二个单词,依此类推。这不起作用,我不确定为什么。

完成这项工作后,我计划将每个单词放入布尔函数,即检查第一个单词是形容词,名词,代词还是动词,并根据其为真返回真。

我是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++ - 使用getline和stringstream检查句子中的单词是否在文件中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42877077/

10-14 12:39
查看更多