Hey, im trying to print what is stored in my 2d dynmic array from a file in my print function. However whenever i try to access information in the array, it crashes the program. maybe the array is not being passed back some how, im not sure, any information would be helpful.
In the function getData(), i can output the array.
Attached is all the program files with a test file "data.txt"
Thank you in advance
void getData(int**,int&,int&);
void print(int**,int,int);
int main()
{
int **array;
int row, column;
getData(array,row,column);
print(array,row,column);
system("PAUSE");
return 0;
}
void print(int **array, int row, int column)
{
for(int y=0; y < row; y++)
{
cout << endl;
for(int x=0; x < column; x++)
{
cout << array[y][x] << " ";
}
}
cout << endl;
}
void getData(int **array, int &row,int &column)
{
string fileName;
ifstream data;
cout << "Enter File Name\n";
getline(cin, fileName);
data.open(fileName.c_str());
if (!data.is_open())
cout << "Error opening file\n";
else
{
data >> row >> column;
array = new int*[column]; //Allocates memory for the array
for (int q = 0; q < column; ++q)
array[q] = new int[row];
for(int y=0; y < column; y++)
{
for(int x=0; x < row; x++)
{
data >> array[y][x];
}
}
}
data.close();
}