0
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using std::vector;
using namespace std;

class dataLine
{

double time;
    int id;
    int inten;
    int cov;
    int type;

    void copyDataLine(dataLine *dat2)
    {
        time = dat2->time;
        /*inten = dat2->inten;
        cov = dat2->cov;
        type = dat2->type;*/
        return;
    }
};

class findLines
{
private:
    int lines;
    ifstream datFile;
    string line;
public:
    int countLines()
    {
        lines = 0;
        datFile.open("precipitation.txt");
        if (datFile.good())

        {
            while (!datFile.eof())
            {
                getline(datFile, line);
                lines++;
            }

        }
        return lines;
    }
};

void readData(dataLine *datLine_one[], dataLine *datLine_two[], dataLine *datLine_three[], dataLine *datLine_four[])
{
    fstream datFile;
    dataLine *datLine = new dataLine;;
    *datLine = dataLine();
    datFile.open("precipitation.txt");
    int count[]{ 0 ,0, 0, 0, 0 };

    while (!datFile.eof())
    {
        datFile >> datLine->time;
        datFile >> datLine->id;
        datFile >> datLine->inten;
        datFile >> datLine->cov;
        datFile >> datLine->type;
        if (datLine->id == 1)
        {
            datLine_one[count[1]]->copyDataLine(datLine);
            count[1]++;
        }
        /*else if (datLine->id == 2)
        {
            datLine_two[count[2]]->copyDataLine(datLine);
            count[2]++;
        }*/
        /*if (datLine->id == 3)
        {
            copyDataLine(datLine_three[count[3]], datLine);
            count[3]++;
        }
        if (datLine->id == 4)
        {
            copyDataLine(datLine_four[count[4]], datLine);
            count[4]++;
        }*/


    }


    return;
}


int main()
{
    findLines precip;
    int lineNum = precip.countLines();
    int datNum = lineNum / 4;

    dataLine *datLine_1 = new dataLine[datNum];
    dataLine *datLine_2 = new dataLine[datNum];
    dataLine *datLine_3 = new dataLine[datNum];
    dataLine *datLine_4 = new dataLine[datNum];

    readData(&datLine_1, &datLine_2, &datLine_3, &datLine_4);

    system("pause");
    return 0;
}

This code is set to read in data from a file, and store it in different arrays based on the IDs of the data (five columns and 33317 rows). The variables are shown to be assigned successfully if I step through the code in Visual Studio, but when I try to debug it, an exception occurs when the copyDataLine function is called.

Aadi
  • 33
  • 5
  • At the very least you should be pointing out on which line the crash/error occurs. Though this code looks pretty suspicious and most likely has some *undefined behavior* somewhere in there (and also a lot of memory leaks) – UnholySheep Apr 08 '17 at 12:35
  • `main()` dynamically allocates arrays with `4` elements. The file has `13317` rows, so the reading code will try to treat that array of `4` elements as if it has `13317` elements. – Peter Apr 08 '17 at 12:36
  • 1
    [`while (!eof())` is wrong.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) Also, you're not freeing anything you new up. And `dataLine *datLine = new dataLine;; *datLine = dataLine();` is pointless because it was already default-constructed. – chris Apr 08 '17 at 12:38
  • Opening the file in `readData` most likely fails. It has already been opened in `findLines::countLines`, and never closed since then. – Igor Tandetnik Apr 08 '17 at 12:40

1 Answers1

1

The problem is that in the readData function, the variables datLine_one etc. are not arrays of pointers. It is a pointer to an array (or really a pointer to a pointer variable).

You can solve your problem very easily by not passing the addresses to the pointer variables in the main function, and not declaring the arguments as pointers to pointers.

So something like this instead:

void readData(dataLine *datLine_one, dataLine *datLine_two, dataLine *datLine_three, dataLine *datLine_four)
{
    ...
    datLine_one[count[1]].copyDataLine(datLine);
    ...
}

int main()
{
    ...
    readData(datLine_1, datLine_2, datLine_3, datLine_4);
    ...
}
Some programmer dude
  • 363,249
  • 31
  • 351
  • 550