### c++ code (console) to gui/windows app

Hi,
I am a student, 12th standard. I recently made this program to solve a sudoku (enter numbers column wise and 0 for empty boxes) in Turbo C++ 3.0 (as it was part of my school project, only this compiler was allowed). I might be going for an inter-school competition for the software display and I wanted to know that if i could make this code work as a windows app.
I have Dev C++.
Any help would be appreciated.
Thanks.
Here is my code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240`` ``````#include #include #include int input[9][9]; int output[9][9]; int input_value(int x, int y, int value) { int i,j; // Scan horizontally and vertically for (i = 0; i < 9; i++) { if (value == output[i][y] || value == output[x][i]) { return 0; } } // Scan its own square if (x < 3) { if (y < 3) { for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { if (value == output[i][j]) { return 0; } } } } else if (y < 6) { for (i = 0; i < 3; i++) { for (j = 3; j < 6; j++) { if (value == output[i][j]) { return 0; } } } } else { for (i = 0; i < 3; i++) { for (j = 6; j < 9; j++) { if (value == output[i][j]) { return 0; } } } } } else if (x < 6) { if (y < 3) { for (i = 3; i < 6; i++) { for (j = 0; j < 3; j++) { if (value == output[i][j]) { return 0; } } } } else if (y < 6) { for (i = 3; i < 6; i++) { for (j = 3; j < 6; j++) { if (value == output[i][j]) { return 0; } } } } else { for (i = 3; i < 6; i++) { for (j = 6; j < 9; j++) { if (value == output[i][j]) { return 0; } } } } } else { if (y < 3) { for (i = 6; i < 9; i++) { for (j = 0; j < 3; j++) { if (value == output[i][j]) { return 0; } } } } else if (y < 6) { for (i = 6; i < 9; i++) { for (j = 3; j < 6; j++) { if (value == output[i][j]) { return 0; } } } } else { for (i = 6; i < 9; i++) { for (j = 6; j < 9; j++) { if (value == output[i][j]) { return 0; } } } } } return value; } int solve(int x, int y) { int temp,i,j; if (output[x][y] == 0) { for (i = 1; i < 10; i++) { temp = input_value(x,y,i); if (temp > 0) { output[x][y] = temp; if (x == 8 && y == 8) { return 1; } else if (x == 8) { if (solve(0,y+1)) return 1; } else { if (solve(x+1,y)) return 1 ; } } } if (i == 10) { if (output[x][y] != input[x][y]) output[x][y] = 0; return 0; } } else { if (x == 8 && y == 8) { return 1; } else if (x == 8) { if (solve(0,y+1)) return 1; } else { if (solve(x+1,y)) return 1; } } } int main() { int i,j; clrscr(); for(int x=0;x<9;x++) { for(int y=0;y<9;y++) { gotoxy(x+1,y+1); cin>>input[x][y]; } } for (i = 0; i < 9; i++) { for (j = 0; j < 9; j++) { output[i][j] = input[i][j]; } } if (solve(0,0)) { gotoxy(2,10); cout<<"Solution is :"; for (i = 0; i < 9; i++) { for (j = 0; j < 9; j++) { gotoxy(i+1,j+12); cout<
I don't know anything about windows, so I can only offer some style points. Sorry, this is not going to help in any way towards your goal, but hopefully you might learn a couple things that might be useful.

My main comment is that you could do with splitting the whole thing into more functions, will make it easier to read & understand.

The input_value function is very long. Where is the call to input_value? Where is the definition of gotoxy?

The normal way of presenting code, is to have the declaration of functions, then main(), then the definition of functions. This way we don't have to scroll through all the code to get to main.

Your indenting makes it hard to read the code, there are various styles. I put the opening brace at the end of the line after the if or while for etc. Others put it on the following line, immediately below (that is the same column number as) the if, for, while etc. The closing brace is probably more important, it needs to be in the same column as the opening if, for etc, so that one can see where the block ends. Indenting is important too, a lot of people set a tab to be 4 spaces. I put braces around one liners, as you have done, to save myself when I add code in the future.Like this:

 ``12345`` ``````for (i = 0; i < 9; i++){ for (j = 0; j < 9; j++){ output[i][j] = input[i][j]; } }``````

or this:

 ``1234567`` ``````for (i = 0; i < 9; i++) { for (j = 0; j < 9; j++) { output[i][j] = input[i][j]; } }``````

This is a matter of style, choose one you like (there are others also) & stick to it.

Also comments, are good always. It is good practice to put them with variable declarations, and at the start of functions. Your current code makes it hard to see the start / end of functions.

Hope this helps a little bit.
Topic archived. No new replies allowed.