This is the header that our instructor gave us for Terrain
Code:
#define N 25
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <fstream>
class Terrain
{
private:
int rowsize;
int columnsize;
int gridmap[N][N];
public:
////Terrain();
//Terrain(int m, int n);
void LoadMap(ifstream &in);
void PrintMap();
//int GetElevation(int i, int j);
//int GetRowSize();
//int GetColumnSize();
};
at the very beginning he has given us a max array size of 25 named N
Note I've also commented out all the other functions that I haven't finished yet
Now onto the actual definition I have
Code:
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include "Terrain.h"
using namespace std;
void Terrain::LoadMap(ifstream &in)
{
const int ROWS = 6;
const int COLUMNS = 7;
cout << "Loading terrain information...\n\n";
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLUMNS; j++)
{
in >> gridmap[i][j];
}
}
}
void Terrain::PrintMap()
{
const int ROWS = 6;
const int COLUMNS = 7;
cout << "Printing terrain information...\n\n";
for(int r = 0; r < ROWS; r++)
{
for(int c = 0; c < COLUMNS; c++)
{
cout << gridmap[r][c] << "\t";
}
cout << endl;
}
}
Now I've slightly altered the code since Ive held constants for the number of rows and columns, which are respectively 6 and 7. But im still wondering..would it be possible to keep the constant value of N(25) in my nested for loops when I read into my array instead of keeping the constants ROWS and COLUMNS? So is there a way around this?
Here's the driver
Code:
ifstream input;
input.open("C:\\terrain_info.dat", ios::in);
discard_line(input);
if(input.fail())
{
cout << "cant be opened";
}
else
{
Terrain map; //created instance of terrain called map
map.LoadMap(input); //used that instance to load the map coordinate
map.PrintMap(); //used that instance to print contents of the map
}