This is (supposed to be) a soln for a ACM problem

But the program produces a runtime error.
Can anyone spot the bug?

#include<iostream>
#include<string>

#define MAX 25

using namespace std;

void print_blocks();
int return_initial(int a);//return all the blocks 
						//on top of block a to their initial pos

int track[MAX];//in which stack a block is currently stacked
int blkpos[MAX][MAX];//block stacks
int n;

int main()
{
	//Initialize Start
	cin>>n;
	for(int i = 0; i < n; ++i)
		for(int j = 0; j < n; ++j)
			blkpos[i][j] = -1;
			
	for(int i = 0; i < n; ++i)
		blkpos[i][0] = i;
		
	for(int i = 0; i < n; ++i)
		track[i] = i;
	//Initialize End
	
	string command;
	int a,b;
	while(cin>>command)			
	{
		
		if(command == "quit")
			break;
			
		if(command == "move")
		{
			
			cin>>a>>command>>b;
						
			if(track[a] == track[b])//Illegal command
				continue;
			if(command == "onto")//command == move a onto b
			{
				int pos = return_initial(a);
				blkpos[track[b]][ return_initial(b) + 1] = a;
				blkpos[track[a]][pos] = -1;
				track[a] = track[b];				
				
			}
			else//command == move a over b
			{
				
				int pos = return_initial(a);
				int i;
				for( i = 0; blkpos[track[b]][i] != -1; ++i)
					;
				blkpos[track[b]][i] = a;
				blkpos[track[a]][pos] = -1;
				track[a] = track[b];				
			}
		}
		else //command == pile
		{
			cin>>a>>command>>b;
			if(track[a] == track[b])
				continue;
			if(command == "onto") // command == pile a onto b
			{
				int pos = return_initial(b);
				int i;
				for( i = 0; blkpos[track[a]][i] != a; ++i)
					;
				int from = track[a];
				for( ; blkpos[from][i] != -1; ++i)
				{
					blkpos[track[b]][++pos] = blkpos[from][i];
					track[blkpos[from][i]] = track[b];
					blkpos[from][i] = -1;					
				}
			}
			else //command == pile a over b
			{
				int pos;
				int i;
				for(i = 0; blkpos[track[b]][i] != b; ++i)
					;
				pos = i;				
				for( i = 0; blkpos[track[a]][i] != a; ++i)
					;
				int from = track[a];
				for( ; blkpos[from][i] != -1; ++i)
				{
					blkpos[track[b]][++pos] = blkpos[from][i];
					track[blkpos[from][i]] = track[b];
					blkpos[from][i] = -1;					
				}
			}			
			
		}
		
			
	}
	
	print_blocks();
}


int return_initial(int a)
{
	
	int i;
	for(i = 0; blkpos[ track[a]][i] != a; ++i )
		;
	int temp;
	int pos = i;
		
	for(++i ; (temp = blkpos[ track[a]][i]) != -1; ++i )
	{
		blkpos[ track[a]][i] = -1;
		int j;
		for( j = 0; blkpos[temp][j] != -1; ++j)
			;
		blkpos[temp][j] = temp;
		track[temp] = temp;		
		
	}
	return pos;
	
}


void print_blocks()
{
	
	for(int i = 0; i < n; ++i)
	{
			cout<<i<<":";
			for(int j = 0; blkpos[i][j] != -1; ++j)
			{
				cout<<" "<<blkpos[i][j];
			}
			cout<<"\n";
	}
}

Have you tried running it in a debugger?
If you do a stack dump (backtrace)
it will show exactly where it bombed.

I havent used a debugger, but I think it produces runtime error depending on the input. For some input the online judge uses it produces the error, not for the input i gave to it.

Hi Asif, I don't know if you still need this, but I think the cause of runtime error is the size of MAX. What if the given input for 'n' is greater than 25??? Then you'll get a Run-time Error. (but i'm not sure, obviously if it is mentioned in problem that the input will be less than 25 than this is NOT the cause of the problem)

Be a part of the DaniWeb community

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