2009-01-19 53 views
0

我想讓行標記//此行應該打印做它的事情,這是打印「同義詞」和「反義詞」之間的int值。C++ ifstream失敗,爲什麼這行不是它應該在哪裏?

這是文本文件:

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; 
     } 
+0

這是作業作業,不是嗎?你有沒有問你的老師的幫助呢?畢竟,這就是他或她正在得到的報酬。 – 2009-01-19 02:43:23

+0

我從委內瑞拉加拉加斯寫這篇文章。我的導師沒有爲此付錢,實際上,我的導師不知道如何使用ifstream ..這個人主張scanf。 – andandandand 2009-01-19 02:59:27

回答

2

這個問題似乎是在這裏:

in>>myId>>word; 

在「同義詞」行myId提取失敗,並將failbit流上的,這會導致下面的提取也失敗。您必須在從流中提取更多元素(如單詞「synonym」)之前重置錯誤控制狀態:

in.clear(); 
0

你有沒有做過診斷打印?例如,什麼是synsAux.size()?在開始處理之前,您是否檢查過synline中的內容?你是否檢查過哪些數字是從輸入流中收集的?

1

首先打開編譯器警告。它可以幫助你找到一些你認爲確定的事情,但事實並非如此。例如,具有非void返回類型的函數應始終返回某些內容。如果他們不這樣做,那麼你的程序的行爲是不確定的,未定義的行爲包括「完全按照你想要的那樣工作,除了稍後在程序中的一些細微差別」。如果您使用的是g ++,則警告選項爲-Wall

其次,請注意,這不僅僅是突出顯示的行未運行。永遠不會調用整個pushSynonyms函數。你的課是否介紹瞭如何使用調試器?如果是這樣,那麼考慮使用它。如果沒有,那麼可以嘗試在程序中添加一些「cout」語句,這樣您就可以在程序出錯之前看到程序到底有多遠。

第三,請注意,當發生流讀取失敗時,會設置流的失敗位。在清除它之前(as shown by sth's answer),不會從該流中進一步提取,因此>>getline的所有進一步使用都將失敗。

相關問題