Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return multidim string array

Status
Not open for further replies.

OOzy

Programmer
Jul 24, 2000
135
SA
Dears

I have this class which reads a file and should return multidim array.
How can I return the array hb_field and be able to use in main()

class
Code:
class HBSeqFile
{

 public:
    HBSeqFile();
    string HBReadFile();

 private:
    int counter1;
    int counter2;
    int strln;
    int lpos;
    int rpos;
    fstream filename;
    string TempStr;
    string hb_line[4];
    string hb_field[4][6];

};

HBSeqFile::HBSeqFile()
{
  counter1=0;
  counter2=0;
  strln=0;
  lpos=0;
  rpos=0;

};

string HBSeqFile::HBReadFile()
{

    //Read line from file and fill in array
      filename.open ("c:\\data.txt",ios::in);
      if (!filename.is_open())
      {
       cout<<"File was not opened successfully.\n\n";

      }

      while(getline(filename, TempStr))
      {
        hb_line[counter1]=TempStr;
        counter1++;
        //cout<<hb_line[counter1-1]<<"-"<<endl;
      }
      filename.close();

     for (int f=0; f<counter1; ++f)
     {
         strln=hb_line[f].length();
         lpos=0;
         rpos=0;
         counter2=0;

         for (int i=0; i<strln; ++i)
         {
             string sc(hb_line[f],i,1);

             if(sc==";")
             {
                rpos=i;
                string sc(hb_line[f],lpos,rpos-lpos);
                lpos=rpos+1;
                hb_field[f][counter2]=sc;
                counter2++;
             }//endif
          }//end for
       }//end for

     return hb_field;
 }//End of Function
main()
Code:
#include <iostream>
#include <stdarg.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <fstream>
#include "seqfile.h"

void main()
{
    //*Declarations
    string ts;
    int i;

    HBSeqFile HSF;
    ts=HSF.HBReadFile();
    //cout<<ts;


           for (int i=0; i<4; ++i)
           {
            for (int t=0; t<6; ++t)
            {
               cout<<ts[i][t]<<"\n"<<endl;
            }
           }

     system("PAUSE");

}//end of main
 
Does this code work for you? The return type should be

Code:
string** HBSeqFile::HBReadFile()

------------------
When you do it, do it right.
 
> void main()
main returns an int.

> cout<<ts[t]<<"\n"<<endl;
Rather than returning say a pointer to your private parts (it's very poor C++ to expose the internal data to the outside world), write another member function which returns a specific record.

Say
Code:
string HBSeqFile::HBGetField( int line, int field )
{
  // here you can actually range check the inputs
  return hb_field[line][field];
}

And main becomes
Code:
           for (int i=0; i<4; ++i)
           {
            for (int t=0; t<6; ++t)
            {
               cout<<HSF.HBGetField(i,t)<<"\n"<<endl;
            }
           }

I also can't see why things like counter need to be private members of the class when they can easily be local variables of a function.
If you tokenise each line as you read it, the only private member of the class you need is hb_field

--
 
You could also use: vector< vector< string > >
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top