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();
}
Attachments
4 5
0 0 1 0 0
0 1 0 0 1
0 0 0 0 1
1 1 0 0 0
#include <iostream>
#include <fstream>
#include "stack.h"
using namespace std;
typedef NodeType* NodePtr;
/*
struct NodeType
{
    ItemType item;
    NodePtr  next;
};

template<class T>
Stack<T>::Stack()
{
  stackPtr = NULL;
  length = 0;
}

template<class T>
void Stack<T>::push(ItemType item)
{
  NodePtr ptr;
  ptr = new NodeType;
  ptr->item = item;
  ptr->next = stackPtr;
  stackPtr = ptr;
  length++;
}*/
//********************//
//*End Implementation*//
//********************//


void getData(int**,int&,int&);
void print(int**,int,int);

int main()
{
  int **array;
  int row, column;
  getData(array,row,column);
  
  cout << array[0][0];
  
  //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();
}
typedef int ItemType;
struct NodeType;
template<class T>
class Stack
{
  public:
    
    Stack();
      //Pre:
      //Post:
             
    void push(ItemType item);
      //Pre:
      //Post:
                 
    void pop();
      //Pre:
      //Post:
                  
    int top();
      //Pre:
      //Post:
                  
    bool isEmpty();
      //Pre:
      //Post:
                  
    bool isFull();
      //Pre:
      //Post:
                  
  private:      
    int length;
    NodeType *stackPtr;
};

When you are passing row and col to get Data, you are derefenrencing it. But they are of type int. And what does " >> " operator do? I've never seen it used out side of cin >> .


[edit] er nvm i didn't read your code right.

Your getData( ) function is being passed the int ** BY VALUE. It can allocate memory to that pointer variable, but that allocation only exists within the scope of the function. The int ** in main( ) is still not allocated any memory.

So, to make life interesting, you must pass the int** by reference, like:

void getData( int** & ,int&,int&);

Also, in getData( ), you are using the row and column variables backwards. Do the allocations and reading loops with row as the outer loop, column as the inner loop, as you do in your display function.

This article has been dead for over six months. Start a new discussion instead.