Sign in with
Sign up | Sign in
Your question
Solved

C++ Sudoku program won't compile properly in GCC

Last response: in Applications
Share
December 13, 2012 1:49:35 PM

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:

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. bool closef = false;
  7. int board[9][9];
  8. fstream puzzle;
  9.  
  10. int getNum() {
  11. if (!puzzle.is_open()) puzzle.open("puzzle.txt", fstream::in);
  12. int x;
  13. puzzle >> x;
  14. return x;
  15. if (closef) puzzle.close();
  16. }
  17.  
  18. int checkBoard(int num, int cellx, int celly) {
  19. int cubex = ((cellx / 3) - ((cellx % 3) / 3)) * 3;
  20. int cubey = ((celly / 3) - ((celly % 3) / 3)) * 3;
  21.  
  22. for (int i = 0; i < 9; i++) if (num == board[cellx][i]) return false;
  23. for (int i = 0; i < 9; i++) if (num == board[i][celly]) return false;
  24.  
  25. if (num > 9) return false;
  26.  
  27. for (int i = 0; i < 3; i++)
  28. for (int j = 0; j < 3; j++)
  29. if (num == board[i + cubex][j + cubey]) return false;
  30.  
  31. return true;
  32. }
  33.  
  34. void drawBoard() {
  35. int blanks = 0;
  36.  
  37. for (int i = 0; i < 9; i++) {
  38. for (int j = 0; j < 9; j++) {
  39. if (board[i][j] == 0) {
  40. blanks++;
  41. cout << " ";
  42. }
  43. else cout << board[i][j] << " ";
  44. }
  45. cout << endl;
  46. }
  47. if (blanks > 0) cout << "Only " << blanks << " to go!" << endl;
  48. else cout << "Congratulations! You solved it!!" << endl;
  49. }
  50.  
  51. int main() {
  52. int num, x, y, tempNum = 0;
  53. for (int i = 0; i < 9; i++) {
  54. for (int j = 0; j < 9; j++) {
  55. tempNum = getNum();
  56. if (tempNum == 0) cout << " ";
  57. else {
  58. board[i][j] = tempNum;
  59. cout << board[i][j] << " ";
  60. }
  61. }
  62. cout << endl;
  63. }
  64.  
  65. while (true) {
  66. cin >> num >> x >> y;
  67. cout << checkBoard(num, x, y) << endl;
  68. if (checkBoard(num, x, y)) board[x][y] = num;
  69. drawBoard();
  70. }
  71.  
  72. puzzle.close();
  73. return 0;
  74. }


And puzzle.txt:
  1. 0 0 7 9 1 5 3 2 6
  2. 6 2 5 7 4 3 1 9 8
  3. 9 3 1 2 6 8 7 5 4
  4. 2 8 3 6 9 4 5 1 7
  5. 7 0 4 5 0 2 8 6 9
  6. 5 9 6 1 8 7 4 3 2
  7. 4 7 2 3 5 9 6 8 1
  8. 3 6 8 4 2 1 9 0 5
  9. 1 5 9 0 7 6 2 4 3


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.

Best solution

a b L Programming
December 13, 2012 6:40:28 PM

What do you mean it won't let you enter? Does the program terminate when you enter 8 0 0? Are you by accident using quotes to enter numbers?
Share
December 13, 2012 9:34:08 PM

Sorry I didn't mark this as sovled, I put a lot of work into it today and now I have SFML and GCC working.

To anyone with the same or a similar issue, I had to recompile the SFML libs because the latest compiled ones aren't compatible with GCC 4.7.2.
m
0
l
December 13, 2012 9:36:14 PM

Best answer selected by AMKohn.
m
0
l
!