Sign in to follow this  

Help with debugging

This topic is 3343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay I just downloaded a compiler off of a CD that came with a book, and wanted to make a quick program. The goal was to have a player class used by a board class which would allow me to move the player(s) around. The main method set up a basic looping that could later be used in a simple text based game. I have gotten many errors and frankly after an hour of pondering do not know what to do. Forgive me for any glaring mistakes my only experience is with Visual Basic and Java in school for two semesters. so here is the code:

#include <iostream>
#include <string>


using namespace std;

class bombBoard{
            
    public: 
        bombBoard(int numrows, int numcols, char letter){
              rows=numrows;
              cols=numcols;
              symbol=letter;
              numPlayers=0;
              for(int r=0;r<numrows;r++)
                  for(int c=0;c<numcols;c++)
                      board[r][c]=letter;
        }
        
        void printBoard(){
            for(int r=0;r<rows;r++){
                cout<<"\n";
                for(int c=0;c<cols;c++)
                    cout<<board[r][c];
            }
            cout<<"\n\n";
            return;
        }
        
        //moves a player within the board
        bool movePlayer(int pnum, int row, int col){
            if(board[row][col]!=symbol||players[pnum-1]==null){
                cout<<"Not a valid move choice"<<endl;
                return false;
            }
            board[players[pnum-1].getRow()][players[pnum-1].getCol()]=symbol;
            players[pnum-1].moveTo(row, col);
            board[row][col]=players[pnum-1].getSymbol();
            printBoard();
            return true;
        }  
        
        //adds a player to the board
        void addPlayer(player playa, int playanum){
            players[playanum-1]=playa;
            numPlayers++;
            board[playa.getRow()][playa.getCol()]=playa.getSymbol();
            return;
        }               
        int getRows(){return rows;}
        int getCols(){return cols;}
        char getSymbol(){return symbol;}
        int getNumPlayers(){return numPlayers;}
        player getPlayer(int playernum){return players[playernum-1];}
    
    protected:
        char board[50][50];
        int rows;
        int cols;
        int numPlayers;
        player players[10];
        char symbol;
        
};



class player{

    public:
        player(char symbol2, string title, int startrow, int startcol){
            symbol=symbol2;
            row=startrow;
            col=startcol;
            name=title;
        }
        
        int getRow(){return row;}
        int getCol(){return col;}
        char getSymbol(){return symbol;}
        string getName(){return name;}
        
        void moveTo(int newrow, int newcol){
            row=newrow;
            col=newcol;
        }
        
    protected:
        char symbol;
        int row;
        int col;
        string name;
        
};



