spstephens
spstephens

Reputation: 73

Having trouble compiling my first c++ program

Here is my code:

#include<iostream>

private int chessBoard[8][8];

void printBoard(int board[][])
{
    for(int i=0; i<8; i++)
    {
        for(int j=0; j<8; j++)
        {
            if(board[i][j])
            {
                cout<<"Q ";
            }

            else
            {
                cout<<"* ";
            }
        }
    cout<<endl;
    }
}

//This method will check if any queens are in attacking position to the left of the test queen placement
bool checkSpot(int board[][],int row, int col)
{
    bool safe = true;
    //checks the current row for queens
    for(int i=0; i<col, i++)
    {   
        if(board[row][i])
        safe=false;
    {

    //checks the upper diag
    for( int i=row, int j=col; i>0 && j>0; i--, j--)
    {
        if(board[i][j])
        safe=false;
    }

    //checks lower diag
    for(int i = row, int j=col; i<8 && j>0; i--, j++)
    {
        if(board[i][j])
        safe=false;
    }

    if(safe)
    {
        return true;
    }
    else
        return false;
}

bool solve(int board[][], int testCol)
{
    bool solved = false;
    bool safe;
    if(testCol==8)
    {
        solved = true;
        return solved;
    }

    for(int i =0; i>8; i++)
    {
        // test if the tested column(testCol) and the row(i) are both safe for the queen to be placed at then we can move into placing said queen and more onto the next column for
        // more solutions in this same method recursivly
        safe = checkSpot(board, i, testCol);
        if(safe)
        {
            //place the queen
            board[i][col]=1;
            //recursion to go back through this method in the next column
            if(solve(board[][], testCol+1)
            {
                solved = true;
                printBoard(board)
                return solved;

            }

            else
            {
                //if the queen cannot be placed, we have to remove the previous queens and move them until a solution is found.
                board[i][testCol]=0;

        }
    }

}       

int main()
{


    solve(chessBoard, 0);
}

There errors i continue to get are as follows:

8queens.cpp:3:17: error: variable or field ‘printBoard’ declared void
 void printBoard(board[][])
                 ^
8queens.cpp:3:17: error: ‘board’ was not declared in this scope
8queens.cpp:3:23: error: expected primary-expression before ‘]’ token
 void printBoard(board[][])
                       ^
8queens.cpp:3:25: error: expected primary-expression before ‘]’ token
 void printBoard(board[][])

The logic of this was pretty simple(at least i hope it works well) but i cant even get past the compiler. Can I get a little guidance on this problem?

You were all a huge help, but sadly i found out that I can only use 1d arrays for this problem so I had to start from scratch. Again thank you all for the help, your advice will definitely help me in the future.

Upvotes: 0

Views: 100

Answers (2)

Arun A S
Arun A S

Reputation: 7026

Well, there are many errors in your code. Lets look at them.

First of all, remove that private from

private int chessBoard[8][8];

you need to use private only on the members of classes . So change it to

int chessBoard[8][8];

Next, in all your functions, you have something like

void printBoard(int board[][])
                            ^
                            here, it's wrong

You need to provide the size, you can only skip the first size, all the rest must be provided so better to change it to

void printBoard(int board[][8])
                          ^
                          it's okay to leave this one

make that change to all of your functions.

You are also missing a few } in some places of your code.

Nearly forgot, you need to either add a

using namespace std;

right after the headers, or use

std::cin
std::cout

and

std::endl

instead of cin , cout and endl .

You also have

for(int i=0; i<col, i++)
                  ^
                  you need a semicolon here

And also

for( int i=row, int j=col; i>0 && j>0; i--, j--)
                 ^
                no need for another int here

just change that to

for( int i=row, j=col; i>0 && j>0; i--, j--)

Upvotes: 2

Tman
Tman

Reputation: 146

This compiles. As to what I did, there was a lot and it would take awhile to write each thing down. One major thing to pay attention to is the for loops. You have:

for( int i=row, int j=col; i>0 && j>0; i--, j--)
{
    if(board[i][j])
      safe=false;
}

When you want to do a nested for loop, do this:

for (int i = row;  i > 0; i--)
{
     for (int j = col; j > 0; j--)
     {
          if (board[i][j])
             safe = false;
     }
}

Also, pay attention to your brackets {} () as they were inconsistent and some were missing.

For arrays, you must specify at least one dimension when using a 2d array.

You had: int board[][] in various places. You must have: int board[][8] instead.

Also, if you want to use cout, endl, cin, etc... You must have include namespace std; otherwise you will have to use: std::cout, std::endl, std::cin. But anyway, this should compile now.

#include<iostream>

using namespace std;

int chessBoard[8][8];

void printBoard(int board[][8])
{
    for (int i = 0; i<8; i++)
    {
        for (int j = 0; j<8; j++)
        {
            if (board[i][j])
            {
                cout << "Q ";
            }
            else
            {
                cout << "* ";
            }
        }
        cout << endl;
    }
}

//This method will check if any queens are in attacking position to the left of the test queen placement
bool checkSpot(int board[][8], int row, int col)
{
    bool safe = true;
    //checks the current row for queens
    for (int i = 0; i < col; i++)
    {
        if (board[row][i])
            safe = false;
        {
            //checks the upper diag
            for (int i = row;  i > 0; i--)
            {
                for (int j = col; j > 0; j--)
                {
                    if (board[i][j])
                        safe = false;
                }
            }

            //checks lower diag
            for (int i = row; i < 8;  i--)
            {
                for (int j = col; j > 0; j++)
                {
                    if (board[i][j])
                        safe = false;
                }
            }

            if (safe)
            {
                return true;
            }
            else
                return false;
        }
    }
}

        bool solve(int board[][8], int testCol)
        {
            bool solved = false;
            bool safe;
            if (testCol == 8)
            {
                solved = true;
                return solved;
            }

            for (int i = 0; i > 8; i++)
            {
                // test if the tested column(testCol) and the row(i) are both safe for the queen to be placed at then we can move into placing said queen and more onto the next column for
                // more solutions in this same method recursivly
                safe = checkSpot(board, i, testCol);
                if (safe)
                {
                    //place the queen
                    board[i][testCol] = 1;
                    //recursion to go back through this method in the next column
                    if (solve(board, testCol + 1))
                    {
                        solved = true;
                        printBoard(board);
                        return solved;
                    }
                    else
                    {
                        //if the queen cannot be placed, we have to remove the previous queens and move them until a solution is found.
                        board[i][testCol] = 0;
                    }
                }
            }
        }

            int main()
            {
                solve(chessBoard, 0);
            }

Upvotes: 0

Related Questions