Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Errors I get following errors on this code and cannot figure out the problem

ID: 3685617 • Letter: C

Question

C++ Errors

I get following errors on this code and cannot figure out the problem.

I bolded the lines 21 and 22 that the error is occuring.

project3.C: In function `int main()':
project3.C:15: warning: comparison between signed and unsigned
integer expressions
project3.C:21: error: ISO C++ forbids variable-size array `grid'
project3.C:21: error: ISO C++ forbids variable-size array `grid'
project3.C:22: error: ISO C++ forbids variable-size array `gridc'
project3.C:22: error: ISO C++ forbids variable-size array `gridc'

-----------------------------------------------------------------------------------------------------------------------------------------------------

#include
#include
#include
#include
using namespace std;
int main()
{
   ifstream file("data.txt");
   string a,word[10];
   int i=0,l=0;
   srand(time(NULL));
   while(!file.eof())
   {
       file>>a;
       if(l            l=a.length();
       word[i++]=a;
   }
   i--;
   int size=l+1; //grid size wil be length of longest word + 1
   char grid[size][size];
   int gridc[size][size]={0};

   for(int x=0;x        for(int y=0;y            grid[x][y]='0';
  
   //placing the words in the grid word by word
   for(int k=0;k    {
       int length=word[k].length();
       int j=rand()%3;
      
       switch(j)
       {
           //horizontal
           case 0:
               {
                   int r=rand()%size;   //to find the row to start the word
                   int c=rand()%(size-length); // this is find the column to start the word
                   for(int m=0;m                    {
                   //if cell is empty then allocate letter
                       if(grid[r][c]=='0')
                           grid[r][c++]=word[k][m];
                           //if the cell contains the same letter of the word already then update gridc to track that letter belongs to some other word also
                       else if(grid[r][c]==word[k][m])
                       {
                           gridc[r][c]=1;
                           c++;
                       }  
                       else
                       {
                           //to deallocate the allocated letters
                           for(int n=m;n>=0;n--)
                           {
                               if(gridc[r][--c]==0)
                                   grid[r][c]='0';
                           }
                          
                           k--; //going back to the same word and processing
                           break ;
                       }
                      
                   }
                   break;
               }
           //vertical
           case 1:
               {
                   int r=rand()%(size-length);   //to find the row to start the word
                   int c=rand()%size; // this is find the column to start the word
                   for(int m=0;m                    {
                       //if cell is empty then allocate letter
                       if(grid[r][c]=='0')
                           grid[r++][c]=word[k][m];
                       //if the cell contains the same letter of the word already then update gridc to track that letter belongs to some other word also
                       else if(grid[r][c]==word[k][m])
                       {
                           gridc[r][c]=1;
                           r++;
                       }  
                       else
                       {
                           //to deallocate the allocated letters
                           for(int n=m;n>=0;n--)
                           {
                               if(gridc[--r][c]==0)
                                   grid[r][c]='0';
                           }
                           k--;//going back to the same word and processing
                           break ;
                       }
                   }
                   break;
               }
           //diagonal
           case 2:
               {
                   int r=rand()%(size-length);   //to find the row to start the word
                   int c=rand()%(size-length); // this is find the column to start the word
                   for(int m=0;m                    {
                   //if cell is empty then allocate letter
                       if(grid[r][c]=='0')
                           grid[r++][c++]=word[k][m];
                           //if the cell contains the same letter of the word already then update gridc to track that letter belongs to some other word also
                       else if(grid[r][c]==word[k][m])
                       {
                           gridc[r][c]=1;
                           r++;
                           c++;
                       }
                      
                       else
                       {
                           //to deallocate the allocated letters
                           for(int n=m;n>=0;n--)
                           {
                               if(gridc[--r][--c]==0)
                                   grid[r][c]='0';
                           }
                           k--;//going back to the same word and processing
                           break ;
                       }
                   }
                   break;
               }
       }
   }
   //to fill the empty cells with random letters
   srand(time(NULL));
   for(int x=0;x        for(int y=0;y        {
           if(grid[x][y]=='0')
           {
          
               int val=rand()%26;
               val+=97;
               grid[x][y]=val;
           }
       }
          
      
   //to print the grid puzzle  
   for(int x=0;x    {
       cout<        for(int y=0;y            cout<

Explanation / Answer

The error is happening because you are allocating memory statically to array using a variable whose value will be determined at running.

Fix:

Use dynamically allocation of array:

//dynamic allocation
   char** grid = new char*[size];
   for(int i=0;i<size;i++)
       grid[i] = new char[size];

   char** gridc = new char*[size];
   for(int i=0;i<size;i++)
       gridc[i] = new char[size];

   //initiallization
   gridc[0][0] = 0;


   //freeing up memory written at the end of program
   for(int i=0;i<size;i++)
   {
       delete grid[i];
       delete gridc[i];
   }  

   delete grid;
   delete gridc;