Jump to content
  • Advertisement


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


simple map file reading problems

This topic is 5408 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

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;

cin >> c;

the stuff i''m using in the text file is as follows, and it''s named hexboard.txt
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
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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!