Hi, I've posted m code below and cant get it to work because of fatal error c1004 unexpected end-of-file found.
Appreciate any help.

#include <iostream>
using namespace std;

/*
//what i addedint q[8][8];
void printSolution();//int q[]);
void hereQueen(int q[], int n1, int n2);
*/

class Queens
{
    bool canGo;
    int squares, norm;
    int *locationRow;
    bool *column;

    bool *lDiag;
    bool *rDiag;

public:
    int Nqueens;
    Queens(int n)

    {
        canGo = true;
        squares=n;
        norm=squares -1;
        locationRow = new int[squares];
        column = new bool[squares];
        lDiag = new bool[squares*2 - 1];
        rDiag = new bool[squares*2 -1];
        Nqueens = 0;

        for(int i = 0; i < squares; i++)

        {
            locationRow[i] = -1;
            column[i] = canGo;
        };

        for(int i = 0; i < squares*2 -1; i++)
            lDiag[i] = rDiag[i] = canGo;
    };


    void hereQueen(int row)
    {
        char respond;
        for(int col = 0; col < squares; col++)
            if(column[col] == canGo &&
                lDiag[row+col] == canGo &&
                rDiag[row-col+norm] == canGo)
            {
                locationRow[row] = col;
                column[col] = !canGo;
                lDiag[row+col] = !canGo;
                rDiag[row-col+norm] = !canGo;
                if(row < squares - 1)
                    hereQueen(row+1);
                else 
                {printSolution(); };

                column[col] = canGo;
                lDiag[row+col] = canGo;
                rDiag[row-col+norm] = canGo;
            };

    };

void main()
{
    int boardSize;
    cout << "Starting Queens Problem..." << endl;
    cout << "Enter the board size (2-12) -->";
    cin >> boardSize;
    Queens queens(boardSize);

    queens.hereQueen(0);
    cout << "Number of solution is" << queens.Nqueens << endl;
}

void printSolution() //int q[])
{
    Nqueens++;
    cout << "Solution for the " << squares << " Queens problem" << endl;
    for(int i = 0; i < squares; i++) 

    {
        for(int j = 0; j < squares; j++)
            if(locationRow[i] == j)
                cout << "Q";
            else
                cout << "*";
        cout << endl;
    };

};

}

Recommended Answers

Duplicate post. Response posted in the other one.

Jump to Post

Oh. They deleted the thread with the response in and kept this one? That wasn't very smart.

Jump to Post

All 6 Replies

Duplicate post. Response posted in the other one.

Oh. They deleted the thread with the response in and kept this one? That wasn't very smart.

commented: Then don't post in BOTH threads. +14

What is main doing inside a class ? Also it should be int main() instead of void main().

What is main doing inside a class ? Also it should be int main() instead of void main().

Took main() out of class and declared it as int instead of void. Have new errors now:

error LNK1120: 1 unresolved externals
error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

#include <iostream>
using namespace std;

/*
//what i addedint q[8][8];
void printSolution();//int q[]);
void hereQueen(int q[], int n1, int n2);
*/


class Queens
{
    bool canGo;
    int squares, norm;
    int *locationRow;
    bool *column;

    bool *lDiag;
    bool *rDiag;

public:
    int Nqueens;
    Queens(int n)

    {
        canGo = true;
        squares=n;
        norm=squares -1;
        locationRow = new int[squares];
        column = new bool[squares];
        lDiag = new bool[squares*2 - 1];
        rDiag = new bool[squares*2 -1];
        Nqueens = 0;

        for(int i = 0; i < squares; i++)

        {
            locationRow[i] = -1;
            column[i] = canGo;
        };

        for(int i = 0; i < squares*2 -1; i++)
            lDiag[i] = rDiag[i] = canGo;
    };


    void hereQueen(int row)
    {
        char respond;
        for(int col = 0; col < squares; col++)
            if(column[col] == canGo &&
                lDiag[row+col] == canGo &&
                rDiag[row-col+norm] == canGo)
            {
                locationRow[row] = col;
                column[col] = !canGo;
                lDiag[row+col] = !canGo;
                rDiag[row-col+norm] = !canGo;
                if(row < squares - 1)
                    hereQueen(row+1);
                else 
                {
                    printSolution(); 
                };

                column[col] = canGo;
                lDiag[row+col] = canGo;
                rDiag[row-col+norm] = canGo;
            };

    };

void printSolution() //int q[])
{
    Nqueens++;
    cout << "Solution for the " << squares << " Queens problem" << endl;
    for(int i = 0; i < squares; i++) 
    {
        for(int j = 0; j < squares; j++)
            if(locationRow[i] == j)
                cout << "Q";
            else
                cout << "*";
        cout << endl;
    };

};
};

int main()
{
    int boardSize;
    cout << "Starting Queens Problem..." << endl;
    cout << "Enter the board size (2-12) -->";
    cin >> boardSize;
    Queens queens(boardSize);

    queens.hereQueen(0);
    cout << "Number of solution is" << queens.Nqueens << endl;
}

Hey, thanks!

IKept main() out of class but changd int main() to void main()
Got no errors.
Appreciate the help.

#include <iostream>
using namespace std;

/*
//what i addedint q[8][8];
void printSolution();//int q[]);
void hereQueen(int q[], int n1, int n2);
*/


class Queens
{
    bool canGo;
    int squares, norm;
    int *locationRow;
    bool *column;

    bool *lDiag;
    bool *rDiag;

public:
    int Nqueens;
    Queens(int n)

    {
        canGo = true;
        squares=n;
        norm=squares -1;
        locationRow = new int[squares];
        column = new bool[squares];
        lDiag = new bool[squares*2 - 1];
        rDiag = new bool[squares*2 -1];
        Nqueens = 0;

        for(int i = 0; i < squares; i++)

        {
            locationRow[i] = -1;
            column[i] = canGo;
        };

        for(int i = 0; i < squares*2 -1; i++)
            lDiag[i] = rDiag[i] = canGo;
    };


    void hereQueen(int row)
    {
        char respond;
        for(int col = 0; col < squares; col++)
            if(column[col] == canGo &&
                lDiag[row+col] == canGo &&
                rDiag[row-col+norm] == canGo)
            {
                locationRow[row] = col;
                column[col] = !canGo;
                lDiag[row+col] = !canGo;
                rDiag[row-col+norm] = !canGo;
                if(row < squares - 1)
                    hereQueen(row+1);
                else 
                {
                    printSolution(); 
                };

                column[col] = canGo;
                lDiag[row+col] = canGo;
                rDiag[row-col+norm] = canGo;
            };

    };

void printSolution() //int q[])
{
    Nqueens++;
    cout << "Solution for the " << squares << " Queens problem" << endl;
    for(int i = 0; i < squares; i++) 
    {
        for(int j = 0; j < squares; j++)
            if(locationRow[i] == j)
                cout << "Q";
            else
                cout << "*";
        cout << endl;
    };

};
};

void main()
{
    int boardSize;
    cout << "Starting Queens Problem..." << endl;
    cout << "Enter the board size (2-12) -->";
    cin >> boardSize;
    Queens queens(boardSize);

    queens.hereQueen(0);
    cout << "Number of solution is" << queens.Nqueens << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.