_adam_ 16 Newbie Poster

You should change the array definition to this:
int a[3];
...and the allocation to this:
a[0] = 1
a[1] = 2
a[2] = 3

If you require dynamically sized arrays you should read into dynamic memory allocation with C++.

Regards,
Adam

Edit: or use a std::vector

_adam_ 16 Newbie Poster

Hey,

What it looks like is probably happening is that you are overwriting previously set mines.

For example, You set a mine (to true) on the first iteration of your outer while loop at (2, 2).

However, at the end of this first iteration you have not placed enough mines. Due to this you have a second iteration.

One failed circumstance is that on the second pass the randomly generated condition returns false over a previously true mine location, thus removing it from the grid.

The second failed circumstance is that on the second iteration of the while loop the same cell (2,2) is set to true again and your code decreases the number of mines to place.

Ok, things to change:

1. The mine grid should initially all be set to false, i.e. no mines are present.
2. There is no need for the second if statement.
3. Before line 20 add the following line:

if( mine[r][c] )
    continue;

An interesting method of generating a random number I might add. However, the above three points should be enough to get you going.

All the best,
Adam

Edit: Also put the lines 32 and 33 inside your if true condition.

_adam_ 16 Newbie Poster

First of all, I'd advise writing the comparison to remove even numbers as something like this:

if(  (*it) % 2 == 0 )

(I know both work, but this tends to be clearer.)

Ok, you've correctly identified the problem, in your debugger have you checked the value of count?

You'll probably find that this value is putting your iterator out of the range of your vector, i.e. you have 50 elements in the array, and you've asked your iterator to access element 51 of your vector (which doesn't exist).

That should be enough to get you moving forward,

All the best,
Adam

_adam_ 16 Newbie Poster

Hey Adam, I tried this code(with the alteration of it++) in place of the for loop. But i keep getting the same error. Do i have to replace any other part of the code?

Could you post what you've got so far with changes made so I can see where you're at?

_adam_ 16 Newbie Poster

Code Error
The code should have read:

it = (buffer.begin()+count);
while( it != buffer.end() )
{
	if ((*it)%2)
	{
		it = buffer.erase(it); // this increments for you!
	}
	else
	{
		// edit: the following line previously read 'iter++' ( my bad :o) )
		it++; // if we don't erase just keep going through the list!
	}
}
_adam_ 16 Newbie Poster

The problem occurrs first on about line 31. (Then a few more times later)

What's happening here is you are erasing the iterator it. This makes it invalid!

Following this your for loop attempts to call the incremental operator '++', however, now the iterator is invalid, the operation will fail, causing the assertion error you're having problems with.

If you check the documentation for std::vector<T>::iterator::erase(...) you will see that erase as a function returns the next element.

Due to this, a better way of performing this for loop(s) would be to use a while loop like this:

it = (buffer.begin()+count);
while( it != buffer.end() )
{
	if ((*it)%2)
	{
		it = buffer.erase(it); // this increments for you!
	}
	else
	{
		iter++; // if we don't erase just keep going through the list!
	}
}

Hope this helps.

Regards,
Adam