I have written code that is supposed to be an 11 X 11 grid. A mouse is placed on the center spot and until such time that it touches each and every square, the program will continue to run. I also need to count how many times the mouse touches a wall if he is in an outer square. I am able to compile with code with two warnings saying results will always be true which is fine because once the results are true, my program should stop. My problem is when I run the program, it crashes and I get an error asking if I want to submit it to Microsoft. Would anyone be willing to see if you can tell me where my issue is. Thanks.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <conio.h>


using namespace std;
// constants

  const int MYMAXROW = 11;
  const int MYMAXCOL = 11;

//header

struct squaretype
{
    int row;     //the row location of this square
    int col;     //the column location of this square
    int frequency;  // number of times touched by mouse
    int order; //to be explained in class
}; 


//function prototype

int allcovered(squaretype grid[][MYMAXCOL]); 
    
void printgrid(squaretype grid[][MYMAXCOL],
              int mouseRow, int mouseCol);
                             
squaretype findmaxLocation (squaretype grid[][MYMAXCOL]);
  

// end of header

//start of main
int main ()
{  
   
  int table [11][11];
  squaretype grid[MYMAXROW][MYMAXCOL];
  int i, j;
  
   
  //rand number test
  //const int arraySize = 10;
  
  //srand(time(0));
  srand(15);
  
  grid[i][j].row = i;
  grid[i][j].col = j;
  grid[i][j].frequency = 0;
  
  //double for loop
  //declaration of two-dimensional array
   
  int location;
  int numberofmoves;
  int move;
   
   
   for (i = 0; i < 11; i++) //initialize and set counter to zero
   {
       for (j = 0; j < 11; j++)
       {
           grid[i][j].frequency = (i * j);
       }
   }
   
   cout << grid << endl;
   cout << "sizeof grid = " << sizeof(grid) << endl;
   
   i = 5;  //# where mouse starts
   j = 5;  //# where mouse starts
   
   location = (i * (5 * sizeof(int))) + (j + sizeof(int));
   
   int row, col, wallhit;
   int currentRow = MYMAXROW/2;
   int currentCol = MYMAXCOL/2;
     cout << allcovered(grid) << endl; //gives squares not touched
   grid[currentRow][currentCol].frequency++; //place mouse down
     cout << allcovered(grid) << endl; //squares not touched
  getch();
  
   //still in main  and inside while loop
   
   while (allcovered(grid) && numberofmoves < 100000) //safety variable
   {
    move = rand() % 8; //switch on this
    
    switch (move) //all moving from m generating possible moves
    {
        case 0:
          row = -1;
          col = -1;
          break;
        case 1:
          row = -1;
          col = 0;
          break;
        case 2:
           row = -1;
           col = 1;
           break;
        case 3:
           row = 0;
           col = 1;
           break;
        case 4:
           row = 1;
           col = 1;
           break;
        case 5:
           row = 1;
           col = 0;
           break;
        case 6:
           row = 1;
           col = -1;
           break;
        case 7:
           row = 0;
           col = -1;
           break;
           
      
    }
       
            //in main still in while after switch
            // row hits
            
        if ((currentRow + row) < 0)
        {
            wallhit++;
        }
        else if ((currentRow + row) > MYMAXROW)
        {
            wallhit++;
        }
        else //upate row
        {
            currentRow = currentRow = row;
        }
        
            //column hits
         
        if (((currentCol + col) < 0))
        {
            wallhit++;
        }                     
        else if ((currentCol + col) > MYMAXCOL)
        {
            wallhit++;
        }
        else //update row
        {
            currentCol = currentCol = col;
        }    
        
        grid[currentRow][currentCol].frequency++; //update grid will go to 0
        
        numberofmoves++; //counter growing while grid shrinks
        
        if (numberofmoves % 100) //shows location of mouse
        {
            printgrid(grid, currentRow, currentCol);
            //possibly print out # of moves
            cout << allcovered;
        }    
   }    //end of while - did mouse touch all squares
   
   //report responses - max, how many times did mouse hit wall
   //how many times squares hit
   
   if(allcovered(grid)==0)
     {
        cout << printgrid; //print results
     }   
   else
     {
        //notify user
     }   
      
   
  getch();
  return 0;

}// end of main

//footer

int allcovered(squaretype grid[][MYMAXCOL]) //get to 0
{
     int retval;
     int i, j; //need to assign value
     retval = 0; //counter counting # of squares
     for (i=0; i < MYMAXROW; i++)
     {
         for (j = 0; j < MYMAXCOL; j++)
         {
            if(grid[i][j].frequency==0)
            {    
              retval++;
            }
         }
     }
 
 return(retval); //1st time thru 121,120 all true til it hits 0
 
 //get to 0 - done
 
}                      
void printgrid(squaretype grid[][MYMAXCOL],
               int mouseRow, int mouseCol) //has mouse been here or not
                                        //not necessarily needed/prints grid
                                       //doesnt need return stmt                                        
{
    int i, j;
    for (i = 0; i < MYMAXROW; i++)
    {
        for(j = 0; j < MYMAXCOL; j++)
        {
          if((i==mouseRow)&&(j==mouseCol))
          {
            cout << "Mouse is here";
          }
          else if(grid[i][j].frequency > 0)
          {
            cout << "X";
          }   
          else
          {
            cout << "O";
          }
          
        } 
      
   } //end outer forloop
   //cout << endl;
}//end print grid

squaretype findmaxLocation (squaretype grid[][MYMAXCOL])
{
   int retval = 0; //counter counting # of squares
   int i, j; //need to assign value
   
   for (i=0; i < MYMAXCOL; j++)
   {
       for (j=0; j < MYMAXCOL; j++)
       {
           if(grid[i][j].frequency==0)
           {
               retval++;
           }
       }
   } 
}

>Would anyone be willing to see if you can tell me where my issue is.
That's simple enough. Your issue is here:

int i, j;

And it manifests here:

grid[i][j].row = i;
grid[i][j].col = j;
grid[i][j].frequency = 0;

i and j aren't initialized. Unless you get very lucky and your compiler automatically initializes local variables to 0, i and j are extremely likely to have garbage values that are well outside the bounds of your array.

thanks!

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.