Hello,

In my class I have a method that basically does this:

- Reads in characters from text file (as a string)
- Converts the characters into an integer
- Stores the values into the integer

Here is the code:

bool Loader::Read(string theFile)
{
    strRepositry = theFile;
    string theData = "";
    int i = 0; 
    int* data2 = new int();
    
    if(!file_exists())
    {
        return false;
    }
    
    ifstream file(strRepositry.c_str());
    
    
    while(!file.eof())
    {
        file >> theData;
        
        istringstream convert(theData);
        cout << i << endl;
        convert >> data2[i++];
    }
    file.close();
    cout << data2[0];
    data = data2;
    

    return true;
}

Now this crashes, it's because I'm using i++ but why? And is there a way around it?

Really, really confused.

Thanks :)

You create enough room for a single int with int * data2 = new int(); You are accessing that memory as if it were an array. You probably mean int * data2 = new int[MAX_SIZE]; where MAX_SIZE is the size of the array you want to support.

Either that, or you can use a std::vector to manage the data for you.

Comments
Great Help. Thanks :)

Thanks for the reply.

It kind of needs to be an array, because the values in the .txt file are:

0 1 0 1 0 1
1 1 1 1 1 1

I read it in as a string, and then convert it to an int array..

SORRY! Having a bad day (I feel stupid now hehe)

Thank you so much for your help :)

This question has already been answered. Start a new discussion instead.