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;

「シノニム」行では、ストリームの抽出myIdとセットが失敗しfailbit、これにより後続の抽出も失敗します。ストリームからさらに要素 (単語「同義語」など) を抽出する前に、エラー制御状態をリセットする必要があります。

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

まず、コンパイラの警告をオンにします。大丈夫だと思っていても実際にはそうではないものを見つけるのに役立つかもしれません。たとえば、void戻り値のない型を持つ関数は、常に何かを返す必要があります。そうでない場合、プログラムの動作は未定義であり、未定義の動作には、「プログラムの後半のわずかな違いを除いて、希望どおりに正確に動作する」ことが含まれます。g++ を使用している場合、警告のオプションは-Wall.

次に、実行されていないのは強調表示された行だけではないことに注意してください。関数全体pushSynonymsが呼び出されることはありません。クラスでデバッガーの使用方法をもうカバーしましたか? もしそうなら、それを使用することを検討してください。そうでない場合は、プログラムに " " ステートメントをいくつか入れてみてcoutください。そうすれば、プログラムが問題を起こす前に、どこまで到達するかを正確に確認できます。

第 3 に、ストリームの読み取りエラーが発生すると、ストリームの失敗ビットが設定されることに注意してください。それをクリアするまで(sth's answer で示されているように)、そのストリームからそれ以上抽出することはできないため、以降の>>andの使用はすべてgetline失敗します。

于 2009-01-19T02:42:23.910 に答える
0

診断印刷はしましたか?たとえば、何synsAux.size()ですか?synline処理を開始する前に、何が入っているか確認しましたか? 入力ストリームから収集される数値を確認しましたか?

于 2009-01-19T02:41:34.800 に答える