Hi Everybody,

I have succeeded in parsing the values I needed from a text file and storing them in an array. The script I've posted works as a function, but now I tried to put it in a class and I can't figure out how to define the constructor. The problem is that in the main, I get the name of the file via CIN:

cin.getline(input,20);

Could somebody explain me how to define the constructor for this example in a correct way?

Thanks in advance,

Kev

-----------------------------------------------------------------------------------

#include <fstream>
#include <string.h>

class X {
    private:
        fstream text;
        string line;
    public:
	   X( const std::string& InputFile){
           text.open(InputFile.c_str());
        }

double** Extract_Data(){

const int x = 2;
const int f = 3;
const int k = 0;




// if (condition) statement
// the statement will only be executed if the condition is true 
  
if (text.fail())
  {
    cout << "File not found: " << InputFile <<endl;  
    return 0;
  }
 



// to check wether the file is correctly opened we include bool is_open(text)
  // this will return a bool of type true if the file is correctly opened, else false
  
   if(! text.is_open())
    { cout << "Error opening file"; exit (1);}
    // Exit one tells Operating System to exit the programm



  if (text.is_open())
  {
    while(! text.eof() ) //(getline(text,line) )
    {
        if (x == 2)
        { 
        while ((x==2) && (! text.eof()) )
        {
            getline(text,line);
            std::string::size_type pos_2 = line.find("R_CMD");
            if((pos_2 == 0) && (pos_2 != std::string::npos) )
            {
                   std::string word = line.substr(pos_2);
                   std::string::size_type pos_true = line.find("True");
                   if(pos_true < 20)
                   {
                        x = 8;
                        break;                         
                   }

             }
         }      
         }
         else if (x == 8)
         {
         while ( (x==8) && (! text.eof())) 
         {
                  
              
                  getline (text,line,'>');                  
                  std::string::size_type pos_81  = line.find("TIME_");
                  std::string::size_type pos_82  = line.find("DELT_");
                  std::string::size_type pos_83  = line.find("SELECTED_");
                  std::string::size_type pos_81b = line.find("ATG");
                  std::string::size_type pos_84  = line.find("LT_");
                  std::string::size_type pos_85  = line.find("LN_");
                  std::string::size_type pos_86  = line.find("G_X_");
                  std::string::size_type pos_87  = line.find("G_Y_");
                  std::string::size_type pos_88  = line.find("G_Z_");
                  std::string::size_type pos_89  = line.find("C_X_");
                  std::string::size_type pos_810 = line.find("C_Y_");
                  std::string::size_type pos_811 = line.find("C_Z_");
                  std::string::size_type pos_812 = line.find("P_");
                  std::string::size_type pos_813 = line.find("R_");
                  std::string::size_type pos_814 = line.find("P_Z_");
                  std::string::size_type pos_815 = line.find("TU_HI_");
                  std::string::size_type pos_816 = line.find("TU_R_");
                  std::string::size_type pos_819 = line.find("SY_T_");
                  std::string::size_type pos_8   = line.find("ST_N_");
                  
                  int positie [19] = {pos_81,pos_82,pos_83,pos_81b,pos_84,pos_85,pos_86,pos_87,pos_88,pos_89,pos_810,pos_811,pos_812,pos_813,pos_814,pos_815,pos_816,pos_819,pos_8};
                                    
                 
              
                   
                  if(positie[0]!= std::string::npos){
                  
                     std::string::size_type pos_  = line.find("<");
                     std::string begin = line.substr(pos_ +1);
                   
                     cout<<begin<<"\n";
                     }
                  
                  else if(positie[3] != std::string::npos){
                  f=1;
                  }
                                  
                  if(f == 1){
                     
                     for(int v=1;v<3;v++){
                     if(positie[v]!= std::string::npos){
                     std::string::size_type pos_  = line.find("<");
                     std::string begin = line.substr(pos_ +1);
                     cout<<begin<<"\n";
                     }
                     }
                   
                     if(positie[4] != std::string::npos){
                     
                     std::string::size_type pos_  = line.find("<");
                     std::string begin = line.substr(pos_ +1);
                     
                     string s1 = begin.substr(2,2);
                     stringstream ss1(s1);
                     float degree = 0;
                     ss1>>degree;
                     
                     string s2 = begin.substr(5,7);
                     stringstream ss2(s2);
                     float minute = 0;
                     ss2>>minute;
                     float latitude = 0;
                     latitude = degree + minute/60;
                      m[k][0] = latitude;
                      //myfile<<latitude<<endl;
                     }
                     
                     if(positie[5] != std::string::npos){
                     
                     std::string::size_type pos_  = line.find("<");
                     std::string begin = line.substr(pos_ +1);
                     
                                     
                       //if(strcmp(c,E)==0){
                        
                        string s1 = begin.substr(2,3);
                        stringstream ss1(s1);
                        float degree = 0;
                        ss1>>degree;
                     
                        string s2 = begin.substr(6,7);
                        stringstream ss2(s2);
                        float minute = 0;
                        ss2>>minute;
                        float longitude = 0;
                        longitude = degree + minute/60;
                        m[k][1] = longitude;
                     }
                     
                     
         
                   
                   
                     for(int i=6; i<18; i++)
                     {
                     if((positie[i]!= std::string::npos) && (positie[i] != 5)) // Otherwise TGT_VEL_X would also be displayed
                     {
                     std::string::size_type pos_  = line.find("<");
                     std::string begin = line.substr(pos_ +1);
                     
    // This way of working I personally find quit sucky, but atof won't work with strings.                 
                     
                     stringstream ss(begin);
                     float beg = 0;
                     ss>>beg;
                     m[k][i-4] = beg;
                     }
                     }
                              
                     if(positie[18] != std::string::npos)
                     {
                         x = 2;
                         f = 3;
                         k++;
                         break;
                     }
                 }
                  
         }
         }
        else { cout << "einde";}
             
     }
     
  //closing the file via the member function close() --> text.close()
    text.close();
  }
  return m;
  }
 
   

virtual ~X(){
		std::cout<<"Destructor";
	}
};

I believe that you did define a constructor. Please be more specific about your problem.

I get a lot of errors when compiling the code (errors in X.h file):

'fstream' is used as a type, but is not defined as a type.
'string' is used as a type, but is not defined as a type.
In constructor 'X::X(const std::string&)':
'text' undeclared
...

Change your pre-processors to this :

#include <fstream>
#include <string>
using namespace std;

The main thing you forgot was the using namespace std, in your code.

This article has been dead for over six months. Start a new discussion instead.