Archived

This topic is now archived and is closed to further replies.

Nexpert

simple map file reading problems

Recommended Posts

Nexpert    122
Hi guys, first the problem. I''m working on a way to read a map file that simply stores the height of each space on the board. I read the first two numbers which are the width and height of the board in spaces, use these numbers to create a 2D array. Then I want to read the rest of the numbers into this array I just created. here is the code thus far.
#include <fstream.h>
#include <iostream>

using namespace std;

main()
{
int** map;
int row, column;
int hexheight;
char c;

fstream FILE;

FILE.open("hexboard.txt", ios::in);

FILE >> column >> row;

cout << column << endl << row << endl;

map = new int* [column];

  for(int i=0; i>column; i++)
  {
   map[i] = new int [row];
  }

cout << "array created..." << endl;

  for(int x = 0; x < row; x++)
  {
      for(int y = 0; y < column; y++)
      {
       FILE >> hexheight;
       cout << hexheight;
       map[x][y] = hexheight;
      }
   cout << endl;
  }
  
FILE.close();

cin >> c;

}
the stuff i''m using in the text file is as follows, and it''s named hexboard.txt
5
6
3 1 1 1 1
1 1 2 2 1
1 1 2 3 2
1 1 2 2 1
1 1 2 1 1
1 1 1 1 1
as it''s printing out the heights, it gets as far as the 3rd row, second column in before it just kinda freezes. Things I suspect are the way I created the 2D map array, and maybe the way fstream works. If you could get me pointed in the right direction I''d be most grateful! Thanks! Nel

Share this post


Link to post
Share on other sites
SiCrane    11839
There are a few problems with this.

1) Don''t mix old style iostream headers with new style iostream headers. i.e. change fstream.h to just fstream.

2) You access your map array by [row][column], but you create it [column][row] in the nested for loop.

3) When you dynamically create your map array you use "i > column" This should probably be a < (and probably change column to row (see above) ).

4) You never free the memory allocated for the map array.

Share this post


Link to post
Share on other sites