int main(int nNumberofArgs, char* pszArgs[]){
    int numRows, numCols, numPlayers, currentNumPlayer=1;
    char symbol;
    
    cout<<"Enter the number of rows:"<<endl;
    cin>>numRows;
    cout<<"Enter the number of columns:"<<endl;
    cin>>numCols;
    cout<<"Enter the default symbol:"<<endl;
    cin>>symbol;
    cout<<"Enter the number of players:"<<endl;
    cin>>numPlayers;
    bombBoard myBoard(numRows, numCols, symbol)
    
    while(1){
        if(currentNumPlayer>=5){
            cout<<"Maximum player number reached."<<endl;
            break;
        }
        int startRow, startCol, val;
        char symb;
        string name;
        cout<<"Enter 0 to exit, any other number to continue:"<<endl;
        cin>>val;
        if(val==0)
            break;
        cout<<"Enter the name of the player:"<<endl;
        cin name;
        cout<<"Enter the starting row of player "<<currentNumPlayer<<":"<<endl;
        cin>>startRow;
        cout<<"Enter the starting column of player "<<currentNumPlayer<<":"<<endl;
        cin>>startCol;
        cout<<"Enter a 1 key symbol for player "<<currentNumPlayer<":"<<endl;
        myBoard.addPlayer(player johnDoe(symb, name, startRow, startCol), currentNumPlayer);
        currentNumPlayer++;
    }
    
    int currPlayerNum=1;
    
    while(1){
        int newRow, newCol, val;
        cout<<"Enter 0 to exit, any other number to continue:"<<endl;
        cin>>val;
        if(val==0)
            break;
        cout<<"Player "<<currPlayerNum<<" enter a new row:"<<endl;
        cin>>newRow;
        cout<<"Player "<<currPlayerNum<<" enter a new column:"<<endl;
        cin>>newCol;
        if(!myBoard.movePlayer(currPlayerNum, newRow, newCol)
            continue;
        if(currPlayerNum!=myBoard.playerNum())
            currPlayerNum+=1;
        else
            currPlayerNum=1;
    }
    
    system("PAUSE");
    return 0;
    
}



Share this post


Link to post
Share on other sites
Here are some of the statements I recieved from the compiler:

44 C:\Dev-Cpp\SouceFileBombGame.cpp
`player' was not declared in this scope

44 C:\Dev-Cpp\SouceFileBombGame.cpp
parse error before `,' token

54 C:\Dev-Cpp\SouceFileBombGame.cpp
parse error before `)' token

56 C:\Dev-Cpp\SouceFileBombGame.cpp
semicolon missing after declaration of `

/Dev-Cpp/SouceFileBombGame.cpp C:\Dev-Cpp\C
In constructor `bombBoard::bombBoard(int,

11 C:\Dev-Cpp\SouceFileBombGame.cpp
`rows' undeclared (first use this

11 C:\Dev-Cpp\SouceFileBombGame.cpp
(Each undeclared identifier is reported

..... and it goes on tenfold, but with most of the errors looking pretty similar to these first few

Share this post


Link to post
Share on other sites
0)First, put your code in [source][/source] tags.

1)I don't know how it's done in Java or Visual Basic, but in C++ you can't use a class until it's been declared. You use player in bombBoard, but the compiler hasn't seen player yet so it doesn't know what to do when it encounters player. So first of all, move the player class above bombBoard.

2)A second common misconception you have is that you can assign to a member variable in the class declaration. You can only do this when the member variables are declared as 'static const'. That is, every class you instantiate will have the same values for all of those variables. It doesn't look like you want this. Instead you'll need to initialize all of the member variables in the constructor. Which you already do. Score.

3)'null' is not a keyword in c++. There is the NULL macro, however.

4)At one point you do this:

myBoard.addPlayer(player johnDoe(symb, name, startRow, startCol), currentNumPlayer);

it looks like you want to create a temporary player to pass into the member function addPlayer. There's a way to do what you want directly but then I'd need to explain some things that would just confuse you. In short, just do this:

player johnDoe(symb, name, startRow, startCOl);
myBoard.addPlayer(johnDoe, currentNumPlayer);

5)At one point you create an array of players in a class. However, you don't provide a default constructor for player after defining your own constructor. Basically, the compiler doesn't know how to make an array of players unless you add this constructor to your player class:

player() { /* initialize here */ }

6)After fixing all that and a few other smaller errors you wind up with this:
#include <iostream>
#include <string>

using namespace std;

class player{

public:
player(char symbol2, string title, int startrow, int startcol){
symbol=symbol2;
row=startrow;
col=startcol;
name=title;
}
player()
{
symbol = 0;
row = 0;
col = 0;
name = "";
}

int getRow(){return row;}
int getCol(){return col;}
char getSymbol(){return symbol;}
string getName(){return name;}

void moveTo(int newrow, int newcol){
row=newrow;
col=newcol;
}
protected:
char symbol;
int row;
int col;
string name;

};

