Console Program just stops

Hey all!

So basically what happens is I run the program and when it gets to the line with all the stars, it just stops. It doesn't exit or crash, it just sits there. But I can't do anything either. All the debug controls get disabled. Any help would be appreciated. Thanks in advance!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

bool diagonal(int (*board)[8], int, int, int);

int _tmain(int argc, _TCHAR* argv[])
{
	
	int i, j, direction;
	bool test = true;
	char pause;
	int chessBoard[8][8];
	for(i = 0; i < 8; i++)
	{
		for(j = 0; j < 8; j++)
		{
		   chessBoard[i][j] = 0;
		   cout << chessBoard[i][j] << " ";
		}
		cout << endl;
	}
	for(i = 0; i < 8; i++)
	{
	  for(j = 0; j < 8; j++)
	  {
            direction = 1;
	    while(direction < 5 && test != false)
	    {
****************test = diagonal(chessBoard, i, j, direction);*******************
                direction++;
            }
            if(test == true)
            {
                chessBoard[i][j] = 1;
                break;
            }
	  }
	}
	for(i = 0; i < 8; i++)
	{
	   for(j = 0; j < 8; j++)
	      cout << chessBoard[i][j] << " ";
	   cout << endl;
	}

    cout << "Press any key + Enter to continue..." << endl;
    cin >> pause;
    return 0;
}

bool diagonal(int (*board)[8], int i, int j, int dir)
{
     int l = 0, m = 0, n = 0, o = 0;
     bool diagTest = true;
     
     switch(dir)
     {
        case 1:
             while((i-l) >= 0 && (j-l) >= 0)
	     {
                if(board[i-l][j-l] == 0)
                    diagTest = true;
                else
                    diagTest = false;
             }
             break;
        case 2:
             while((i+m) < 8 && (j+m) < 8)
	     {
	        if(board[i+m][j+m] == 0)
                    diagTest = true;
                else
                    diagTest = false;
             }
             break;
        case 3:
             while((i+n) < 8 && (j-n) >= 0)
	     {
        	if(board[i+n][j-n] == 0)
                    diagTest = true;
                else
                    diagTest = false;
             }
             break;
        case 4:
             while((i-o) >= 0 && (j+o) < 8)
             {
        	if(board[i-o][j+o] == 0)
                    diagTest = true;
                else
                    diagTest = false;
             }
             break;
     }
     
     return diagTest;
}


For those of you who can't already tell from the code, I'm trying to write a program to solve the 8 queens problem in chess. Obviously this isn't the final solution, but I need to figure this error out before I can do anything else.

The output looks like this:

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0


There should be 1's where the queens would be, but obviously that's not happening...
Last edited on
In diagonal
1
2
while((i-l) >= 0 && (j-l) >= 0){ //while true
//execute a code that doesn't change i, j or l 
I hope you've got good eyes

Also test != false who teach you that? It's obfuscated
@ ne555: That bit about test != false is a Windows thing, because BOOL is a seperate data type in Windows then bool is in C\C++; and it actually returns an int that is only '0' when whatever fails, but may return any one of a range of integer values if it passes.

Reference: http://msdn.microsoft.com/en-us/library/aa383751(VS.85).aspx#BOOL

EDIT: I noticed that he used bool not BOOL but habits like that die hard, I find myself doing it to.
Last edited on
But if( test ) or if( not test ) should cast correctly (even in C).
Edit: it seems that C does not support the alternative operators.
Last edited on
@ne555:
1
2
while((i-l) >= 0 && (j-l) >= 0){ //while true
//execute a code that doesn't change i, j or l  


I passed i, j, and l by value not by reference, so changing them within bool diagonal shouldn't be a problem. I don't think that's the problem here....

EDIT: I assume that's what you were referring to anyway.
Last edited on
Simplified version
1
2
3
int a=42;
while( a==42 )
    cout << '*';
That is an infinite loop, the condition will be always evaluated as true, because a never changes its value

Edit: What is diagonal supposed to do? l=m=n=o=0;
Last edited on
Wow, yeah, infinite loop was the problem. What a fail. I had everything there, just made a stupid mistake. Thanks for the help.

And diagonal is supposed to test to make sure the current position in the array is a valid location for a queen.

Again, thanks!
Topic archived. No new replies allowed.