11 Replies - 363 Views - Last Post: 04 December 2017 - 07:34 AM Rate Topic: -----

#1 coolandon13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 17

Sudoku Edit square function

Posted 03 December 2017 - 03:36 PM

Hey everyone, I am making a code to display a sudoku board right now and allow a selection from different options for editing or displaying, etc. The issue I seem to be facing right now is that when the option for edit comes into play the user will enter in the coordinates of the square they want to edit, I have a switch case to get the correct coordinates according to the input of the user.
int editSquare(char board[][9],int row, int column)
{
   char c[2] = {};
   cout << "What are the coordinates of the square: ";
   cin >> c;
   switch(c[0])
   {
      case 'A':
      case 'a':
         column = 0;
         break;
      case 'B':
      case 'b':
         column = 1;
         break;
      case 'C':
      case 'c':
         column = 2;
         break;
      case 'D':
      case 'd':
         column = 3;
         break;
      case 'E':
      case 'e':
         column = 4;
         break;
      case 'F':
      case 'f':
         column = 5;
         break;
      case 'G':
      case 'g':
         column = 6;
         break;
      case 'H':
      case 'h':
         column = 7;
         break;
      case 'I':
      case 'i':
         column = 8;
         break;
      default:
         cout << "ERROR: Square '" << c << "' is invalid" << endl;
         editSquare(board, row, column);
         break;
   }
   switch(c[1])
   {
      case '1':
         row = 0;
         break;
      case '2':
         row = 1;
         break;
      case '3':
         row = 2;
         break;
      case '4':
         row = 3;
         break;
      case '5':
         row = 4;
         break;
      case '6':
         row = 5;
         break;
      case '7':
         row = 6;
         break;
      case '8':
         row = 7;
         break;
case '9':
         row = 8;
         break;
      default:
         cout << "ERROR: Square '" << c << "' is invalid" << endl;
         editSquare(board, row, column);
         break;
   }
   int value = 0;
   if (board[row][column] >= 0 || board[row][column] <= 9)
   {
      cout << "ERROR: Square '" << c << "' is filled\n";
   }
   else
   {
      cout << "What is the value at '" << c << "': ";
      cin >> value;
      board[row][column] = value;
   }
}


if I cout the "board[row][column]" it will output the correct number by its self, which in this case is 7. It's not working inside the in if statement for some reason. I gave the value of board[row][column] to another variable to test it out and it said it was 55 instead of 7. I also tried cout for the board, and then the value, just to triple check. It showed "7 55" so both answers are printing out correctly, I just don't know where the 55 is coming from.
If you could provide any help with this that would be much appreciated.

This post has been edited by snoopy11: 03 December 2017 - 03:47 PM
Reason for edit:: fixed code tags thanks for trying.


Is This A Good Question/Topic? 0
  • +

Replies To: Sudoku Edit square function

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1454
  • Posts: 4,698
  • Joined: 20-March 10

Re: Sudoku Edit square function

Posted 03 December 2017 - 03:57 PM

This line

if (board[row][column] >= 0 || board[row][column] <= 9)



should be

if (board[row][column] > 0 && board[row][column] <= 9)



apart from that you would need to show how you initialise the array and print it out to get more help, at a guess though the 55 is probably coming from a garbage value because you haven't initialised the board.

This post has been edited by snoopy11: 03 December 2017 - 03:58 PM
Reason for edit:: double post

Was This Post Helpful? 1
  • +
  • -

#3 coolandon13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 17

Re: Sudoku Edit square function

Posted 03 December 2017 - 04:04 PM

Oh, yeah! you are completely right with that code. I did have that set as "&&" before but I was playing with everything and I must have not changed it back. I changed it back and still got the same value.
I did initialize the array in main, would that be something that I would need to reinitialize when passing it to another function? Because I do agree with that coming from garbage.

This post has been edited by Skydiver: 03 December 2017 - 06:32 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 coolandon13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 17

Re: Sudoku Edit square function

