0
votes

I am trying to write a c++ program to read a csv file. At this moment i have two functions std::string * readdatastr() and double * readdatalf() that successfully puts in a matrix the data in the file if the data is a string and if the data is a double. This function are ensembled in a class like the following:

class readcsv{
    public:
        int nrows;
        int ncols;
        std::string * datastr;
        double * datalf;

        void getdim(int * nrows, int * ncols){
            //function to get column and row number of the file
        }
        std::string * readdatastr(FILE * filept, int nrows, int ncols){
            //function to read data if data is std::string
        }
        double * readdatalf(FILE * filept, int nrows, int ncols){
            //function to read data if data is double
        }
        readcsv(FILE * filept, bool isstr, int nrows, int ncols){
            if(isstr){
                datastr=new std::string[nrows*ncols];
                datastr=readdatastr();
            }else{
                datalf=new double[nrows*ncols];
                datalf=readdatalf();
            }
        }
};

This works, but it bothers me (and afterwards it becomes a problem) that the name of the variable containing the data changes if the data is a string or a double. Is there a way to have the two variables datastr,datalf having the same name? And if not, is there a way to have a single data matrix in the end with the right type (isstr ? std::string : double)?

Where is the variable datalf being used right now? Is this exactly your code? - FCo
@Fco, sorry there was an error at line 22-23. In any case this is not exactly my code but an example to show what i am doing. - Giacomo Marcon
Lots of errors in this code, for example datastr=new std::string[nrows*ncols]; datastr=readdatastr(); If you assign to a variable twice then the value from the first assignment is lost. So datastr=new ... does nothing (except leak memory). - john