0

我想让标有 // THIS LINE SHOULD BE PRINTING 的行做它的事情,即打印“同义词”和“反义词”之间的 int 值。

这是文本文件:

字典.txt

1 cute
2 hello
3 ugly
4 easy
5 difficult
6 tired
7 beautiful
synonyms
1 7
7 1
antonyms
1 3
3 1 7
4 5
5 4
7 3






#include <iostream>
#include <fstream>
#include <string>

#include <sstream>
#include <vector>


using namespace std;

class WordInfo{

      public:

             WordInfo(){}

             ~WordInfo() {     
             }

             int id() const {return myId;}

             void readWords(istream &in)
             {
               in>>myId>>word;     
             }


             void pushSynonyms (string synline, vector <WordInfo> wordInfoVector)

             {

             stringstream synstream(synline);

             vector<int> synsAux;

             int num;

             while (synstream >> num) synsAux.push_back(num);

              for (int i=0; i<synsAux.size(); i++){
              cout<<synsAux[i]<<endl;  //THIS LINE SHOULD BE PRINTING

             }       



             }

             void pushAntonyms (string antline, vector <WordInfo> wordInfoVector)
             {

             }

             //--dictionary output function

             void printWords (ostream &out)
             {
                out<<myId<< " "<<word;     
             }



             //--equals operator for String
             bool operator == (const string &aString)const
             {
                           return word ==aString; 

             }


             //--less than operator

             bool operator <(const WordInfo &otherWordInfo) const
             { return word<otherWordInfo.word;}

             //--more than operator

             bool operator > (const WordInfo &otherWordInfo)const
             {return word>otherWordInfo.word;}

             private:
                   vector <int> mySynonyms;
                   vector <int> myAntonyms;
                   string word;
                   int myId;


      };

      //--Definition of input operator for WordInfo
      istream & operator >>(istream &in, WordInfo &word)
      {
         word.readWords(in); 

      }



      //--Definition of output operator

      ostream & operator <<(ostream &out, WordInfo &word)
      {
            word.printWords(out);  

      }

      int main() {

          string wordFile;
          cout<<"enter name of dictionary file: ";
          getline (cin,wordFile);

          ifstream inStream (wordFile.data());

          if(!inStream.is_open())
          {
          cerr<<"cannot open "<<wordFile<<endl; 
          exit(1);                      

          }

          vector <WordInfo> wordVector; 

          WordInfo aword;



          while (inStream >>aword && (!(aword=="synonyms")))
          {
              wordVector.push_back(aword);      
          }

          int i=0;          
          while (i<wordVector.size()){
                cout<<wordVector[i]<<endl;
                i++;
                }




          vector <int> intVector;
          string aLine; //suspect


          // bad statement?
          while (getline(inStream, aLine)&&(aLine!=("antonyms"))){

                aword.pushSynonyms(aLine, wordVector);

                }




          system("PAUSE");

          return 0;
      }
4

3 回答 3

2

问题似乎在这里:

in>>myId>>word;

On the "synonyms" line the extraction of myId fails and sets failbit on the stream, which causes the following extractions to also fail. You have to reset the error control state before extracting further elements (like the word "synonyms") from the stream:

in.clear();
于 2009-01-19T02:46:06.587 回答
1

首先,打开编译器警告。它可能会帮助您找到一些您认为可以但实际上不是的事情。例如,具有非void返回类型的函数应该总是返回一些东西。如果他们不这样做,那么你的程序的行为是未定义的,未定义的行为包括“完全按照你的意愿工作,除了程序后面的一些细微差别”。如果您使用的是 g++,则警告选项是-Wall.

其次,请注意,不只是突出显示的行没有运行。整个函数永远pushSynonyms不会被调用。您的课程是否涵盖了如何使用调试器?如果是这样,那么考虑使用它。如果没有,那么试着cout在你的程序中加入一些“”语句,这样你就可以准确地看到你的程序在出错之前能走多远。

第三,注意当流读取失败时,流的失败位被设置。在您清除它之前(如 sth's answer 所示),无法从该流中进行进一步提取,因此所有进一步的使用>>andgetline都将失败。

于 2009-01-19T02:42:23.910 回答
0

你做过诊断打印吗?例如,什么是synsAux.size()?您在synline开始处理之前检查过里面的内容吗?您是否检查过从输入流中收集了哪些数字?

于 2009-01-19T02:41:34.800 回答