I appear to need some help with a sudoku solver I'm working on. It's supposed to check the row, column, and grid. When I run my program it displays the puzzle, but then it ends abruptly. I was hoping someone can take a look at my functions and see if I'm doing something wrong.

void Sudoku::Row(int ptx,int pty)
{
	int e=0;
	flag=1;
	for(int d=0;d<8;d++)
	{
		for(;e<8;e++)
		{
			if(Puzzle[d][pty]==Possible[e])
			{
				remains--;
				int f=e;
				while(e!=8)
				{
					Possible[f]=Possible[f+1];
					f++;
				}
			}
		}
		e=0;
	}
	cout<<"
	if (remains==1)
	{
		Update(ptx,pty);
	}
return;
}

void Sudoku::Column(int ptx,int pty)
{
	int g=0;
	flag=1;
	for(int f=0;f<8;f++)
	{
		for(;g<8;g++)
		{
			if(Puzzle[ptx][f]==Possible[g])
			{
				remains--;
				int h=g;
				while(g<9)
				{
				Possible[h]=Possible[h+1];
				h++;
				}
			}
		}
		g=0;
	}
	if (remains==1)
	{
		Update(ptx,pty);
	}
return;
}

void Sudoku::Grid(int ptx,int pty)
{
	flag=1;
	int top,left;
	top=(pty/3)*3;
	left=(ptx/3)*3;

	for(int h=0;h<3;h++)
	{
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<9;j++)
			{
				if(Puzzle[left+i][top+h]==Possible[j])
				{
					remains--;
					int k=j;
					while(j<9)
					{
						Possible[j]=Possible[j+1];
						j++;
					}
					j=k;
				}
			}
			int j=0;
		}
		int i=0;
	}
	if (remains==1)
	{
		Update(ptx,pty);
	}
return;
}

void Sudoku::Update(int ptx,int pty)
{
	flag=0;
	if (Puzzle[ptx][pty]==0)
	{
		Puzzle[ptx][pty]=Possible[0];
		Fill_9(); //fills "possibilities" array with 1-9
	}
	else
	{
		cout<<"|-=ERROR=-| That space is already filled."<<endl;
	}
	Display();
return;
}

I've been staring at this program for so long I've gotten sick of it already and I'm probably making a really dumb mistake. Any help would be really great.

Without the rest of the program, and a puzzle, it's kind of hard to find an error. Do your functions even get called correctly? Does the program crash or just end without (seeming to) solve the puzzle.

Send stimulus check for new crystal ball batteries and we might be able to help more.

Sorry about that. I was trying to keep it short and sweet. Here's all the function calls.

#include <iostream>
using namespace std;
#include "doth.h"
int main()
{

	Sudoku call;

	int flag=0,ptx=0,pty=0;

	call.Display();	
	for(;flag != 1;)
	{
		if (call.Puzzle[ptx][pty]==0)
		{
			call.Fill_9();
			call.Row(ptx,pty);
			call.Column(ptx,pty);
			call.Grid(ptx,pty);
		}
		if (ptx<8)
		{
			ptx++;
		}
		if (ptx==8)
		{
			ptx=0;
			pty++;
		}
		if (pty>8)
		{
			ptx=0;
			pty=0;
		}
	}

	call.Display();

return(0);
}

As for the puzzle, I've tried various ones, but this is the one I'm currently trying:

9 5 7 0 0 0 0 0 0 
1 0 6 7 0 0 5 0 0
0 3 0 0 0 1 0 0 2
0 0 2 0 4 3 9 0 6
3 0 0 0 0 0 0 0 4
5 0 4 8 2 0 3 0 0
7 0 0 9 0 0 0 4 0
0 0 1 0 0 7 8 0 9
0 0 0 0 0 0 7 2 3

And just to be clear, here are the rest of the functions:

Sudoku::Sudoku()
{
	ifstream Read ("Puzzles.txt");
	for (int a=0; a<9;a++)
	{
		for (int b=0;b<9;b++)
		{
			Read>>Puzzle[a][b];
		}
	}
	flag=0;
	remains=9;
}

void Sudoku::Display()
{
 for (int m=0;m<9;m++)
	{
	for (int n=0;n<9;n++)
		{
			if(Puzzle[m][n]==0)
			{
				cout<<". ";
			}
			else
			{
				cout<<Puzzle[m][n]<<" ";
			}
		}
		cout<<endl;
	}
 cout<<"\n";
return;
}

void Sudoku::Update(int ptx,int pty)
{
	flag=0;
	if (Puzzle[ptx][pty]==0)
	{
		Puzzle[ptx][pty]=Possible[0];
		Fill_9();
	}
	else
	{
		cout<<"|-=ERROR=-| That space is already filled."<<endl;
	}
	Display();
return;
}

void Sudoku::Fill_9()
{
	int v=0;
	for (int c=1;c<=9;c++)
	{
		Possible[v]=c;
		v++;
	}
	remains=9;
return;
}

It displays the puzzle, but it crashes right after that.

How about the part that begins with
class Sudoku......

Can't build or debug the program without the whole program.

And why the penchant for partial for loops for(;flag != 1;) If you want to do a condtional (while) loop, say so. while( flag != 1 )

Header file:

class Sudoku
{
private:
	int remains;
	
public:
	Sudoku();
	int flag;
	int Possible[9];
	int Puzzle[9][9];
	void Display();
	void Peek();
	void Fill_9();
	void Update(int ptx, int pty);
	void Row(int ptx,int pty);
	void Column(int ptx,int pty);
	void Grid(int ptx,int pty);
};

That should be all of it.

in the closing code tag, use a / instead of \ to make it end properly.

I'll let you know what I find in the code shortly (I hope)

Oops, my bad. I'll just fix that.

How about this in your row function?

while(e!=8)
	{
		Possible[f]=Possible[f+1];
		f++;
	}

What would cause that loop to ever end? And you have a similar problem in the column function.

Considering the loop in main( ), how will the loop control "flag" ever get set so as to stop the loop? You're stuck in nested infinite loops!

One more comment. Variable names. You have a "ptx" that is the index of rows (a vertical direction to most folks) and "pty" that is the column index (a horizontal direction). These names are contrary to the usual association of x and y as horizontal and vertical axes, respectively. This makes reading the code confusing. How about using r and c or row and col to make it clearer what the variables refer to? Then there's all your absolutely meaningless a, b, c, d, e, f, g.... variables.

I just had to go grab a couple aspirin! ;)

commented: Have a green pill on me! +15
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.