C++ C++;如果流失败,为什么这条线不去它的地方';应该是吗?

C++ C++;如果流失败,为什么这条线不去它的地方';应该是吗?,c++,ifstream,C++,Ifstream,我想让标有//的行打印它自己的东西,即打印“同义词”和“反义词”之间的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> #inc

我想让标有//的行打印它自己的东西,即打印“同义词”和“反义词”之间的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;
      }
1可爱
2你好
三丑
4轻松
5困难
6累了
7漂亮
同义词
1 7
7 1
反义词
1 3
3 1 7
4 5
5 4
7 3
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类WordInfo{
公众:
WordInfo(){}
~WordInfo(){
}
int id()常量{return myId;}
无效读字(istream&in)
{
在>>myId>>word中;
}
void同义词(字符串synline、向量词infovector)
{
弦流-弦流(弦线);
向量综合征;
int-num;
while(synstream>>num)synsAux.push_back(num);

对于(int i=0;i您是否进行过任何诊断打印?例如,什么是
synsAux.size()
?您是否在开始处理它之前检查了
synline
中的内容?您是否检查了从输入流中收集的数字?

您进行过任何诊断打印吗?例如,什么是
synsAux.size()
?在开始处理前,您是否检查了
synline
中的内容?是否检查了从输入流中收集的数字?

首先,打开编译器警告。它可能会帮助您找到一些您认为正常但实际上不正常的内容。例如,具有非
void
返回类型的函数应该始终返回某些内容。如果它们不返回,则程序的行为未定义,未定义的行为包括“完全按照您想要的方式工作,除了程序中稍后的细微差异”。如果使用g++,则警告选项为
-Wall

第二,请注意,不仅仅是高亮的行没有运行。整个<代码>推同义词< /代码>函数永远不会被调用。您的类是否已经覆盖了如何使用调试器?如果是的话,请考虑使用它。如果没有,那么尝试设置一些“<代码> CUT < /代码>”语句,这样您就可以在程序出错之前准确地看到程序运行了多远


第三,请注意,当流读取失败时,将设置流的失败位,无法从该流中进行进一步提取,因此所有进一步使用
>
getline
的操作都将失败。

首先,打开编译器警告。它可能会帮助您找到一些您认为正常但实际上不正常的内容。例如,具有非
void
返回类型的函数应始终返回某些内容。如果如果不这样做,那么您的程序行为是未定义的,未定义的行为包括“完全按照您想要的方式工作,除了程序后面的一些细微差别。”如果您使用的是g++,则警告选项是
-Wall

第二,请注意,不仅仅是高亮的行没有运行。整个<代码>推同义词< /代码>函数永远不会被调用。您的类是否已经覆盖了如何使用调试器?如果是的话,请考虑使用它。如果没有,那么尝试设置一些“<代码> CUT < /代码>”语句,这样您就可以在程序出错之前准确地看到程序运行了多远


第三,请注意,当流读取失败时,将设置流的失败位。除非清除它(),否则无法从该流中进一步提取,因此所有进一步使用
>
getline
的操作都将失败。

问题似乎在这里:

in>>myId>>word;
在“同义词”行上,提取
myId
失败,并在流上设置
failbit
,这会导致以下提取也失败。在从流中提取更多元素(如“同义词”)之前,必须重置错误控制状态:

in.clear();

问题似乎在这里:

in>>myId>>word;
在“同义词”行上,提取
myId
失败,并在流上设置
failbit
,这会导致以下提取也失败。在从流中提取更多元素(如“同义词”)之前,必须重置错误控制状态:

in.clear();

这是一个家庭作业,不是吗?你向你的老师寻求帮助了吗?毕竟,这是他或她得到的报酬。我在委内瑞拉加拉加斯写这篇文章。我的老师没有为此得到报酬,事实上,我的老师不知道如何使用ifstream。这家伙提倡scanf。这是一个家庭作业,不是吗?你向你的导师寻求帮助了吗?毕竟,这就是他或她得到的报酬。我在委内瑞拉加拉加斯写这篇文章。我的导师没有得到报酬,事实上,我的导师不知道如何使用ifstream。这家伙提倡scanf。谢谢,我在一段时间后发表了明确的声明(inStream>>aword&(!(aword==“同义词”){wordVector.push_back(aword);}并且它允许阅读,它达到了push同义词功能,尽管打印现在被废弃了。谢谢,我在while(inStream>>aword&(!(aword==“同义词”){wordVector.push_back(aword)}之后放了清楚的语句它允许阅读,它达到了pushSynonyms功能,尽管打印现在被废弃了。