ok so im checking a 9x9 soduku game and i need help checking the part where the 3x3 boxes are all cheked. i cant seem to output the errors for the 3x3 box check can someone help me out. i want it to output which box that has the error with the number that is repeated in the box (replace the line that says cout<<"DISPLAY BOX ERROR HERE" <<endl;)

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;

void readAPuzzle(int grid[] [9]);
void printGrid(int grid[] [9]);
void ValidPuzzle(int grid[] [9]);

int main()
{
    int grid[9][9];

    readAPuzzle(grid);
    printGrid(grid);
    ValidPuzzle(grid);
    system("pause");
    return 0;
}


void readAPuzzle(int grid[] [9])
{
    ifstream in_f;    
    in_f.open("soduku.txt");
    
    for (int r = 0; r < 9; r++)
    for (int c = 0; c < 9; c++)
    in_f >> grid[r] [c];
}



//Prints out the puzzle read from file
void printGrid(int grid[] [9])
{

cout<<"Your Puzzle Is Listed Below: "<<endl;
for (int r = 0; r < 9; r++)
{

    if ((r)%3 == 0)
    cout<<endl<<endl;
    for (int c = 0; c < 9; c++)
{

    cout << (grid[r] [c]) << " ";
    if ((c+1)%3 == 0)
    cout<<"   ";
    }
    cout<<endl;
    }
    cout<<endl;
}


//Check Validity Of Puzzle
void ValidPuzzle(int grid[] [9])
{
    int sum = 0;
    int nototest = 0;
    int count = 0;
    int no_to_test;
    int box[9][9];

    


// check rows
cout<<endl;
for(int r=0;r<9;r++)
{
for(int c=0;c<9;c++)
{
no_to_test = c+1;
count=0;
for(int c=0;c<9;c++)
{
if(grid[r][c]==no_to_test)
count++;
}
if(count>1)
cout<<"The Number: "<<no_to_test<< " is duplicated on row: " << r+1<<endl;
}    
}
    

// check columns
for(int c=0;c<9;c++)
for(int r=0;r<9;r++)
{
no_to_test = r+1;
count=0;

for(int r=0;r<9;r++)
{
    if(grid[r][c]==no_to_test)
    count++;
}
if(count>1)
{
    cout <<"The Number: "<<no_to_test<< " is duplicated on column: " << c+1;
    cout<<endl;
}
}


//Check BOXES!
for (int r = 0; r < 9; r++) // zeroing out boxes
        for (int c = 0; c < 9; c++)
            box[r][c] = 0;

for (int r = 0; r < 3; r++)
    {
        for (int c = 0; c < 3; c++)
        {// keep track of each number in the first box, etc.
            box[0][grid[r][c]-1]++;
        }

        for (int c = 3; c < 6; c++)
        {
            box[1][grid[r][c]-1]++;
        }

        for (int c = 6; c < 9; c++)
        {
            box[2][grid[r][c]-1]++;
        }
    }

    for (int r = 3; r < 6; r++)
    {
        for (int c = 0; c < 3; c++)
        {
            box[3][grid[r][c]-1]++;
        }

        for (int c = 3; c < 6; c++)
        {
            box[4][grid[r][c]-1]++;
        }

        for (int c = 6; c < 9; c++)
        {
            box[5][grid[r][c]-1]++;
        }
    }

    for (int r = 6; r < 9; r++)
    {
        for (int c = 0; c < 3; c++)
        {
            box[6][grid[r][c]-1]++;
        }

        for (int c = 3; c < 6; c++)
        {
            box[7][grid[r][c]-1]++;
        }

        for (int c = 6; c < 9; c++)
        {
            box[8][grid[r][c]-1]++;
        }
    }

    for (int r = 0; r < 9;r++)
    {
        for (int c = 0; c < 9; c++)
        {// if a number has a value other than 1 we counted it twice
            if (box[r][c] != 1)
                cout<<"DISPLAY BOX ERROR HERE" <<endl;
        }
        cout<<endl;
}
    }

Perhaps a function called int checkBox( int grid[] [9], int sx, int sy ); which checks a 3x3 square starting at sx,sy.

Where example calls would be

for ( sx = 0 ; sx < 9 ; sx += 3 ) {
  for ( sy = 0 ; sy < 9 ; sy++ ) {
    checkBox( grid, sx, sy );
  }
}
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.