<solved>C++ problem system("pause") needed to prevent crash?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Sir_Tripalot3
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Thu Mar 04, 2010 12:51 pm

<solved>C++ problem system("pause") needed to prevent crash?

Post by Sir_Tripalot3 »

I am using a Dev-C++ compiler and my code is in C++.

Problem: my program crashes when system("pause"); or system("color 0"); is removed from code
on the second time this code is run.

Program background: I am attempting to write a chess program with a computer player. This problem occurs both in my 2 player version and my version with a computer player. I not using a GUI, instead I am using console. The locats variable is an array of 64 square objects that contains 64 pointers to piece objects. These piece pointers either point to NULL or a subclass object of a piece object (pawn, knight, bishop, queen, or king). square.get_occupant returns address of the occupying piece, and prints an error message when returning NULL. No error message is printed. The overloaded move(char[][], int []) stores the moves a piece can make in the char [][]. int board[] has 64 ints each corresponding to a location on the board. The value stored shows which player occupies the location or 0 if the square is unoccupied.

classes

Code: Select all

class square
{
          piece *occupant;
                 ...
          piece *get_occupant()
          {	
                  if (occupant==NULL)
                  {
                           cout<<"Error, returning NULL in function get_occupant for square "<<column<<row<<"\n";
                           getchar();
                   }
                  return occupant;
          }
};

class piece
{};
other classes inherit from piece class and are referred to by the occupant pointer in the square objects

CODE

Code: Select all

char moves[256][10];
for (int count=0; count<256; count++)
    moves[count][0]=0;
system ("pause");//crashes without this line of code or system("color 0");
for (int x=0; x<64; x++)
{
	if (board[x]==turn)
	{
//	   cout<<x;//program crashes when x==8 on first time the entire loop is run if this line of code
//         is used, if not used, program crashes that the 2nd time entire loop is run
           if (locats[x]->get_occupant()!=NULL)
         	locats[x]->get_occupant()->move(moves, board);//crashes here
   	   else
   	        cout<<"Error in getting moves\n";
        }
}
Debugging: This is slowing my program down when I run this code too many times. Using cout<<x without commenting it out causes the program to crash when x==8 the first time through this code. The line printf("%d", x); on the line that the cout<<x; is shown causes some of the values that should be stored in moves[][] to not be stored, but does not crash. The moves[][] that should be stored are stored when printf is removed. Replacing the system("pause") with getchar() or other imput functions did not solve the problem.

I would have posted more code, but I don't know what could be the problem, and this post is too
long as it is.
Last edited by Sir_Tripalot3 on Sat Mar 06, 2010 5:22 am, edited 4 times in total.
dephbokks
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Wed Mar 03, 2010 11:27 am

Re: C++ problem system("pause") needed to prevent crash?

Post by dephbokks »

1. System pause sucks.
2. Dev-C++ sucks. It is not a compiler, but rather an IDE. It uses a Mingw port of GCC (GNU Compiler Collection) as it's compiler, but the one it ships with is outdated. Most users do not upgrade. DevC++ is old, outdated, and no longer maintained. Ditch DevC++ and use Code Blocks if you hate MS, otherwise VC++ is the best. I use both Codeblocks & MSVC++. The MSVC++ debugger is sublime too.
3. Use code tags when posting code. It cuts down on the headaches for potential responders.
4. I'd put bound checking on all your array accessing - maybe you are out of bounds.
5. You say it crashes on the move function, yet you do not provide this function. Put it in code tags; it will not take up a lot of space since code tag blocks have a scroll bar on 'em.
Sir_Tripalot3
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Thu Mar 04, 2010 12:51 pm

Re: C++ problem system("pause") needed to prevent crash?

Post by Sir_Tripalot3 »

I realize that system("pause"); is bad coding. I found the error when I tried to remove the system commands from my code.

move is a virtual function in my piece class. My piece class probably should be an abstract class, but I needed to use piece addresses to reference subclass objects.

Code: Select all

 void piece::move (char moves[][10], int board[]){}
The move function used should be refering to rook, knight, bishop, queen, king and pawn objects. These are subclasses of the piece class
When I used cout<<x; the program crashed when x==8, locats[8]->get_occupant->move(moves, board); would be accessing the pawn move function if it was working correctly.

Code: Select all

