我想打标有//该线路应该是做印刷的东西线,这是打印“同义词”和“反义词”。

之间的INT值

这是文本文件:

dictionary.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;
      }
有帮助吗?

解决方案

这个问题似乎是这里:

in>>myId>>word;

在的“同义词”线myId的提取失败,并将该流,这会导致以下的萃取也失败failbit。你必须从流中提取另外的元件(如单词“同义词”)前向误差控制状态复位:

in.clear();

其他提示

首先,打开编译器警告。它可以帮助你发现一些东西,你认为是确定的,但它真的不是。例如,对于非void返回类型应该总是返回的东西。如果他们不这样做,那么你的程序的行为是不确定的,和未定义的行为包括“正是工作作为你想要的,只是后来在节目中一些细微的差别。”如果您使用的克++,对警告的选项是-Wall

二,请注意,它不只是没有运行突出显示的行。在整个pushSynonyms功能的不会被调用。贵班介绍了如何使用调试器了吗?如果是的话,那么可以考虑使用它。如果没有,那么请尝试将一些“cout”语句在你的程序,所以你可以看到你的程序到底有多远它出错了。

第三,注意,当发生一个流中读取失败,该流的失败位被置位。直到你清楚它(的如通过某事物的回答示出),可以从该流不发生进一步的提取,所以>>getline的所有进一步的用途将失败。

让你做任何诊断打印?例如,什么是synsAux.size()?你检查什么是synline开始处理之前?你检查其数字从输入流收集?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top