Can anyone tell me what might cause the presentation error in this code?
Code: Select all
#include <iostream>
#include <cstring>
#include <cassert>
using namespace std;
class CMines
{
public:
CMines();
CMines(char**, int, int);
~CMines();
void setRows(int);
void setCols(int);
void setBoard(char**);
int getRows() const;
int getCols() const;
int** getBoard() const;
private:
int ** board;
int rows, cols;
}; // end class CMines header
ostream &operator<< (ostream &, const CMines &);
char * StrDup(const char*);
int main()
{
int l_rows, l_cols, count = 0;
char temp[255], ** input;
CMines* game;
cin >> l_rows;
cin.get();
cin >> l_cols;
cin.get();
while(l_rows != 0 || l_cols != 0)
{
input = new char*[l_rows];
for(int x = 0; x < l_rows; x++)
input[x] = new char[l_cols];
for(int x = 0; x < l_rows; x++)
{
cin.getline(temp, 255);
input[x] = StrDup(temp);
} // end for
game = new CMines(input, l_rows, l_cols);
cout << "Field #" << ++count << ":\n" << *game << endl;
for(int x = 0; x < l_rows; x++)
delete [] input[x];
delete [] input;
input = NULL;
cin >> l_rows;
cin.get();
cin >> l_cols;
cin.get();
} // end while
return 0;
} // end main()
CMines::CMines()
{
board = NULL;
} // end default CMines constructor
CMines::CMines(char** in, int r, int c)
{
setRows(r);
setCols(c);
setBoard(in);
} // end explicit CMines constructor
CMines::~CMines()
{
for(int x = 0; x < getRows(); x++)
delete [] board[x];
delete [] board;
board = NULL;
} // end CMines DESTRUCTOR
void CMines::setRows(int r)
{
rows = r;
} // end setRows()
void CMines::setCols(int c)
{
cols = c;
} // end setCols()
void CMines::setBoard(char ** in)
{
int up, down, left, right;
up = left = -1;
down = right = 1;
board = new int*[getRows()];
for(int x = 0; x < getRows(); x++)
board[x] = new int[getCols()];
for(int x = 0; x < getRows(); x++)
for(int y = 0; y < getCols(); y++)
(in[x][y] == '*') ? board[x][y] = -1 : board[x][y] = 0;
for(int x = 0; x < getRows(); x++)
{
for(int y = 0; y < getCols(); y++)
{
if(board[x][y] == -1)
{
if(x + up > -1 && y + left > -1)
if(board[x + up][y + left] != -1)
board[x + up][y + left]++;
if(y + left > -1)
if(board[x][y + left] != -1)
board[x][y + left]++;
if(x + up > -1 && y + right < getCols())
if(board[x + up][y + right] != -1)
board[x + up][y + right]++;
if(x + up > -1)
if(board[x + up][y] != -1)
board[x + up][y]++;
if(x + down < getRows() && y + right < getCols())
if(board[x + down][y + right] != -1)
board[x + down][y + right]++;
if(y + right < getCols())
if(board[x][y + right] != -1)
board[x][y + right]++;
if(x + down < getRows() && y + left > -1)
if(board[x + down][y + left] != -1)
board[x + down][y + left]++;
if(x + down < getRows())
if(board[x + down][y] != -1)
board[x + down][y]++;
} // end if
} // end for
} // end for
} // end setBoard()
int CMines::getRows() const { return rows; } // end getRows()
int CMines::getCols() const { return cols; } // end getCols()
int** CMines::getBoard() const { return board; } // end getBoard()
ostream &operator<< (ostream & out, const CMines & A)
{
for(int x = 0; x < A.getRows(); x++)
{
for(int y = 0; y < A.getCols(); y++)
(A.getBoard()[x][y] == -1) ? out << "*" : out << A.getBoard()[x][y];
out << endl;
} // end for
return out;
} // end ostream operator<< overload
char * StrDup(const char *in)
{
char * copy = new char [1 + strlen(in)];
assert (copy != NULL);
strcpy (copy, in);
return copy;
} // end StrDup()