void pawn::move(char moves[][10], int board[])
{
	int x;
	have_moved=0;
	for (int x=0; x<256&&moves[0][x]!='\0'; x++){}
	if (player==1)
	{
       if (row==2&&board[columnrowtolocation(column, row+1)]==0&&board[columnrowtolocation(column, row+2)]==0)
       {
          moves[x][0]=column;
          moves[x][1]=dtoa(row);
          moves[x][2]=column;
          moves[x][3]=dtoa(row+2);
          moves[x++][4]='\0';
		}
		if (row+1<9&&board[columnrowtolocation(column, row+1)]==0)
		{
            moves[x][0]=column;
		    moves[x][1]=dtoa(row);
			moves[x][2]=column;
			moves[x][3]=dtoa(row+1);
			moves[x++][4]='\0';
		}
		if (column-1>='a'&&row+1<8&&board[columnrowtolocation(column-1, row+1)]!=0&&board[columnrowtolocation(column-1, row+1)]!=player)
		{
            moves[x][0]=column;
		    moves[x][1]=dtoa(row);
			moves[x][2]=column;
			moves[x][3]='x';
			moves[x][4]=column-1;
			moves[x][5]=dtoa(row+1);
			moves[x++][6]='\0';
		}
		if (column+1<'i'&&row+1<8&&board[columnrowtolocation(column+1, row+1)]!=0&&board[columnrowtolocation(column+1, row+1)]!=player)
		{
            moves[x][0]=column;
		    moves[x][1]=dtoa(row);
			moves[x][2]=column;
			moves[x][3]='x';
			moves[x][4]=column+1;
			moves[x][5]=dtoa(row+1);
			moves[x++][6]='\0';
		}
		if (row==5&&column+1<'i'&&board[columnrowtolocation(column+1, row)]==2&&location->get_plus1()!=NULL&&location->get_plus1()->get_occupant()!=NULL&&location->get_plus1()->get_occupant()->get_type()=='p'&&board[columnrowtolocation(column+1, row+1)]==0&&location->get_plus1()->get_occupant()->get_have_moved())
		{
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]=column;
            moves[x][3]='x';
            moves[x][4]=column+1;
            moves[x++][5]=dtoa(row+1);
        }
        if (row==5&&column-1>='a'&&board[columnrowtolocation(column-1, row)]==2&&location->get_minus1()!=NULL&&location->get_minus1()->get_occupant()!=NULL&&location->get_minus1()->get_occupant()->get_type()=='p'&&board[columnrowtolocation(column-1, row+1)]==0&&location->get_minus1()->get_occupant()->get_have_moved())
		{
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]=column;
            moves[x][3]='x';
            moves[x][4]=column-1;
            moves[x++][5]=dtoa(row+1);
        }
        if (row+1==8&&column+1<'i'&&board[columnrowtolocation(column+1, row+1)]==2)
        {
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]='P';
            moves[x][3]='R';
            moves[x][4]='x';
            moves[x][5]=column+1;
            moves[x][6]=dtoa(row+1);
            moves[x++][7]='\0';
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]='P';
            moves[x][3]='N';
            moves[x][4]='x';
            moves[x][4]=column+1;
            moves[x][5]=dtoa(row+1);
            moves[x++][6]='\0';
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]='P';
            moves[x][3]='B';
            moves[x][4]='x';
            moves[x][5]=column+1;
            moves[x][6]=dtoa(row+1);
            moves[x++][7]='\0';
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]='P';
            moves[x][3]='Q';
            moves[x][4]='x';
            moves[x][5]=column+1;
            moves[x][6]=dtoa(row+1);
            moves[x++][7]='\0';
		}
        if (row+1==8&&column-1>='a'&&board[columnrowtolocation(column-1, row+1)]==2)
        {
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]='P';
            moves[x][3]='R';
            moves[x][4]='x';
            moves[x][5]=column-1;
            moves[x][6]=dtoa(row+1);
            moves[x++][7]='\0';
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]='P';
            moves[x][3]='N';
            moves[x][4]='x';
            moves[x][4]=column-1;
            moves[x][5]=dtoa(row+1);
            moves[x++][6]='\0';
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]='P';
            moves[x][3]='B';
            moves[x][4]='x';
            moves[x][5]=column-1;
            moves[x][6]=dtoa(row+1);
            moves[x++][7]='\0';
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]='P';
            moves[x][3]='Q';
            moves[x][4]='x';
            moves[x][5]=column-1;
            moves[x][6]=dtoa(row+1);
            moves[x++][7]='\0';
		}
        if (row+1==8&&board[columnrowtolocation(column, row+1)]==0)
        {
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]='P';
            moves[x][3]='R';
            moves[x][4]=column;
            moves[x][5]=dtoa(row+1);
            moves[x++][6]='\0';
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]='P';
            moves[x][3]='N';
            moves[x][4]=column;
            moves[x][5]=dtoa(row+1);
            moves[x++][6]='\0';
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]='P';
            moves[x][3]='B';
            moves[x][4]=column;
            moves[x][5]=dtoa(row+1);
            moves[x++][6]='\0';
            moves[x][0]=column;
            moves[x][1]=dtoa(row);
            moves[x][2]='P';
            moves[x][3]='Q';
            moves[x][4]=column;
            moves[x][5]=dtoa(row+1);
            moves[x++][6]='\0';
		}
		}
		else if (player==2)
		{
			if (row==7&&board[columnrowtolocation(column, row-1)]==0&&board[columnrowtolocation(column, row-2)]==0)
			{
                moves[x][0]=column;
			    moves[x][1]=dtoa(row);
				moves[x][2]=column;
				moves[x][3]=dtoa(row-2);
				moves[x++][4]='\0';
			}
			if (row-1>1&&board[columnrowtolocation(column, row-1)]==0)
			{
                moves[x][0]=column;
			    moves[x][1]=dtoa(row);
				moves[x][2]=column;
				moves[x][3]=dtoa(row-1);
				moves[x++][4]='\0';
		    }
			if (row-1>1&&column-1>='a'&&row-1>0&&board[columnrowtolocation(column-1, row-1)]!=0&&board[columnrowtolocation(column-1, row-1)]!=player)
			{
                moves[x][0]=column;
		        moves[x][1]=dtoa(row);
				moves[x][2]=column;
				moves[x][3]='x';
				moves[x][4]=column-1;
				moves[x][5]=dtoa(row-1);
				moves[x++][6]='\0';
			}
			if (column+1<'i'&&row-1>1&&board[columnrowtolocation(column+1, row-1)]!=0&&board[columnrowtolocation(column+1, row-1)]!=player)
			{
                moves[x][0]=column;
			    moves[x][1]=dtoa(row);
				moves[x][2]=column;
				moves[x][3]='x';
				moves[x][4]=column+1;
				moves[x][5]=dtoa(row-1);
				moves[x++][6]='\0';
			}
			if (row==4&&column+1<'i'&&board[columnrowtolocation(column+1, row)]==1&&location->get_plus1()!=NULL&&location->get_plus1()->get_occupant()!=NULL&&location->get_plus1()->get_occupant()->get_type()=='p'&&board[columnrowtolocation(column+1, row-1)]==0&&location->get_plus1()->get_occupant()->get_have_moved())
			{
                moves[x][0]=column;
                moves[x][1]=dtoa(row);
                moves[x][2]=column;
                moves[x][3]='x';
                moves[x][4]=column+1;
                moves[x++][5]=dtoa(row-1);
            }
            if (row==4&&column-1>='a'&&board[columnrowtolocation(column+1, row)]==1&&location->get_plus1()!=NULL&&location->get_plus1()->get_occupant()!=NULL&&location->get_plus1()->get_occupant()->get_type()=='p'&&board[columnrowtolocation(column+1, row-1)]==0&&location->get_plus1()->get_occupant()->get_have_moved())
			{
                moves[x][0]=column;
                moves[x][1]=dtoa(row);
                moves[x][2]=column;
                moves[x][3]='x';
                moves[x][4]=column+1;
                moves[x++][5]=dtoa(row-1);
            }
            if (row-1==1&&board[columnrowtolocation(column, row-1)]==0)
            {
                moves[x][0]=column;
                moves[x][1]=dtoa(row);
                moves[x][2]='P';
                moves[x][3]='R';
                moves[x][4]=column;
                moves[x][5]=dtoa(row-1);
                moves[x++][6]='\0';
                moves[x][0]=column;
                moves[x][1]=dtoa(row);
                moves[x][2]='P';
                moves[x][3]='N';
                moves[x][4]=column;
                moves[x][5]=dtoa(row-1);
                moves[x++][6]='\0';
                moves[x][0]=column;
                moves[x][1]=dtoa(row);
                moves[x][2]='P';
                moves[x][3]='B';
                moves[x][4]=column;
                moves[x][5]=dtoa(row-1);
                moves[x++][6]='\0';
                moves[x][0]=column;
                moves[x][1]=dtoa(row);
                moves[x][2]='P';
                moves[x][3]='Q';
                moves[x][4]=column;
                moves[x][5]=dtoa(row-1);
                moves[x++][6]='\0';
			}
			if (row-1==1&&column+1<'i'&&board[columnrowtolocation(column+1, row-1)]==1)
            {
                moves[x][0]=column;
                moves[x][1]=dtoa(row);
                   moves[x][2]='P';
                   moves[x][3]='R';
                   moves[x][4]='x';
                   moves[x][5]=column+1;
                   moves[x][6]=dtoa(row-1);
                   moves[x++][7]='\0';
                   moves[x][0]=column;
                   moves[x][1]=dtoa(row);
                   moves[x][2]='P';
                   moves[x][3]='N';
                   moves[x][4]='x';
                   moves[x][4]=column+1;
                   moves[x][5]=dtoa(row-1);
                   moves[x++][6]='\0';
                   moves[x][0]=column;
                   moves[x][1]=dtoa(row);
                   moves[x][2]='P';
                   moves[x][3]='B';
                   moves[x][4]='x';
                   moves[x][5]=column+1;
                   moves[x][6]=dtoa(row-1);
                   moves[x++][7]='\0';
                   moves[x][0]=column;
                   moves[x][1]=dtoa(row);
                   moves[x][2]='P';
                   moves[x][3]='Q';
                   moves[x][4]='x';
                   moves[x][5]=column+1;
                   moves[x][6]=dtoa(row-1);
                   moves[x++][7]='\0';
			       }
            if (row-1==1&&column-1>='a'&&board[columnrowtolocation(column-1, row-1)]==1)
            {
                   moves[x][0]=column;
                   moves[x][1]=dtoa(row);
                   moves[x][2]='P';
                   moves[x][3]='R';
                   moves[x][4]='x';
                   moves[x][5]=column-1;
                   moves[x][6]=dtoa(row-1);
                   moves[x++][7]='\0';
                   moves[x][0]=column;
                   moves[x][1]=dtoa(row);
                   moves[x][2]='P';
                   moves[x][3]='N';
                   moves[x][4]='x';
                   moves[x][4]=column-1;
                   moves[x][5]=dtoa(row-1);
                   moves[x++][6]='\0';
                   moves[x][0]=column;
                   moves[x][1]=dtoa(row);
                   moves[x][2]='P';
                   moves[x][3]='B';
                   moves[x][4]='x';
                   moves[x][5]=column-1;
                   moves[x][6]=dtoa(row-1);
                   moves[x++][7]='\0';
                   moves[x][0]=column;
                   moves[x][1]=dtoa(row);
                   moves[x][2]='P';
                   moves[x][3]='Q';
                   moves[x][4]='x';
                   moves[x][5]=column-1;
                   moves[x][6]=dtoa(row-1);
                   moves[x++][7]='\0';
            }
        }
}
my other move functions that it could be referencing are