Posted 03 December 2017 - 04:22 PM

Here is the code for my first time initializing it.
int main()
{
   char board[9][9] = {};
   int row = 0;
   int column = 0;
   char fileName[256] = {};


This post has been edited by andrewsw: 03 December 2017 - 05:20 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6110
  • View blog
  • Posts: 21,027
  • Joined: 05-May 12

Re: Sudoku Edit square function

Posted 03 December 2017 - 04:23 PM

As an aside, you could simplify those row and column switch statements to just a pair of calculations.

Some code to get the juices flowing:
indexOfC = 'C' - 'A';   // result is 2


Was This Post Helpful? 1
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1454
  • Posts: 4,698
  • Joined: 20-March 10

Re: Sudoku Edit square function

Posted 03 December 2017 - 04:39 PM

board should be initialised like this...

char board[9][9] = {{' ',' '}};

Was This Post Helpful? 1
  • +
  • -

#7 coolandon13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 17

Re: Sudoku Edit square function

Posted 03 December 2017 - 04:45 PM

View PostSkydiver, on 03 December 2017 - 04:23 PM, said:

As an aside, you could simplify those row and column switch statements to just a pair of calculations.

Some code to get the juices flowing:
indexOfC = 'C' - 'A';   // result is 2



Oh! Very cool! Thanks for the suggestions!
Was This Post Helpful? 0
  • +
  • -

#8 coolandon13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 17

Re: Sudoku Edit square function

Posted 03 December 2017 - 04:51 PM

View Postsnoopy11, on 03 December 2017 - 04:39 PM, said:

board should be initialised like this...

char board[9][9] = {{' ',' '}};


Okay, I gave that a try. It doesn't seem to have changed anything. I would like to show you more of my code. But I don't want to post too much on here because it is for an assignment and I don't want to give anything away to others who have to do this assignment.

Would it have anything to do with the array being passed to my function to allow the user to interact, and then from there it is passed to the editSquare function.
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7150
  • View blog
  • Posts: 14,890
  • Joined: 16-October 07

Re: Sudoku Edit square function

Posted 03 December 2017 - 05:06 PM

Do NOT use recursion for looping! Write more functions. e.g.
typedef int Board[BOARD_ROWS][BOARD_COLS];
// don't let them out of these until entered value is valid
int getColumnFromUser();
int getRowFromUser();
int getCellValueFromUser();

// helper
bool cellEmpty(Board, int row, int col);
std::string strPos(int row, int col);

// why are you passing row and column here?  why return int?
// int editSquare(char board[][9],int row, int column) {
void editSquare(Board board) {
    // spin until they enter valid position
    while(true) {
        int col = getColumnFromUser();
        int row = getRowFromUser();
        if (cellEmpty(board, row, col)) {
            cout << "What is the value at " << strPos(row, col) << ": ";
            board[row][col] = getCellValueFromUser();
            break;
        }
        cout << "ERROR: Square " << strPos(row, col) << " is filled\n";
    }
}



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#10 coolandon13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 17

Re: Sudoku Edit square function

Posted 03 December 2017 - 06:22 PM

Wow, that is actually a big help. I guess I was just over complicating it. I just started programming a couple months ago, so I am definitely grateful for the help! I will give this a try and see if it helps out!
Thanks!

This post has been edited by Skydiver: 03 December 2017 - 06:31 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6110
  • View blog
  • Posts: 21,027
  • Joined: 05-May 12

Re: Sudoku Edit square function

Posted 03 December 2017 - 06:30 PM

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7150
  • View blog
  • Posts: 14,890
  • Joined: 16-October 07

Re: Sudoku Edit square function

Posted 04 December 2017 - 07:34 AM

@coolandon13Happy to help. Remember, programming is the fine art of breaking down large, complex, problems, in to many small, simple ones. There are innumerable opinions on how to best do this, but just keeping that in mind helps. One method is to try to have a function do one, and only one, thing. And, that function should know only as much as it needs to. Smaller and simpler.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1