I have written a function to place mines. I need 'X' to be in array position [0][0] I cannot randomize so it does not overlap the mines b/c I can have only four mines in a [4][5] array.

The problem I am having so that it checks to see if it is not already occupied and something with the 2nd if statement

void placeMines(char mineField[][5])
{
	int z;
	
	for(z = 0; z <4; z++)
	{

		int a, b;

		a = rand() % 4;
		b = rand() % 5;

		if(a != 0 && b != 0)
		{
			a = rand() % 4;
			b = rand() % 5;
		}
		
		mineField[a][b] = 'M';		
		
		if(mineField[a][b] == 'M')
		{
			a = rand() % 4;
			b = rand() % 5;
			mineField[a][b] = 'M';
		}	

	}
				
}

I have written a function to place mines. I need 'X' to be in array position [0][0] I cannot randomize so it does not overlap the mines b/c I can have only four mines in a [4][5] array.

The problem I am having so that it checks to see if it is not already occupied and something with the 2nd if statement

See the comments:

void placeMines(char mineField[][5])
{
	int z;
	
	for(z = 0; z <4; z++)
	{

		int a, b;

		a = rand() % 4;
		b = rand() % 5;

		if(a != 0 && b != 0)   // What is this for? 
		{                      // It seems to be useless to me.
			a = rand() % 4;
			b = rand() % 5;
		}
		
		mineField[a][b] = 'M';	// Change the value to M no matter 
                                        // what it is currently
		
		if(mineField[a][b] == 'M')  // This IF is ALWAYS true. You just 
                                            // changed the value
		{
			a = rand() % 4;
			b = rand() % 5;
			mineField[a][b] = 'M';
		}	

	}
				
}

Start with that and rethink this code.

if you look at the original post the cursor X starts at the [0][0] at the start of every game and if it starts there than there cannot be a mine on that position.

I was trying to see if there is a way so that when the iteration of the loop it needs to make sure a mine is placed on [0][0]. I need to check on the second,third and fourth iteration of the loop. i thought of the condition in the while loop
while(mineField[a] == 'M') and making it != will make it an infinite loop.

if you look at the original post the cursor X starts at the [0][0] at the start of every game and if it starts there than there cannot be a mine on that position.

The original post does not say this. And the code is far from doing it.

Write a truth table for your first IF statement and you'll will see why it's not doing what you want.

I was trying to see if there is a way so that when the iteration of the loop it needs to make sure a mine is placed on [0][0].

1) if you want to make sure a mine is at 0,0 just place one there. You will have to do a LOT of random calls to make sure you get two 0 values.
2) If X starts at 0,0 why would you want a mine there? Doesn't that make an immediate loss?

I need to check on the second,third and fourth iteration of the loop. i thought of the condition in the while loop
while(mineField[a] == 'M') and making it != will make it an infinite loop.

Yes, != will definitely make it an infinite loop, since the location is guaranteed to be 'M'. That guarantee make the IF worthless. It's always true so you will always place another mine. So you will place 8 mines -- but with no guarantee they will be in 8 separate locations. You might place mines on top of other mines.

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.