Could someone help me with this problem?

Pages: 12
Here is a more complete version that still doesn't work:
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
#include <iostream>
using namespace std;

int main()
{
  int q = 0;
  int board[8];
  cout << "Enter the rows containing queens, in order by column: ";
 for(int i = 0; i < 8; i++)
   cin >> board[i];
       for (int i2 = 0; i2 < 8; i2++)
       {
       q = 0; 

               for (int j = 0; j < 8; j++)
               {
                       if (board [j] == i2)
                       {
                               q++; 
                       }
               }

       if (board[q]+1 == board[q+1] || board[q]-1 == board[q+1])
               {
             cout << endl;
                       cout << "Unsafe";
                       return 0;
               }
       }

       cout << endl;
       cout << "Safe";
}

Can someone help with it?
Update: I found a solution, but a if condition involved in it requires = (as opposed to ==) to work properly, which is not acceptable; is there any way to get it working with the ==? The code with the == (non-working, but if you change it to = it will work but that's not accepted) is in:
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
#include <iostream>
using namespace std;

int main()
{
  int q = 0;
  int board[8];
  cout << "Enter the rows containing queens, in order by column: ";
 for(int i = 0; i < 8; i++)
   cin >> board[i];
       for (int i2 = 0; i2 < 8; i2++)
       {
       q = 0; 

               for (int j = 0; j < 8; j++)
               {
                       if (board [j] == i2)
                       {
                               q++; 
                       }
               }
                 /* this is the if condition that needs the single = to work */   if(board[i2] == board[q]) {
                                    cout << "Unsafe";
                                    return 0;
                                    }
       if (board[q]+1 == board[q+1] || board[q]-1 == board[q+1])
               {
             cout << endl;
                       cout << "Unsafe";
                       
                       return 0;
               }
               
       }

       cout << endl;
       cout << "Safe";
}

Is there any way for me to get it working with the ==? I'm not even sure why it won't work with the ==, never seen that before.
its a complete mess.
if you code as we suggested you can finish your work faster.

it is your previous thread:
http://www.cplusplus.com/forum/beginner/157377/

in both thread people suggested to use 2d array. but you are still on your own. if you don't follow suggestion, why people would suggest you ?

I can't follow a 2D array.
bump
I can't follow a 2D array.

Please explain.

keltonfan2 wrote:
I don't want to use recursion or multiple functions (if possible)
I don't want to use any header files I haven't already included
I don't know how to fill in from files or any of this.
I just want to get the program working.

It sounds like you want to get a working program without knowing anything or doing anything.
That is barely acceptable, when some actual work has to be done using such program.
That is absolutely not acceptable, if the whole purpose is to learn programming.

You should want to know. You should want to experiment. You should want to use available options.
The bump, bump, bump is not an option.


On your latest code on line 17 you still have to test whether a value in the board matches some index, because your input routine does not make such guarantee. Lets add that for you:
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
#include <iostream>

int main()
{
  int board[8] {};
  std::cout << "Enter the rows (values [1..8]) containing queens, in order by column:\n";
  int col = 0;
  while ( col < 8 ) {
    int value = 0;
    std::cout << "Row on column " << static_cast<char>('A'+col) << ' ';
    if ( ! (std::cin >> value) ) return 1; // input failure
    if ( 0 < value && value <= 8 ) {
      board[col] = value-1;
      ++col;
    }
  }
  std::cout << '\n';

  // now the board contains valid values
  // show the queens:
  for ( col = 0; col < 8; ++col ) {
    const char column = 'A' + col;
    std::cout << 'Q' << 1+col << " is in " << column << 1+board[col] << '\n';
  }

  // checking the status of the board starts here


  return 0;
}


This board has queens on the first two columns. The 'x' mark where they can attack (row and diagonals):
 ABCDEFGH
8...xx...
7x.xx....
6xQxxxxxx
5xxx.....
4Qxxxxxxx
3.x..x...
2..x..x..
1...x..x.

The third queen is on column C:
 87654321
C.xxxx.x.

If the queen is on C8, C3 or C1, then it is "safe so far" and you must mark the rest of its row and diagonals, just like the first queens have marked theirs.
If the queen is on any other row, then the entire board is Unsafe.
Update:
I have updated my code and it now works for some numbers, but not others. I'm still not sure why it isn't working and I need more help.
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
#include <iostream>
using namespace std;

int main()
{
  int q = 0;
  int board[8];
  cout << "Enter the rows containing queens, in order by column: ";
 for(int i = 0; i < 8; i++) {
   cin >> board[i];
}
cout << endl;
       for (int i2 = 0; i2 < 8; i2++)
       {
       q = 0; 

               for (int j = 0; j < 8; j++)
               {
                       if (board [j] == i2)
                       {
                               q++; 
                       }
               }
                 board[i2] = board[q];
        if(board[i2] != 0) {
            cout << "Unsafe";
            return 0;
        }
       if (board[q]+1 == board[q+1] || board[q]+2 == board[q+2] || board[q]+3 == board[q+3] || board[q]+4 == board[q+4] || board[q]+5 == board[q+5] || board[q]+6 == board[q+6] || board[q]+7 == board[q+7] ||  board[q]-1 == board[q+1] || board[q]-2 == board[q+2] || board[q]-3 == board[q+3] || board[q]-4 == board[q+4] || board[q]-5 == board[q+5] || board[q]-6 == board[q+6] || board[q]-7 == board[q+7])

               {
             cout << endl;
                       cout << "Unsafe";
                       
                       return 0;
               }
               
       }

       cout << endl;
       cout << "Safe";
}
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
#include <iostream>
int main (){	

	bool safe = true;
	int board[8];
	std::cout << "enter values(1 to 8) for 8 columns: \n";
	for(auto &x:board){
		std::cin >> x;
	}
	
	int uleft, uright, dleft, dright;
	
	for(int i=0; i<8; i++){
		// row check
		for(int j=0; j<8; j++){  
			if(board[i]==board[j] && i!=j){
				safe = false;
			}
		}
		
		uleft=uright=dleft=dright= board[i];
		
		// check diagonaly to the right
		for(int j=i+1; j<8; j++){ 
			if(++uright==board[j] || --dright==board[j]){
				safe = false;
			}
		}
		
		// check diagonaly to the left
		for(int j=i-1; j>=0; j--){ 
			if(++uleft==board[j] || --dleft==board[j]){
				safe = false;
			}
		}		
	}
	
	if(safe) std::cout << "correct position!";  //example: 6 4 7 1 8 2 5 3
	else std::cout << "capture possible";
	
return 0;
}
It won't compile.
7 expected primary-expression before "auto"
7 expected `;' before "auto"
7 expected `;' before "auto"
11 expected `;' before "int"
11 expected primary-expression before "int"
11 expected `)' before "int"
21 `uleft' undeclared (first use this function)
21 `uright' undeclared (first use this function)
21 `dleft' undeclared (first use this function)
21 `dright' undeclared (first use this function)
The C++11 support in the compiler. Do you have it enabled?
activate C++11 support.
Topic archived. No new replies allowed.
Pages: 12