Hi Guys !!
I'm new to this forum so please excuse me if i'm not posting my problem correctly.
I have been given a project to make a program in C++ that solves a given sudoku problem.
Here is what i did :

#include<iostream.h>
#include<conio.h>
int board[9][9];
int checkrow(int t, int _x, int _y)
{
   int c=1;
	for(int y=0; y<9 && c==1 ; y++)
   	if(board[_x][y]==t)
      	c=0;
   return c;
}
int checkcol(int t, int _x, int _y)
{
   int c=1;
	for(int x=0; x<9 && c==1 ; x++)
   	if(board[x][_y]==t)
      	c=0;
   return c;
}
int checkbox(int t, int _x, int _y)
{
   int c=1;
   int __x=(_x+1)%3, __y=(_y+1)%3;
   switch(__x)
   {
      case 0: switch(__y)
      		  {
              		case 0:{
                           for(int x=_x ; x>=_x-2 && c==1 ; x--)
                              for(int y=_y ; y>=_y-2 && c==1; y--)
                                 if(board[x][y]==t)
                                    c=0;
                  		 }
                  		 break;
                  case 1:{
                           for(int x=_x ; x>=_x-2 && c==1 ; x--)
                              for(int y=_y ; y<=_y+2 && c==1; y++)
                                 if(board[x][y]==t)
                                    c=0;
                  		 }
                  		 break;
                  case 2:{
                           for(int x=_x ; x>=_x-2 && c==1 ; x--)
                              for(int y=_y-1 ; y<=_y+2 && c==1; y++)
                                 if(board[x][y]==t)
                                    c=0;
                         }
      						 break;
              }
              break;
      case 1: switch(__y)
      		  {
              		case 0:{
                           for(int x=_x ; x<=_x+2 && c==1 ; x++)
                              for(int y=_y ; y>=_y-2 && c==1; y--)
                                 if(board[x][y]==t)
                                    c=0;
                         }
                         break;
                  case 1:{
                           for(int x=_x ; x<=_x+2 && c==1 ; x++)
                              for(int y=_y ; y<=_y+2 && c==1; y++)
                                 if(board[x][y]==t)
                                    c=0;
                         }
                         break;
                  case 2:{
                           for(int x=_x ; x<=_x+2 && c==1 ; x++)
                           	for(int y=_y-1 ; y<=_y+2 && c==1; y++)
                                 if(board[x][y]==t)
                                    c=0;
                         }
                         break;
              }
              break;
      case 2: switch(__y)
      	     {
              		case 0:{
                           for(int x=_x-1 ; x<=_x+2 && c==1 ; x++)
                              for(int y=_y ; y>=_y-2 && c==1; y--)
                                 if(board[x][y]==t)
                                    c=0;
                         }
                  	    break;
                  case 1:{
                  			for(int x=_x-1 ; x<=_x+2 && c==1 ; x++)
                              for(int y=_y ; y<=_y+2 && c==1; y++)
                                 if(board[x][y]==t)
                                    c=0;
                         }
                  		 break;
                  case 2:{
                  			for(int x=_x-1 ; x<=_x+2 && c==1 ; x++)
                              for(int y=_y-1 ; y<=_y+2 && c==1; y++)
                                 if(board[x][y]==t)
                                    c=0;
                  		 }
                  		 break;
              }
              break;
   }
   return c;
}
struct node
{
	int x, y;
   node* next;
};
class list
{
	node* top;
	public:
   	list(){top=NULL;}
      node* rettop(){return top;}
      void add(int , int);
};
void list::add(int x, int y)
{
	if(!top)
   {
   	top=new node;
      top->x=x;
      top->y=y;
      top->next=NULL;
   }
   else
   {
    	node* ptr=new node;
      ptr->x=x;
      ptr->y=y;
      ptr->next=top;
      top=ptr;
   }
}
void solve(node* point)
{
   if(point)
   {
      for(int t=1; t<10; t++)
      {
          if(checkrow(t, point->x, point->y))
          	if(checkcol(t, point->x, point->y))
          		if(checkbox(t, point->x, point->y))
               {

                  board[point->x][point->y]=t;
                  solve(point->next);
                  if(point->next)
                  {
                  	if(board[point->next->x][point->next->y]==0)
								board[point->x][point->y]=0;
                     else
                     	break;
                  }
                  else
                  	break;
              }
      }
   }
}
void main()
{
   list a;
   for(int x=0; x<9; x++)
     	cin>>board[x][0]>>board[x][1]>>board[x][2]
      	>>board[x][3]>>board[x][4]>>board[x][5]
         >>board[x][6]>>board[x][7]>>board[x][8];
	for(int x=8; x>=0; x--)
   	for(int y=8; y>=0; y--)
      	if(board[x][y]==0)
         	a.add(x, y);
   solve(a.rettop());
   for(int x=0; x<9; x++)
     	cout<<board[x][0]<<board[x][1]<<board[x][2]
      	 <<board[x][3]<<board[x][4]<<board[x][5]
          <<board[x][6]<<board[x][7]<<board[x][8]<<endl;
   getch();
}

Whenever I run the program , It gives back the original puzzle to me.
But what was interesting was that suppose u gave it a completely blank puzzle or a puzzle with 5-6 blanks then it came up with the correct solution.
I know I'm making some very careless error somewhere.
Please tell me if I'm doing something wrong.

NOTE: The program takes 0 for blanks and the input has to be row wise with the numbers seperated by spaces.

Thanks in advance

Did you (or can you) step through each step with the debugger?

Did you (or can you) step through each step with the debugger?

I'm sorry I did'nt quite understand what u mean to say...please excuse me !
How do I step through each step with the debugger?
Please excuse me if I'm being a complete noob.

Perhaps try something simple like a Message Box or console message after each line where there is a possible error, with a pause right after it, then you can see the flow progress slowly (step by step) and check where the problem arises...

Perhaps try something simple like a Message Box or console message after each line where there is a possible error, with a pause right after it, then you can see the flow progress slowly (step by step) and check where the problem arises...

Alrite i'll try and let u ppl know if i find anything. Thanks neways..!

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.