Having a problem setting user input to a dynamic array. It worked with a static array, but now I'm having some kind of pointer issue. The user sets the dimensions and enters in a number of characters, which should be set to the array 'gameboard'. it's just setting jibberish though. Any help or advice would be great. Thanks in advance.

char **gameboard = NULL;
gameboard = new char*[dimen];

for (int row = 0; row < dimen; row++)
{
       gameboard[row] = new char[dimen];

}
for (int n = 0; n < (dimen * dimen); n++)
{
	*((char*)gameboard +n) =  tolower( (cin >> ws).get() );
}

Recommended Answers

All 5 Replies

Can you post your full code, please? We might understand your problem that way.

Like tkud said, try sending more of your code so we can understand your problem better. also, the variable "dimen" specifies the number of rows right?

sorry, here is the rest of the code. It's actually a boggle game based on this thread: http://www.daniweb.com/forums/showthread.php?t=138662&highlight=use+dictionary+C%2B%2B&page=2
but I need to be able to change the board size based on user input.
Also, yes 'dimen' is the row, but it is also the column, since it is always a square board.

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
using namespace std;

char **gameboard = NULL;

bool validate_word( const string& word, int row, int col, int index, int touched )

{

if ((unsigned)index == word.length()) return true;	//check last letter of word

for (int r = -1; r < 2; r++)	//check adjacent rows

for (int c = -1; c < 2; c++)	//check adjacent columns

{

if (((row +r) < 0) || ((row +r) > 3) || ((col +c) < 0) || ((col +c) > 3)) continue; //check if the adjacent rows and columns exist (check for edges)

int mask = 1 << (((row +r) *4) +col +c);

if (((touched & mask) == 0)

&& (gameboard[ row +r ][ col +c ] == word[ index ]))

if (validate_word( word, row +r, col +c, (word[ index ] == 'q') ? index +2 : index +1, touched | mask ))

return true;

}

return false;

}

void process_word( const string& word )

{

if ((3 > word.length()) || (word.length() > 17)) return;

for (int row = 0; row < 4; row++)

for (int col = 0; col < 4; col++)

if (gameboard[ row ][ col ] == word[ 0 ])	//check if the current element is equal to the first letter of the current word

if (validate_word( word, row, col, (word[ 0 ] == 'q') ? 2 : 1, (1 << ((row *4) +col)) ))	//check the rest of the word

cout << word << '\n';	//print the word if it works

}

int main()

{

	string dict_name;
	int dimen;

	cout << "Please enter the name of the dictionary file you would like to use: ";
	cin >> dict_name;
	cout << "Please enter the height of the board you want to use: ";
	cin >> dimen;
	cerr << "Enter the gameboard. Use just 'Q' for 'Qu'.\n";
	
	gameboard = new char*[dimen];
for (int row = 0; row < dimen; row++)
{
       gameboard[row] = new char[dimen]; 

}

	for (int n = 0; n < (dimen * dimen); n++)
	{

//this next line should be setting each element of the board to an input letter, but when debugging, it is setting them to jibberish characters
		*((char*)gameboard +n) =  tolower( (cin >> ws).get() );
	}
	ifstream dictionary(dict_name);

	if (!dictionary) return 1;

	for_each( istream_iterator <string> ( dictionary ), istream_iterator <string> (), process_word );

	dictionary.close();

	return 0;

}

instead of allocating arrays of char pointers try allocating a contiguous region of chars which is large enough to hold the whole board.

Eg. board width = 10
board height = 5;

char *board = new char[width * height];

now you can simply index into the char array using a combination of the row and column you wish to edit.

board[ column + row * width ];

Fixed it! Thanks so much for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.