Hi,
This is not a schoolwork assignment, I'm just learning/practicing C++.
I am making a sudoku program/app/game and so far it's working great (it's a WIP so it doesn't generate the puzzles yet). I am trying to use SFML to make a GUI but was having issues with the VS compiler so I'm switching to GCC. Besides the fact that the exe is 700kb larger it isn't working correctly.
Here's my code, it's not commented, if this is an issue than I can comment and repost it:
[cpp]#include <iostream>
#include <fstream>
using namespace std;
bool closef = false;
int board[9][9];
fstream puzzle;
int getNum() {
if (!puzzle.is_open()) puzzle.open("puzzle.txt", fstream::in);
int x;
puzzle >> x;
return x;
if (closef) puzzle.close();
}
int checkBoard(int num, int cellx, int celly) {
int cubex = ((cellx / 3) - ((cellx % 3) / 3)) * 3;
int cubey = ((celly / 3) - ((celly % 3) / 3)) * 3;
for (int i = 0; i < 9; i++) if (num == board[cellx]) return false;
for (int i = 0; i < 9; i++) if (num == board[celly]) return false;
if (num > 9) return false;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (num == board[i + cubex][j + cubey]) return false;
return true;
}
void drawBoard() {
int blanks = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[j] == 0) {
blanks++;
cout << " ";
}
else cout << board[j] << " ";
}
cout << endl;
}
if (blanks > 0) cout << "Only " << blanks << " to go!" << endl;
else cout << "Congratulations! You solved it!!" << endl;
}
int main() {
int num, x, y, tempNum = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
tempNum = getNum();
if (tempNum == 0) cout << " ";
else {
board[j] = tempNum;
cout << board[j] << " ";
}
}
cout << endl;
}
while (true) {
cin >> num >> x >> y;
cout << checkBoard(num, x, y) << endl;
if (checkBoard(num, x, y)) board[x][y] = num;
drawBoard();
}
puzzle.close();
return 0;
}
[/cpp]
And puzzle.txt:
[cpp]0 0 7 9 1 5 3 2 6
6 2 5 7 4 3 1 9 8
9 3 1 2 6 8 7 5 4
2 8 3 6 9 4 5 1 7
7 0 4 5 0 2 8 6 9
5 9 6 1 8 7 4 3 2
4 7 2 3 5 9 6 8 1
3 6 8 4 2 1 9 0 5
1 5 9 0 7 6 2 4 3[/cpp]
This compiles and works perfectly in VS but when I compile it in GCC it will only let me enter the first 8 (enter "8 0 0", number x y) and no other numbers, there are no errors and the program works perfectly otherwise.
Any ideas as to what I can do?
Thank you in advance.
P.S. If anyone has any comments or notices mistakes in my code, please let me know! I'm trying to learn more.
This is not a schoolwork assignment, I'm just learning/practicing C++.
I am making a sudoku program/app/game and so far it's working great (it's a WIP so it doesn't generate the puzzles yet). I am trying to use SFML to make a GUI but was having issues with the VS compiler so I'm switching to GCC. Besides the fact that the exe is 700kb larger it isn't working correctly.
Here's my code, it's not commented, if this is an issue than I can comment and repost it:
[cpp]#include <iostream>
#include <fstream>
using namespace std;
bool closef = false;
int board[9][9];
fstream puzzle;
int getNum() {
if (!puzzle.is_open()) puzzle.open("puzzle.txt", fstream::in);
int x;
puzzle >> x;
return x;
if (closef) puzzle.close();
}
int checkBoard(int num, int cellx, int celly) {
int cubex = ((cellx / 3) - ((cellx % 3) / 3)) * 3;
int cubey = ((celly / 3) - ((celly % 3) / 3)) * 3;
for (int i = 0; i < 9; i++) if (num == board[cellx]) return false;
for (int i = 0; i < 9; i++) if (num == board[celly]) return false;
if (num > 9) return false;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (num == board[i + cubex][j + cubey]) return false;
return true;
}
void drawBoard() {
int blanks = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[j] == 0) {
blanks++;
cout << " ";
}
else cout << board[j] << " ";
}
cout << endl;
}
if (blanks > 0) cout << "Only " << blanks << " to go!" << endl;
else cout << "Congratulations! You solved it!!" << endl;
}
int main() {
int num, x, y, tempNum = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
tempNum = getNum();
if (tempNum == 0) cout << " ";
else {
board[j] = tempNum;
cout << board[j] << " ";
}
}
cout << endl;
}
while (true) {
cin >> num >> x >> y;
cout << checkBoard(num, x, y) << endl;
if (checkBoard(num, x, y)) board[x][y] = num;
drawBoard();
}
puzzle.close();
return 0;
}
[/cpp]
And puzzle.txt:
[cpp]0 0 7 9 1 5 3 2 6
6 2 5 7 4 3 1 9 8
9 3 1 2 6 8 7 5 4
2 8 3 6 9 4 5 1 7
7 0 4 5 0 2 8 6 9
5 9 6 1 8 7 4 3 2
4 7 2 3 5 9 6 8 1
3 6 8 4 2 1 9 0 5
1 5 9 0 7 6 2 4 3[/cpp]
This compiles and works perfectly in VS but when I compile it in GCC it will only let me enter the first 8 (enter "8 0 0", number x y) and no other numbers, there are no errors and the program works perfectly otherwise.
Any ideas as to what I can do?
Thank you in advance.
P.S. If anyone has any comments or notices mistakes in my code, please let me know! I'm trying to learn more.