'Why does my program go into an infinite loop?

#include <iostream>
#include <iomanip>
#include <string>


int main(){

    int r,c,g;

    std::cin>>r>>c>>g;

    char box[r][c];
    char copy[r][c];

    for(int i = 0; i < r; ++i){
        for(int j = 0; j < c; ++j){
            std::cin >> box[i][j];
            }
    }
    
    
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            copy[i][j] = box[i][j];
        }
    }


    //making a copy of box
    for(int i=0;i<g;i++){
        int count = 0;
        for(int i=0; i<r; i++){
            for(int j=0; j<c; j++){
                count =0;
                
                 if((i-1 >= 0 ) && (j-1>=0)) {
                     if(copy[i-1][j-1] == '*'){
                       count+=1;
                   }
                 }
                   
                   
                if(j-1>=0){
                   if(copy[i][j-1] == '*'){
                       count+=1;
                   }
                }
                
                
                if((i+1 < r ) && (j-1>=0)){
                   if(copy[i+1][j-1] == '*'){
                       count+=1;
                   }
                 }
                 
                 if(i+1<r){
                   if(copy[i+1][j] == '*'){
                       count+=1;
                   }
                 }
                 
                 if((i+1<r) && (j+1<c)){
                   if(copy[i+1][j+1] == '*'){
                       count+=1;
                   }
                 }
                 
                 if(j+1<c){
                   if(copy[i][j+1] == '*'){
                       count+=1;
                   }
                 }
                 
                 
                 if ((i-1>=0) && (j+1<c)){
                   if(copy[i-1][j+1] == '*'){
                       count+=1;
                   }
                 }
                 if (i-1>=0){
                   if(copy[i-1][j] == '*'){
                       count+=1;
                   }

                 }
                 
            //modify the original grid based on the rules
            if ((count<2) || (count>3)){
                box[i][j] = '.';
            }

            if((copy[i][j]=='.') && (count == 3)){
                box[i][j] = '*';
            }
            
            
           
        
            } 
        }

            
         for(int i=0;i<r;i++){
             for(int j=0;j<c;j++){
                 box[i][j] = copy[i][j];
                }   
            }        
        }
    
    
        for(int i=0; i<r; i++){
            for(int j=0; j<c; j++){
                if (j < c - 1){
                    std::cout<<box[i][j]<<" ";
                }
                else{
                    std::cout<<box[i][j];
                }
    
            }
            std::cout<<std::endl;
        }
    
    
    
    
}
   

So for some context, I must implement Conway's Game of Life using a two-dimensional array as a grid in which I store the cells. Live cells are denoted as * characters, dead cells are denoted by the '.' character. enter image description here

The rules for the game are as followed:

  • Any live cell with fewer than two live neighbors dies (as if by underpopulation).
  • Any live cell with more than three live neighbors dies (as if by overpopulation/overcrowding).
  • Any live cell with two or three live neighbors lives, unchanged, to the next generation.
  • Any dead cell with exactly three live neighbors will come to life (as if by reanimation or birth).
  • Neighbors refer to the eight cells adjacent to any given cell in a grid, with the exception of border cells.

Now I must check the copy of the initial grid and count the cells and modify the original based on these rules: enter image description here

Ive been debugging for hours and I really don't understand why my code goes into an infinite loop?



Solution 1:[1]

Usually infinite loops come from blunders, I read really quickly your code and spotted that inside the nested loop where you copy the box specifically at the most inner loop you increase i instead of j:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gigis95