I'm trying to code a Bingo Board in C++, but I'm doing something very wrong. I'm not sure what, but for whatever reason when I initialize the number of rows and columns, and do a nested for loop on the arrays I created to implement this structure, I get what appears to be more than a hundred rows and 30+ columns, when I should just be getting a five, by five board. I'm also trying to specify a max and min value for my rand function, but it appears that there isn't a way to do this. Thus, what would be the best approach to accomplish this without giving away the solution? The reason why I mention that last bit is so I can learn how to do this lol.
Here is my code:
#ifndef BOARD_H
#define BOARD_H
#include <cstdlib>
#include <time.h>
#include <stdio.h>
class Board
{
public:
Board(unsigned int number开发者_运维知识库OfRows, unsigned int numberOfColumns, unsigned int seed, unsigned int max, unsigned int min);
void generate();
void setSeedValue(int seed);
private:
unsigned int m_rows[];
unsigned int m_columns[];
unsigned int m_max, m_min;
};
#endif // BOARD_H
Board::Board(unsigned int numberOfRows, unsigned int numberOfColumns, unsigned int seed, unsigned int max, unsigned int min)
{
this->m_rows[numberOfRows];
this->m_columns[numberOfColumns];
srand(seed);
this->m_max = max;
this->m_min = min;
printf("%d\n", size_t(m_rows));
printf("%d\n", size_t(m_columns));
}
void Board::generate()
{
for (int i = 0; i < size_t(m_rows); i++)
{
for(int j = 0; j < size_t(m_columns); j++)
{
this->m_columns[j] = (rand() % 10) + j;
std::cout << this->m_columns[j];
}
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Board * board = new Board(5, 5, time(NULL), 100, 1);
board->generate();
delete board;
return a.exec();
}
In order to create the 2-dimensional board your wanting, where the size is input at runtime, you're going to have to actually dynamically allocate a board in memory ... you can't declare your board the way you've done in your class as zero-length arrays.
Code like this:
this->m_rows[numberOfRows];
does not initialize your array-size ... rather it actually attempts to access memory allocated at that offset from the start of m_rows
... that could cause a segmentation fault or some other undefined behavior due to accessing memory beyond the end of the class/structure type.
It would be much better, since you're using C++, to create your board class using the STL's std::vector
container. Your Board
class would then look like the following:
class Board
{
public:
Board(unsigned int numberOfRows, unsigned int numberOfColumns,
unsigned int seed, unsigned int max, unsigned int min);
void generate();
void setSeedValue(int seed);
private:
vector<vector<unsigned int> > board; //use the STL vector container
unsigned int m_max, m_min;
};
Then in your constructor, you would actually allocate the necessary memory (through the STL's vector container) that your board will take up:
Board::Board(unsigned int numberOfRows, unsigned int numberOfColumns,
unsigned int seed, unsigned int max, unsigned int min)
{
for (int i=0; i < numberOfRows; i++)
{
this->board.push_back(vector<unsigned int>(numberOfColumns, 0));
}
srand(seed);
this->m_max = max;
this->m_min = min;
printf("%d\n", size_t(m_rows));
printf("%d\n", size_t(m_columns));
}
Finally, your Board::generate
function would now look like the following:
void Board::generate()
{
for (int i = 0; i < this->board.size(); i++)
{
for(int j = 0; j < this->board[i].size(); j++)
{
this->board[i][j] = (rand() % 10) + j;
std::cout << this->board[i][j];
}
}
}
Let your board class store the row size and column size that as member variables. Use those member variables as your upper bounds in your for loops in the member function generate(). Also, use a 2D array rather an 1D array, as this better represents the structure of a bingo board. Currently, your initialization of temporary size_t's in your for loops is not correct. The array name is acts as a pointer to the first element of the array -- so that variable you create is not giving you the length of the array. You have to store the length of arrays separately from the array itself (or use boost::array).
精彩评论