class bombBoard{
public:
bombBoard(int numrows, int numcols, char letter){
rows=numrows;
cols=numcols;
symbol=letter;
numPlayers=0;
for(int r=0;r<numrows;r++)
for(int c=0;c<numcols;c++)
board[r][c]=letter;
}
void printBoard(){
for(int r=0;r<rows;r++){
cout<<"\n";
for(int c=0;c<cols;c++)
cout<<board[r][c];
}
cout<<"\n\n";
return;
}
//moves a player within the board
bool movePlayer(int pnum, int row, int col){
//if(board[row][col] != symbol || players[pnum-1] == NULL){
// cout<<"Not a valid move choice"<<endl;
// return false;
//}
board[players[pnum-1].getRow()][players[pnum-1].getCol()]=symbol;
players[pnum-1].moveTo(row, col);
board[row][col]=players[pnum-1].getSymbol();
printBoard();
return true;
}
//adds a player to the board
void addPlayer(player playa, int playanum){
players[playanum-1]=playa;
numPlayers++;
board[playa.getRow()][playa.getCol()]=playa.getSymbol();
return;
}
int getRows(){return rows;}
int getCols(){return cols;}
char getSymbol(){return symbol;}
int getNumPlayers(){return numPlayers;}
player getPlayer(int playernum){return players[playernum-1];}
protected:
char board[50][50];
int rows;
int cols;
int numPlayers;
player players[10];
char symbol;

};

int main(int nNumberofArgs, char* pszArgs[]){
int numRows, numCols, numPlayers, currentNumPlayer=1;
char symbol;

cout<<"Enter the number of rows:"<<endl;
cin>>numRows;
cout<<"Enter the number of columns:"<<endl;
cin>>numCols;
cout<<"Enter the default symbol:"<<endl;
cin>>symbol;
cout<<"Enter the number of players:"<<endl;
cin>>numPlayers;
bombBoard myBoard(numRows, numCols, symbol);
while(1){
if(currentNumPlayer>=5){
cout<<"Maximum player number reached."<<endl;
break;
}
int startRow, startCol, val;
char symb;
string name;
cout<<"Enter 0 to exit, any other number to continue:"<<endl;
cin>>val;
if(val==0)
break;
cout<<"Enter the name of the player:"<<endl;
cin >> name;
cout<<"Enter the starting row of player "<<currentNumPlayer<<":"<<endl;
cin>>startRow;
cout<<"Enter the starting column of player "<<currentNumPlayer<<":"<<endl;
cin>>startCol;
cout << "Enter a 1 key symbol for player " << currentNumPlayer << ":" << endl;
player johnDoe(symb, name, startRow, startCol);
myBoard.addPlayer(johnDoe, currentNumPlayer);
currentNumPlayer++;
}

int currPlayerNum=1;

while(1){
int newRow, newCol, val;
cout<<"Enter 0 to exit, any other number to continue:"<<endl;
cin>>val;
if(val==0)
break;
cout<<"Player "<<currPlayerNum<<" enter a new row:"<<endl;
cin>>newRow;
cout<<"Player "<<currPlayerNum<<" enter a new column:"<<endl;
cin>>newCol;
if(!myBoard.movePlayer(currPlayerNum, newRow, newCol))
continue;
if(currPlayerNum!=myBoard.playerNum())
currPlayerNum+=1;
else
currPlayerNum=1;
}

system("PAUSE");
return 0;
}

7)There is still an error, on this line:

if(currPlayerNum!=myBoard.playerNum())

because no member function playerNum exists. I don't know what you want it to do, exactly, so I left it as is.

8)In case I wasn't clear in (7) that code does not compile. Because it did not compile your code will probably crash as I saw a lot of questionable tactics, like accessing an array like this:

data[index-1];

That could blow up badly.

9)A word of advice. Don't write all of the code and then try to compile it. Write small bits and compile often. If you had done this you wouldn't have had so many errors

10)Unless you have a compelling reason otherwise (and I doubt you do), ditch DevC++ and get the free Visual C++ Express Edition or the free Code::Blocks.

11)It is quite likely that your program will have errors, so here's a handy tutorial on debugging! Clicky.

Share this post


Link to post
Share on other sites

This topic is 3343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this