Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I coded this but it seems to be stuck in an infinite loop somewhere and isn't outputting anything at all.

Any help would be much appreciated.

C++
int board[8][8] = {0}, r =0, c=0, count =0;
    board[0][0] = 1;
    
    
NC: c++;
    if(c==8){
        goto PRINT;
    }
    r =-1;
    
NR: r++;
    if(r==8)
        goto BT;
    
  for(int i=0; iif(board[r][i] == 1)
            goto NR;
    }
    
    for(int i=1; (r-i)>=0 && (c-i)>=0; i++){//Up-diagonal test
        if(board[r-i][c-i] == 1)
            goto NR;
    }
    
    for(int i =1; (r+i)<=8 && (c-i)>=0; i++){//Down-diagonal test
        if(board[r+i][c-i] == 1)
            goto NR;
        else{
            board[r][c] =1;
            goto NC;
        }
    }
    
BT: c--;
    if(c == -1)
        return 0;
    r=0;
    
    while(board[r][c] != 1){//Search the column until a queen is found
        r++;
    board[r][c] = 1;//Once a queen is found, take it away and continue the search
    goto NR;//Stay in the column and look for more options
    }
    
    
PRINT:
   // void print(int board[][8]){
    //    static
        cout<< "Solution #"<< count++<< ":"<< endl;
        for(int i =0;i<8; i++){
            for(int j =0; j<8; j++){
                cout<< board[i][j];
            }
            cout<< endl;
        }
    goto BT;
   // }
}
Posted
Updated 23-Sep-13 14:28pm
v3
Comments
PIEBALDconsult 23-Sep-13 20:30pm    
Sounds like home work, so you may not get much help here, and if it's homework you might consider getting rid of the gotos before anything else.
Sergey Alexandrovich Kryukov 23-Sep-13 20:34pm    
Of course, get rid of goto and use the debugger. There is no such thing as "somewhere". Use the debugger and find it.
—SA

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900