Code: Select all

void king::move(char moves[][10], int board[])
{
	if (have_moved)
	{
        castlequeen=0;
        castleking=0;
    }
    int y;
    const int z=1;
	for (int y=0; y<256&&moves[y][0]!=0; y++){}
	if (column+z<'i'&&board[columnrowtolocation(column+z, row)]!=player)
	{
	   int x=0;
       moves[y][x++]=column;
	   moves[y][x++]=dtoa(row);
	   moves[y][x++]='K';
	   if (board[columnrowtolocation(column+z, row)]!=0)
	      	moves[y][x++]='x';
	   moves[y][x++]=column+z;
	   moves[y][x++]=dtoa(row);
	   moves[y++][x]=0;
	}
	if (column-z>='a'&&board[columnrowtolocation(column-z, row)]!=player)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='K';
		if (board[columnrowtolocation(column-z, row)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column-z;
		moves[y][x++]=dtoa(row);
		moves[y++][x]=0;
	}
	if (row+z<9&&board[columnrowtolocation(column, row+z)]!=player)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='K';
		if (board[columnrowtolocation(column, row+z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row+z);
		moves[y++][x]=0;
	}
	if (row-z>0&&board[columnrowtolocation(column, row-z)]!=player)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='K';
		if (board[columnrowtolocation(column, row-z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row-z);
		moves[y++][x]=0;
	}
	if(column+z<'i'&&row+z<9&&board[columnrowtolocation(column+z, row+z)]!=player)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='K';
		if (board[columnrowtolocation(column+z, row+z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column+z;
		moves[y][x++]=dtoa(row+z);
		moves[y++][x]=0;
	}
	if(column-z>='a'&&row-z>0&&board[columnrowtolocation(column-z, row-z)]!=player)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='K';
		if (board[columnrowtolocation(column-z, row-z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column-z;
		moves[y][x++]=dtoa(row-z);
		moves[y++][x]=0;
	}
	if(row+z<9&&column-z>='a'&&board[columnrowtolocation(column-z, row+z)]!=player)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='K';
		if (board[columnrowtolocation(column-z, row+z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column-z;
		moves[y][x++]=dtoa(row+z);
		moves[y++][x]=0;
	}
	if(row-z>0&&column+z<'i'&&board[columnrowtolocation(column+z, row-z)]!=player)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='K';
		if (board[columnrowtolocation(column+z, row-z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column+z;
		moves[y][x++]=dtoa(row-z);
		moves[y++][x]=0;
	}	
	if (castleking&&board[columnrowtolocation(column+1, row)]==0&&board[columnrowtolocation(column+2, row)]==0)
	     stringcopy(moves[y++], "O-O");
	if (castlequeen&&board[columnrowtolocation(column-1, row)]==0&&board[columnrowtolocation(column-2, row)]==0&&board[columnrowtolocation(column-3, row)]==0)
	     stringcopy(moves[y++], "O-O-O");
}
//*********************************************************************************************************
void queen::move(char moves[][10], int board[])
{
	int y;
	for (int y=0; y<256&&moves[y][0]!=0; y++){}
	for(int z=1;column+z<'i'&&row+z<9&&board[columnrowtolocation(column+z, row+z)]!=player; z++)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='Q';
		if (board[columnrowtolocation(column+z, row+z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column+z;
		moves[y][x++]=dtoa(row+z);
		moves[y++][x]=0;
		if (board[columnrowtolocation(column+z, row+z)]!=0)
			break;
	}
	for(int z=1; column-z>='a'&&row-z>0&&board[columnrowtolocation(column-z, row-z)]!=player;z++)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='Q';
		if (board[columnrowtolocation(column-z, row-z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column-z;
		moves[y][x++]=dtoa(row-z);
		moves[y++][x]=0;
		if (board[columnrowtolocation(column-z, row-z)]!=0)
			break;
	}
	for(int z=1; row+z<9&&column-z>='a'&&board[columnrowtolocation(column-z, row+z)]!=player;z++)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='Q';
		if (board[columnrowtolocation(column-z, row+z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column-z;
		moves[y][x++]=dtoa(row+z);
		moves[y++][x]=0;
		if (board[columnrowtolocation(column-z, row+z)]!=0)
			break;
	}
	for(int z=1; row-z>0&&column+z<'i'&&board[columnrowtolocation(column+z, row-z)]!=player;z++)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='Q';
		if (board[columnrowtolocation(column+z, row-z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column+z;
		moves[y][x++]=dtoa(row-z);
		moves[y++][x]=0;
		if (board[columnrowtolocation(column+z, row-z)]!=0)
			break;
	}
	for(int z=1;column+z<'i'&&board[columnrowtolocation(column+z, row)]!=player; z++)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='Q';
		if (board[columnrowtolocation(column+z, row)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column+z;
		moves[y][x++]=dtoa(row);
    	moves[y++][x]=0;
		if (board[columnrowtolocation(column+z, row)]!=0)
			break;
	}
	for(int z=1; column-z>='a'&&board[columnrowtolocation(column-z, row)]!=player;z++)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='Q';
		if (board[columnrowtolocation(column-z, row)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column-z;
		moves[y][x++]=dtoa(row);
		moves[y++][x]=0;
		if (board[columnrowtolocation(column-z, row)]!=0)
			break;
	}
	for(int z=1; row+z<9&&board[columnrowtolocation(column, row+z)]!=player;z++)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='Q';
		if (board[columnrowtolocation(column, row+z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row+z);
		moves[y++][x]=0;
		if (board[columnrowtolocation(column, row+z)]!=0)
			break;
	}
	for(int z=1; row-z>0&&board[columnrowtolocation(column, row-z)]!=player;z++)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='Q';
		if (board[columnrowtolocation(column, row-z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row-z);
		moves[y++][x]=0;
		if (board[columnrowtolocation(column, row-z)]!=0)
			break;
	}
}
//******************************************************************************************************************
void knight::move(char moves[][10], int board[])
{
	int y;
	for (y=0; y<256&&moves[y][0]!='\0'; y++){}
	if (row+1<9&&column+2<'i'&&board[columnrowtolocation(column+2, row+1)]!=player)
	{	
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='N';
		if (board [columnrowtolocation(column+2, row+1)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column+2;
		moves[y][x++]=dtoa(row+1);
		moves[y++][x]='\0';
		//cout << "\n"<<moves[y-1]<<"\n";
	}
	if (row+2<9&&column+1<'i'&&board[columnrowtolocation(column+1, row+2)]!=player)
	{	
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='N';
		if (board [columnrowtolocation(column+1, row+2)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column+1;
		moves[y][x++]=dtoa(row+2);
		moves[y++][x]='\0';
              //cout <<"\n"<< moves[y-1]<<"\n";	
	}
	if (row-2>0&&column+1<'i'&&board[columnrowtolocation(column+1, row-2)]!=player)
	{	
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='N';
		if (board [columnrowtolocation(column+1, row-2)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column+1;
		moves[y][x++]=dtoa(row-2);
		moves[y++][x]='\0';
	}
	if (row-1>0&&column+2<'i'&&board[columnrowtolocation(column+2, row-1)]!=player)
	{	
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='N';
		if (board [columnrowtolocation(column+2, row-1)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column+2;
		moves[y][x++]=dtoa(row-1);
		moves[y++][x]='\0';
	}
	if (row+1<9&&column-2>='a'&&board[columnrowtolocation(column-2, row+1)]!=player)
	{	
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='N';
		if (board [columnrowtolocation(column-2, row+1)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column-2;
		moves[y][x++]=dtoa(row+1);
		moves[y++][x]='\0';
	}
	if (row+2<9&&column-1>='a'&&board[columnrowtolocation(column-1, row+2)]!=player)
	{	
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='N';
		if (board [columnrowtolocation(column-1, row+2)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column-1;
		moves[y][x++]=dtoa(row+2);
		moves[y++][x]='\0';
		//cout <<"\n"<< moves[x-1]<<"\n";
	}
	if (row-2>0&&column-1>='a'&&board[columnrowtolocation(column-1, row-2)]!=player)
	{	
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='N';
		if (board [columnrowtolocation(column-1, row-2)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column-1;
		moves[y][x++]=dtoa(row-2);
		moves[y++][x]='\0';
	}
	if (row-1>0&&column-2>='a'&&board[columnrowtolocation(column-2, row-1)]!=player)
	{	
              //system ("pause");
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='N';
		if (board [columnrowtolocation(column-2, row-1)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column-2;
		moves[y][x++]=dtoa(row-1);
		moves[y++][x]='\0';
	}
}
//***************************************************************************************************************
void bishop::move(char moves[][10], int board[])
{
	int y;
	for (y=0; y<256&&moves[y][0]!=0; y++){}
	for(int z=1;column+z<'i'&&row+z<9&&board[columnrowtolocation(column+z, row+z)]!=player; z++)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='B';
		if (board[columnrowtolocation(column+z, row+z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column+z;
		moves[y][x++]=dtoa(row+z);
		moves[y++][x]=0;
		if (board[columnrowtolocation(column+z, row+z)]!=0)
			break;
	}
	for(int z=1; column-z>='a'&&row-z>0&&board[columnrowtolocation(column-z, row-z)]!=player;z++)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='B';
		if (board[columnrowtolocation(column-z, row-z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column-z;
		moves[y][x++]=dtoa(row-z);
		moves[y++][x]=0;
		if (board[columnrowtolocation(column-z, row-z)]!=0)
			break;
	}
	for(int z=1; row+z<9&&column-z>='a'&&board[columnrowtolocation(column-z, row+z)]!=player;z++)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='B';
		if (board[columnrowtolocation(column-z, row+z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column-z;
		moves[y][x++]=dtoa(row+z);
	    moves[y++][x]=0;
		if (board[columnrowtolocation(column-z, row+z)]!=0)
			break;
	}
	for(int z=1; row-z>0&&column+z<'i'&&board[columnrowtolocation(column+z, row-z)]!=player;z++)
	{
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='B';
		if (board[columnrowtolocation(column+z, row-z)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column+z;
		moves[y][x++]=dtoa(row-z);
		moves[y++][x]=0;
		if (board[columnrowtolocation(column+z, row-z)]!=0)
			break;
	}
}
//****************************************************************************************************
void rook::move(char moves[][10], int board[])
{
    int y;
    for (y=0; y<256&&moves[y][0]!=0; y++){}
    for(int z=1;column+z<'i'&&board[columnrowtolocation(column+z, row)]!=player; z++)
    {
		int x=0;
		moves[y][x++]=column;
		moves[y][x++]=dtoa(row);
		moves[y][x++]='R';
		if (board[columnrowtolocation(column+z, row)]!=0)
			moves[y][x++]='x';
		moves[y][x++]=column+z;
		moves[y][x++]=dtoa(row);
		moves[y++][x]=0;
		if (board[columnrowtolocation(column+z, row)]!=0)
			break;
    }
    for(int z=1; column-z>='a'&&board[columnrowtolocation(column-z, row)]!=player;z++)
    {
	int x=0;
	moves[y][x++]=column;
	moves[y][x++]=dtoa(row);
	moves[y][x++]='R';
	if (board[columnrowtolocation(column-z, row)]!=0)
		moves[y][x++]='x';
	moves[y][x++]=column-z;
	moves[y][x++]=dtoa(row);
	moves[y++][x]=0;
	if (board[columnrowtolocation(column-z, row)]!=0)
		break;
   }
   for(int z=1; row+z<9&&board[columnrowtolocation(column, row+z)]!=player;z++)
   {
	int x=0;
	moves[y][x++]=column;
	moves[y][x++]=dtoa(row);
                moves[y][x++]='R';
	if (board[columnrowtolocation(column, row+z)]!=0)
		moves[y][x++]='x';
	moves[y][x++]=column;
	moves[y][x++]=dtoa(row+z);
	moves[y++][x]=0;
	if (board[columnrowtolocation(column, row+z)]!=0)
		break;
   }
   for(int z=1; row-z>0&&board[columnrowtolocation(column, row-z)]!=player;z++)
   {
	int x=0;
	moves[y][x++]=column;
	moves[y][x++]=dtoa(row);
	moves[y][x++]='R';
	if (board[columnrowtolocation(column, row-z)]!=0)
		moves[y][x++]='x';
	moves[y][x++]=column;
	moves[y][x++]=dtoa(row-z);
	moves[y++][x]=0;
	if (board[columnrowtolocation(column, row-z)]!=0)
		break;
   }
}
I guess I don't understand exactly what system commands do. I thought that system("pause"); or system("color 0"); should not affect any of the data in my program. Is there anything that system commands set to default to make it more robust? Is there a quick debugging tool for bounds checking because I think I have checked most places where that could occur, and I don't know how cout, printf, or system functions could affect the index I use for an array. If there isn't a quick way to do this, where should I start because checking the values of my indexes before every time I use an index in the char moves[][] array will make a lot of if statements, and mathematically I shouldn't be out of bounds, and the program works perfectly when a system command is written every time before the for loop, for(int x=0;x<64; x++) shown in the previous post, is run.
Sir_Tripalot3
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Thu Mar 04, 2010 12:51 pm

Re: C++ problem system("pause") needed to prevent crash?

Post by Sir_Tripalot3 »

I think the program crashes when turn==2 and no system command is used, or if the cout statement is used in the loop.
dephbokks
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Wed Mar 03, 2010 11:27 am

Re: C++ problem system("pause") needed to prevent crash?

Post by dephbokks »

Ok. Your move function(s) have some issues I think.

When you write:

Code: Select all

   int x; // what is going on here? x is the index of your for loop! get rid of this statement.
   have_moved=0;
   for (int x=0; x<256&&moves[0][x]!='\0'; x++){ // this is what your for loop is executing: nothing! nothing in between these curly brackets!}
   if (player==1)
   {
       if (row==2&&board[columnrowtolocation(column, row+1)]==0&&board[columnrowtolocation(column, row+2)]==0)
       {

.... Then go on to a chain of if statements that access the 'x' variable which has gone out of scope from the for loop....
This code is not doing what I think you're trying to make it do.

Really, I am surprised the compiler doesn't complain about this. Do you mean to have all those 'if' statements inside to for loop and looped over for all x's? If so, then you need to write:

Code: Select all

for (int x=0; x<256&&moves[0][x]!='\0'; x++)
{
     if (player==1)
     {
          if (row==2&&board[columnrowtolocation(column, row+1)]==0&&board[columnrowtolocation(column, row+2)]==0)
           {

.... Then go on to a chain of if statements that access the 'x' variable
}
I just made some test code and both VC++ and mingw compiled it, but both had run time errors. MS's was more verbose telling me that x needed to be initialized:

Code: Select all

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;

    int x;

    int testArray[10];

    for ( int x = 0; x < 50; x++ ) {  }

    testArray[x] = 5;

    return 0;
}
The code above is the basic thing to illustrate your problem. You loop over x in the for loop doing nothing, then access some array using x, which leads to x could be anything and thus sometimes it is not in bounds.
Sir_Tripalot3
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Thu Mar 04, 2010 12:51 pm

Re: C++ problem system("pause") needed to prevent crash?

Post by Sir_Tripalot3 »

Thanks, that was my problem!! I was defining x twice.
dephbokks
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Wed Mar 03, 2010 11:27 am

Re: C++ (solved)problem system("pause") needed to prevent crash?

Post by dephbokks »

Just to be clear, view this small example:

Code: Select all

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;

    int xx = 0;

	int testArray[10] = { 0,1,2,3,4,5,6,7,8,9 };

    for ( int x = 0; (x < 5)&&(testArray[x]!=2); x++ ) 
	{
		printf("\ntestArray[%d] = %d", x, testArray[x]);
		xx = x+1;
	}

	printf("\n\nx = %d; testArray[%d] = %d", xx, xx, testArray[xx]);

	cin.get();

    return 0;
}
The variable 'x' in the for loop is local to that for loop; it goes out of scope when the loop is finished executing. Instead of what you did previously, I made another variable 'xx' that is declared outside the for loop. Its scope is valid for all of the main function. So when we loop thru the for loop, we set xx = x+1 when the condition in the for loop breaks, that is when testArray[x] == 2. The reason we add one to xx is that the for loop contents do not execute when the condition breaks, so we need to add one to it so that xx = 2 when testArray[x] = 2.

So it looks like you were getting into scope issues. But still, you're for loop, as you had it, was not doing anything since you had no code inside its curly brackets. If you would like to keep track of which x causes the break from the for loop, you will need to use an external variable, like xx, as I did whose scope is outside of the for loop.

I am just trying to be clear so that you are aware of these potential issues.
Sir_Tripalot3
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Thu Mar 04, 2010 12:51 pm

Re: C++ (solved)problem system("pause") needed to prevent crash?

Post by Sir_Tripalot3 »

I only wanted 1 variable x. I redefined x in the loop by mistake. . I don't understand why int xx is needed. Wouldn't

Code: Select all

int main (void)
{
   cout << "Hello world!" << endl;
    int x = 0;
   int testArray[10] = { 0,1,2,3,4,5,6,7,8,9 };
  for ( x = 0; (x < 5)&&(testArray[x]!=2); x++ )
   {
      printf("\ntestArray[%d] = %d", x, testArray[x]);
   }
  printf("\n\nx = %d; testArray[%d] = %d", x, x, testArray[x]);

   cin.get();

    return 0;
}
work exactly the same with one less variable?
I accidentally put x in the wrong index of char moves[][] in the code that I posted earlier. Does the code you recommended have to do with that mistake?
Thanks for the help!
dephbokks
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Wed Mar 03, 2010 11:27 am

Re: C++ (solved)problem system("pause") needed to prevent crash?

Post by dephbokks »

Yeah I think what you put last will work fine. My issue was when you put the code like this:

Code: Select all

int x; 

// some more code

for (int x=0; x<256&&moves[0][x]!='\0'; x++)
{
     // blah, blah, blah
}
When you re-declare 'int x' you run into problems, but as you have it like

Code: Select all

int x; 

// some more code

for (x=0; x<256&&moves[0][x]!='\0'; x++)
{
     // blah, blah, blah
}
you should be okay. It was just the re-declaring of x by saying 'int x' that I wanted to point out the problem with. Anyways sorry for any confusion I just wanted to make sure we were on the same page. So everything works according to specification now?
Sir_Tripalot3
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Thu Mar 04, 2010 12:51 pm

Re: C++ (solved)problem system("pause") needed to prevent crash?

Post by Sir_Tripalot3 »

That particular error is fixed. I still have some errors later in the program, but I wasn't able to debug earlier because the system functions that I was using took forever and its hard to find where an error is if it takes 10 minutes to reach it. I think I can figure it out now. Thanks for the help!!! Any ideas on why the system ("pause") or system("color 0") prevented the program from crashing earlier? Nothing should have been able to work, but my program was running fine as long as I used either of those functions.
Post Reply