Greetings everyone.
Starting off to the point: I am trying to build a program with C++ (very basic!) that will solve Sudokus. I know there are programs out there that will do this - but I wanted to try this as a challenge to see actually if I know what I am learning. There are three steps:
(1) Entering all known numbers.
(2) Displaying the board.
(3) Solving.
(1) was pretty easy to set up. I just created an array. I haven't started (3) yet; that'll probably take some logic and time. So that leaves me at (2) - shouldn't be too hard, right? Well...
My problem lies with displaying the board. I used some for statements, as shown below:
Code:
while (option == 2)
{
for (i=0;i<9;i++){
for (j=0;j<9;j++){
cout << "[" << sudoku[i][j] << "]";
counter++;
if ((counter % 9) == 0)
{
cout << "\t";
}
}
cin.get();
}
}
(some preliminary stuff: option is my menu variable to get the user's input of which of the 3 things to do. i and j are the variables to use when displaying the board. counter is a variable that I use to determine when to put a break in the line to have 9 lines of 9 cells of data, rather than one huge glop of 81 pieces of data!)
However, when I do this, it'll display pieces of data I entered in step (1), but it will never stop! I can keep hitting ENTER forever. It will loop back (ie; the 10th row, 1st column will have the same data piece as 1st row, 1st column). Also, while the new line part with the variable counter works, only for lines 3 and onward. Lines 1 and 2 share the same line, but then it's good for lines 3, 4, 5, 6, 7, 8, and 9.
So here are my two questions:
(1) How can I get the array's display of data to end after 3 lines?
(2) How can I get the second line to be on a seperate line than line one?
Here is my full code:
Code:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Sudoku Solving Program";
int sudoku[9][9];
int a,b = 0;
for (a=0;a<9;a++){
for (b=0;b<9;b++){
sudoku[a][b] = 0; }
}
int go = 1;
int row = 0;
int column = 0;
int knownnumber = 0;
int menu = 1;
int option = 1;
int counter = 0;
int i, j = 0;
while (menu == 1)
{
cout << "\nWhat do you want to do?";
cout << "\n1 = Enter a Number";
cout << "\n2 = View Board";
cout << "\n3 = Solve";
cout << "\nYour choice: ";
cin >> option;
while (go == 1 && option == 1)
{
cout << "\nEnter the row of a number you know: ";
cin >> row;
cout << "Enter the column of that number: ";
cin >> column;
cout << "Enter the number: ";
cin >> knownnumber;
sudoku[row-1][column-1] = knownnumber;
cout << "Keep going? 1 = Yes / 0 = No: ";
cin >> go;
}
while (option == 2)
{
for (i=0;i<9;i++){
for (j=0;j<9;j++){
cout << "[" << sudoku[i][j] << "]";
counter++;
if ((counter % 9) == 0)
{
cout << "\t";
}
}
cin.get();
}
}
}
}
Critiques on the code are also welcome! Thank you to anyone that helps (much less reads this whole post! ).