943,713 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 612
  • C++ RSS
Nov 2nd, 2008
0

Dynamic 2d array not passing?

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. void getData(int**,int&,int&);
  2. void print(int**,int,int);
  3.  
  4. int main()
  5. {
  6. int **array;
  7. int row, column;
  8. getData(array,row,column);
  9.  
  10. print(array,row,column);
  11.  
  12. system("PAUSE");
  13. return 0;
  14. }
  15.  
  16. void print(int **array, int row, int column)
  17. {
  18. for(int y=0; y < row; y++)
  19. {
  20. cout << endl;
  21. for(int x=0; x < column; x++)
  22. {
  23. cout << array[y][x] << " ";
  24. }
  25. }
  26. cout << endl;
  27. }
  28.  
  29. void getData(int **array, int &row,int &column)
  30. {
  31. string fileName;
  32. ifstream data;
  33. cout << "Enter File Name\n";
  34. getline(cin, fileName);
  35. data.open(fileName.c_str());
  36. if (!data.is_open())
  37. cout << "Error opening file\n";
  38. else
  39. {
  40. data >> row >> column;
  41. array = new int*[column]; //Allocates memory for the array
  42. for (int q = 0; q < column; ++q)
  43. array[q] = new int[row];
  44. for(int y=0; y < column; y++)
  45. {
  46. for(int x=0; x < row; x++)
  47. {
  48. data >> array[y][x];
  49. }
  50. }
  51. }
  52. data.close();
  53. }
Attached Files
File Type: cpp Maze.cpp (1.6 KB, 4 views)
File Type: h stack.h (564 Bytes, 3 views)
File Type: txt data.txt (49 Bytes, 5 views)
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
DevC++4.9.9.2 is offline Offline
39 posts
since Apr 2008
Nov 2nd, 2008
0

Re: Dynamic 2d array not passing?

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.
Last edited by freudian_slip; Nov 2nd, 2008 at 8:34 pm.
Reputation Points: 11
Solved Threads: 4
Light Poster
freudian_slip is offline Offline
36 posts
since Oct 2008
Nov 2nd, 2008
0

Re: Dynamic 2d array not passing?

how am i derefrencing it?
Reputation Points: 10
Solved Threads: 1
Light Poster
DevC++4.9.9.2 is offline Offline
39 posts
since Apr 2008
Nov 2nd, 2008
0

Re: Dynamic 2d array not passing?

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:
C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: simple help needed
Next Thread in C++ Forum Timeline: ISBN Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC