0

I have to write a program that takes a completed sudoku board, saves only the numbers (meaning all the symbols used between the numbers to separate them such as '-', '|' etc cant be saved) into a two-dimensional array.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int input[11] = { 0 };
    int sudoku[9][9] = { 0 };

    for (int line = 0; line <= 10; line++)
    {
        cin >> input[line];
    }

    system("PAUSE");
    return 0;
}

This is the only working code I've got so far. I've tried different kinds of for loops to get this done but I can't figure why it doesn't work.

So I wanted to ask, is it even possible save all the numbers of a string into a multi-dimensional array? And if it's not, where is my approach wrong or how could I solve this task?

One example of the input would be:

.5.1.4.|.8.6.9.|.7.2.3
.8.7.2.|.3.4.5.|.6.1.9
.9.6.3.|.2.1.7.|.5.4.8
-------|-------|-------
.6.2.8.|.1.3.4.|.9.5.7
.1.9.7.|.6.5.2.|.8.3.4
.4.3.5.|.7.9.8.|.1.6.2
-------|-------|-------
.2.4.6.|.9.7.1.|.3.8.5
.7.5.1.|.4.8.3.|.2.9.6
.3.8.9.|.5.2.6.|.4.7.1
Scott Hutchinson
  • 1,703
  • 12
  • 22
Kazzy K
  • 11
  • 4
  • It certainly seems possible. It would be helpful if you provided an example input string other than "". – Scott Hutchinson Nov 13 '19 at 00:59
  • Yes, it is possible to interpret the content of a string in order to populate an array. Where your approach is wrong is that you expect it to somehow happen without specifying or producing code which does it. Try closing down your computer, and starting with a pen and paper. Describe how the numbers/symbols will be within the string.Then describe how you would extract a set of integral values from each string. Imagine you are writing a recipe that describes, to someone else who knows nothing about sudoku, how to do that. Only then, try to work out how to translate that description into code. – Peter Nov 13 '19 at 01:00
  • What is string input []. It's not c style char array nor c++ library string . – Nikhil Badyal Nov 13 '19 at 01:12
  • Use a `std::istringstream` and `std::getline` to separate the string into lines. then use another `std::istringstream` and probably another `std::getline` to pull apart each line. – user4581301 Nov 13 '19 at 01:20
  • @ScottHutchinson Sorry for that, added an example now. – Kazzy K Nov 13 '19 at 01:49
  • @problematicDude That was a mistake, corrected now – Kazzy K Nov 13 '19 at 01:49
  • It's tough to figure out what's wrong with your approach when you have not shown the approach that "doesn't work" (whatever that is supposed to mean here). – JaMiT Nov 13 '19 at 02:11
  • cin ONLY uses whitespace as a delimiter so it is trying to read the entire line into an integer but it isn't and integer so it doesn't work. Either use getline with a doifferent delimiter https://stackoverflow.com/questions/7297623/how-to-provide-your-own-delimiter-for-cin or change the delimiter for cin itself https://stackoverflow.com/questions/7302996/changing-the-delimiter-for-cin-c – Jerry Jeremiah Nov 13 '19 at 05:10

1 Answers1

1

One approach is to use regular expressions. This way the formatting of the sudoku board can change but your will still be able to parse out the numbers.

The reason I broke it into two for loops was to easily ignore the row that has no numbers in it.

#include <iostream>
#include <regex>
#include <string>
#include <vector>

int main()
{
  std::string line;

  // this regular expression matches a single digit
  std::regex exp("(\\d)");
  std::smatch res;

  int sudoku[9][9] = {{0}};

  int row = 0;
  for (int i = 0; i < 3; ++i)
  {
    for (int j = 0; j < 3; ++j)
    {
      // get a line of the board
      std::getline(std::cin, line);

      // search for the next digit in the line
      for (int k = 0; std::regex_search(line, res, exp); ++k)
      {
        // convert the digit into an integer and store it in the board
        sudoku[row][k] = std::stoi(res[0]);

        // the rest of the line after the first match becomes the new 
        // line so that we can search for the next digit
        line = res.suffix();
      }
      row += 1;
    }
    // ignore every third row that is used to separate the board sections
    std::getline(std::cin, line);
  }

  for (int i = 0; i < 9; ++i)
  {
    for (int j = 0; j < 9; ++j)
    {
      std::cout << sudoku[i][j] << " ";
    }
    std::cout << std::endl;
  }
  return 0;
}

For your example board, it produces this output:

5 1 4 8 6 9 7 2 3
8 7 2 3 4 5 6 1 9
9 6 3 2 1 7 5 4 8
6 2 8 1 3 4 9 5 7
1 9 7 6 5 2 8 3 4
4 3 5 7 9 8 1 6 2
2 4 6 9 7 1 3 8 5
7 5 1 4 8 3 2 9 6
3 8 9 5 2 6 4 7 1
Philip Nelson
  • 1,027
  • 12
  • 28