Program stuck in infinite loop

closed account (41pkoG1T)
I'm writing this program as an assignment for a class, and when I run it, after the user inputs it gets stuck in an infinite loop. I've been looking at it forever and can't seem to find where it gets stuck. Any help would be great.

Here's the code:

#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;

const int ROWS = 20;
const int COLS = 20;

// prompt user to enter width and verify that it is between 3 and 20
// Pre: N/A
// Post: width
int getWidth() {
int width=0;

while (width < 3 || width > 20) {
cout << "Please enter a width from 3 to 20: ";
cin >> width;
}
return width;
}

// prompt user to enter height and verify that it is between 3 and 20
// Pre: N/A
// Post: height
int getHeight () {
int height=0;

while (height < 3 || height > 20) {
cout << "Please enter a height from 3 to 20: ";
cin >> height;
}
return height;
}

// prompt user to enter the number of mines and verify that it is <1 and >(w*h)/4
// Pre: width, height
// Post: number of mines
int getMineCount(double w, double h) {
int mineCount=0;

while (mineCount<1 || mineCount > ((w*h)/4)) {
cout << "Please enter the number of mines: ";
cin >> mineCount;
if (mineCount<1 || mineCount > ((w*h)/4))
cout << "Mine count out of bounds. Try again." << endl;
}
return mineCount;
}

// output a random number
// Pre: highest number to randomly pick
// Post: random number
int randInt(int max) {
int number = rand()%max;
return number;
}

void clearMinefield(char minefield[ROWS][COLS], int width, int height) {

for (int row=0; row<=height; row++) {
for (int column=0; column<=width; column++) {
minefield[row][column]=0;
}
}
return;
}

// randomly place mines on the minefield
// Pre: minefield array, number of mines, width, height
// Post: mines randomly placed on minefield
void placeMines(char minefield[ROWS][COLS], int mines, int width, int height) {
int row;
int column;

for (int c=0; c<mines; c++) {
row = randInt(height);
column = randInt(width);
if (minefield[row][column]==0)
minefield[row][column]=-1;
else
c--;
}
return;
}

// calculate the number of mines next to the each space
// Pre: minefield array
// Post:
int findNumberOfMinesAjacent(char minefield[ROWS][COLS], int row, int column) {
int numberOfMinesAjacent=0;

for (int x=(row-1); x=(row+1); x++)
for (int y=(column-1); y=(column+1); y++)
if (minefield[x][y]==-1)
numberOfMinesAjacent++;

return numberOfMinesAjacent;
}

// fill unmined spaces with number of ajacent mines
// Pre: minefield array, width, height
// Post: unmined spaces show how many mines are ajacent
void fillRemainingSpaces(char minefield[ROWS][COLS], int width, int height) {
for (int row=0; row<=height; row++)
for (int column=0; column<=width; column++)
if (minefield[row][column] != -1)
minefield[row][column]=findNumberOfMinesAjacent(minefield, row, column);
return;
}

// display the minefield array
// Pre: minefield array
// Post: minefield array is displayed
void displayMinefield(char minefield[ROWS][COLS]) {
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++)
cout << minefield[r][c];
cout << endl;
}
return;
}

int main () {
char minefield[ROWS][COLS];
int width = getWidth();
int height = getHeight();
int mines = getMineCount(width, height);

srand(time(0));

clearMinefield(minefield, width, height);

placeMines(minefield, mines, width, height);

fillRemainingSpaces(minefield, width, height);

displayMinefield(minefield);

return 0;
}
1
2
3
4
5
6
7
8
9
10
int findNumberOfMinesAjacent(char minefield[ROWS][COLS], int row, int column) {
    int numberOfMinesAjacent=0;
    
    for (int x=(row-1); x=(row+1); x++)
    for (int y=(column-1); y=(column+1); y++)
    if (minefield[x][y]==-1)
       numberOfMinesAjacent++;
    
    return numberOfMinesAjacent;
}

check the 2 for statements
closed account (41pkoG1T)
Thank you thank you thank you! I looked over it 100 times and kept missing that.
Topic archived. No new replies allowed.