read a multidimensional dynamical array from txt file

Hi,
this is my first post here and my second day programming in C++.

My program should read a file with unknown number of rows and given number of columns in an 2D-array. This is my code so far.

Thanks for help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>

using namespace std;
using std::ifstream;
 
int main ()
{
    double array[][3];
    int loop=0;
    double xp,F2,errF2;
    
    ifstream file("model/Q65b206.txt");
        while (! file.eof() )
        {
            file.get(xp,F2,errF2);
            array[loop][1] = xp;
            array[loop][2] = F2;
            array[loop][3] = errF2;
            loop++;
        }
    cout << array[][1] << endl;
    file.close();
    
    delete [] array;
    
    return 0;
}
You seem to have a misunderstanding of how multidimensional arrays work. You can't simply leave the first size undefined in the local variable, you must give it a size. You are also deleteing the array even though you never used new with it, so you will have an issue there as well. I would suggest looking at this article:

http://www.cplusplus.com/forum/articles/7459/
Topic archived. No new replies allowed.