0

I am making a Sudoku program and my i have a test.txt file that reads

53__7____    
6__195___
_98____6_
8___6___3
4__8_3__1
7___2___6
_6____28_
___419__5
____8__79

where the "_" are actually spaces. The reason i give you _ is so you can see that there are literally only 9 characters on each line.

I was thinking that I would have to do something like having GRID[row][column], however I frankly don't know what types I should put my arrays as and I am just lost.

I simply want to make it so when i want to output say GRID[0][0] it returns 5, while if i ask for GRID[0][3] it returns a ' '.

It is getting it so the array store both the numbers and the spaces is where i am getting completely lost

What I currently have tried so far:

int main()
{
 ifstream myfile(test.txt);
 string line;
 char sudoku_grid[9][9];

 if (myfile.is_open())
 {
  while(myfile.good())
   {
    getline(myfile, line);
    cout << sudoku_grid[line] << endl;
   }
   myfile.close();
  }
 else cout << "error";
 return 0;
}

it returns the error line 12: no match for 'operator [ ]' in 'sudoku_grid[line]'

Here is my attempt though guidelines through you guys:

int main()
{
 ifstream myfile(test.txt);
 string line;
 char sudoku_grid[9][9];

 if (myfile.good())
 {
  for(int i = 0; i < 9; i++)
   {
    getline(myfile, line);
    for(int j = 0; j < 9; j++)
       {
       if (line[j] == ' ')
          sudoku_grid[j][i] = -1;
       else  sudoku_grid[j][i] = line[i];
       }
    cout << sudoku_grid[i] << endl;
   }
   myfile.close();
  }
 else cout << "error";
 return 0;
}

The result is a very awkward answer of strange letters and a new numbers.

3
  • C or C++? Also can you give us your attempts in code? Commented Oct 4, 2012 at 9:46
  • Is this C or C++ as the answers will vary depending? Commented Oct 4, 2012 at 9:54
  • @keyerer Your title says C though... Commented Oct 4, 2012 at 10:14

3 Answers 3

2

I'll just give you the algorithm/logic, not going to write the code for you. Try it and come back when stuck.

  1. Initialize output in memory 2D array: numbers[9][9]
  2. Open the file
  3. Until there is no line left in the file:
    a. Get the line i
    b. Until there are no more characters in the line:
      b1. Get each character of the line c
      b2. If the character is not space, then numbers[i]=c, else numbers[i]=-1

Your array can be made up of int and in b2 if a whitespace is encountered you can insert -1 to indicate the absence of a number. Of course your code manipulating numbers array needs to take that into account.

Sign up to request clarification or add additional context in comments.

4 Comments

Check my most recent change, it gives me a weird answer of weird letters and a new numbers what did i do wrong?
You have confused i and j in your code: 1) sudoku_grid[j][i] = -1; should be sudoku_grid[i][j] = -1; 2) sudoku_grid[j][i] = line[i] should be sudoku_grid[i][j] = line[j];
I get really weird answers of usually where spaces are i get y with 2 dots over it and on the end of first line i get @, and last 2 lines i get 5w@ and aNi2 on last line
scrap that when i used 0 instead of ' ' strange
0

Since you need to store both chars and integer type values, use char. each of your integer lies in the range 0-9, so can be stored as a character.

char Grid[9][9];

now you can read each character from the string and store it in the array. It will not only keep your spaces intact but also each character. Always remember to use ASCII codes to access the elements of the grid. For 0-9, ASCII codes are 48-57, ASCII code for space is 32.

Hope it helps...

Edit code: Here is the simplest example... PLace your test file in d:, or edit the path of file in code

int main (void)
{
    FILE    *fp = fopen("d:\\test.txt","r");
    char sudoku_grid[9][9], ch;
    // I am assuming that file is valid and data in that is also valid
    if(fp)
    {
        for(int i = 0; i < 9; i++)
        {            
            for(int j = 0; j < 9; j++)
            {
                //too read each character
                ch = fgetc(fp);
                sudoku_grid[i][j] = ch;   
            }
            // to read '\n' from the line
            ch = fgetc(fp);
        }
//for checking if data went correctly
        for(int i = 0; i< 9;i++)
        {
            for(int j= 0; j<9;j++)
                cout<<sudoku_grid[i][j];
            cout<<endl;
        }
    }


 return 0; 
}

7 Comments

Since he's mentioned Sudoku, you really should steer him away from a two dimensional array, since it is a very poor choice to represent a Sudoku board. And you definitely don't want to store the values as character codes, since you'll want to use them as indexes in the implementation.
@JamesKanze For comparing and all other purposes he can easily use ASCII codes as stated in answer, simple +/- will serve the purpose.
can you check my tests that i have ran using what you suggested and give me some hints on what I did wrong?
I tried running that and found that on my 4-6 line the first character was cut off, and on the last line i get everything pushed to the right two spaces and have the last two spaces be y with two dots over it
Correction on the 3-4 line however, it seems that it just moved it to the next line, but still
|
0

In the first code you get the error message because sudoku_grid can only be indexed by numbers and not by strings.

In the second code the line

sudoku_grid[j][i] = line[i];

should probably be

sudoku_grid[j][i] = line[j];

Does this answer your question?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.